Ejemplo n.º 1
0
Archivo: serv.go Proyecto: Ganben/blog
// init server,
// set static files, theme and middleware
func InitServer(_ interface{}) *action.Result {

	// set static directory
	app.Server.Use(tango.Static(tango.StaticOptions{
		RootPath: filepath.Join(app.Config.UserDirectory, app.Config.UserThemeDirectory),
		Prefix:   "/theme",
	}))
	app.Server.Use(tango.Static(tango.StaticOptions{
		RootPath: filepath.Join(app.Config.UserDirectory, app.Config.UserUploadDirectory),
		Prefix:   "/upload",
	}))

	// set theme directory
	app.Server.Use(renders.New(renders.Options{
		Reload:     true,
		Directory:  filepath.Join(app.Config.UserDirectory, app.Config.UserThemeDirectory),
		Extensions: []string{".tmpl"},
	}))

	// binding middleware
	app.Server.Use(binding.Bind())
	app.Server.Use(base.AuthHandler())

	return action.OkResult(nil)
}
Ejemplo n.º 2
0
// add middleware to server
func ServeMiddleware(ctx *cli.Context) {
	vars.Server.Use(base.LoggingHandler())
	vars.Server.Use(base.Recovery(true))
	for prefix, path := range vars.StaticDirectory {
		vars.Server.Use(tango.Static(tango.StaticOptions{
			RootPath: path,
			Prefix:   prefix,
		}))
	}
	vars.Server.Use(xsrf.New(time.Second * 3600))
	vars.Server.Use(binding.Bind())
	vars.Server.Use(base.AuthHandler())
	vars.Server.Use(base.SettingHandler())
	vars.Server.Use(base.I18nHandler())
	vars.Server.Use(renders.New(renders.Options{
		Reload:     true,
		Directory:  "static",
		Extensions: []string{".tmpl"},
		Funcs: template.FuncMap{
			"Str2HTML":             utils.Str2HTML,
			"Md2HTML":              utils.Md2Html,
			"Pager2HTML":           utils.Pager2HTML,
			"Pager2Simple":         utils.Pager2HTMLSimple,
			"TimeUnixFormat":       utils.TimeUnixFormat,
			"TimeUnixFormatFriend": utils.FriendTimeUnixFormat,
			"FriendBytesSize":      utils.FriendBytesSize,
			"Nl2Br":                utils.Nl2Br,
		},
	}))
}
Ejemplo n.º 3
0
func Run() {

	t = tango.New()

	t.Use(tango.Static(tango.StaticOptions{
		RootPath: utils.Abs(settings.Static.VirtualRoot),
	}))
	t.Use(binding.Bind())
	t.Use(new(time.TimeHandler))
	t.Use(tango.ClassicHandlers...)
	t.Use(renders.New(renders.Options{
		Reload:      true,
		Directory:   utils.Abs(settings.Template.Home),
		Charset:     settings.Template.Charset,
		DelimsLeft:  settings.Template.DelimesLeft,
		DelimsRight: settings.Template.DelimesRight,
		Funcs:       utils.DefaultFuncs(),
	}))
	t.Use(events.Events())
	t.Group("/pair", func(g *tango.Group) {
		g.Get("/all", new(pairs.GetAllRouter))
		g.Post("/new", new(pairs.NewPairRouter))
	})
	t.Get("/", new(page.HomeRouter))

	t.Run(settings.Server.Port)
}
Ejemplo n.º 4
0
func InitTango(isDebug bool) *tango.Tango {
	t := tango.New()
	if isDebug {
		t.Use(debug.Debug(debug.Options{
			HideResponseBody: true,
			IgnorePrefix:     "/static",
		}))
	}
	t.Use(tango.ClassicHandlers...)
	sess := session.New(session.Options{
		MaxAge: sessionTimeout,
	})
	t.Use(
		binding.Bind(),
		tango.Static(tango.StaticOptions{
			RootPath: filepath.Join(*homeDir, "static"),
			Prefix:   "static",
		}),
		renders.New(renders.Options{
			Reload:    true,
			Directory: filepath.Join(*homeDir, "templates"),
			Funcs: template.FuncMap{
				"isempty": func(s string) bool {
					return len(s) == 0
				},
				"add": func(a, b int) int {
					return a + b
				},
				"isNil": isNil,
				"i18n":  i18n.Tr,
				"Range": func(size int) []struct{} {
					return make([]struct{}, size)
				},
				"multi": func(a, b int) int {
					return a * b
				},
			},
			Vars: renders.T{
				"GoVer":    strings.Trim(runtime.Version(), "go"),
				"TangoVer": tango.Version(),
				"XormVer":  xorm.Version,
				"NodbVer":  nodb.Version,
			},
		}),
		middlewares.Auth("/login", sess),
		flash.Flashes(sess),
		sess,
	)

	t.Any("/", new(actions.Home))
	t.Any("/login", new(actions.Login))
	t.Any("/logout", new(actions.Logout))
	t.Any("/addb", new(actions.Addb))
	t.Any("/view", new(actions.View))
	t.Any("/del", new(actions.Del))
	t.Any("/delRecord", new(actions.DelRecord))
	t.Any("/chgpass", new(actions.ChgPass))
	t.Get("/test", new(actions.Test))
	return t
}
Ejemplo n.º 5
0
Archivo: we.go Proyecto: trigrass2/wego
func initTango(isprod bool) *tango.Tango {
	middlewares.Init()

	tg := tango.NewWithLog(setting.Log)

	if false {
		//if !isprod {
		tg.Use(debug.Debug(debug.Options{
			IgnorePrefix:     "/static",
			HideResponseBody: true,
			HideRequestBody:  true,
		}))
	}

	tg.Use(tango.ClassicHandlers...)

	sess := session.New(session.Options{
		MaxAge: time.Duration(setting.SessionCookieLifeTime),
	})
	tg.Use(
		tango.Static(tango.StaticOptions{
			RootPath: "./static",
			Prefix:   "static",
		}),
		tango.Static(tango.StaticOptions{
			RootPath: "./static_source",
			Prefix:   "static_source",
		}),
		sess,
		middlewares.Renders,
		setting.Captcha,
	)
	tg.Get("/favicon.ico", func(ctx *tango.Context) {
		ctx.ServeFile("./static/favicon.ico")
	})
	if setting.EnableXSRF {
		tg.Use(xsrf.New(time.Duration(setting.SessionCookieLifeTime)))
	}
	tg.Use(flash.Flashes(sess), events.Events())
	return tg
}
Ejemplo n.º 6
0
func Run() {
	// t := tango.Classic()
	router := vars.Server // gin.Default()

	//    router.GET("/home", func(c *gin.Context) {//测试,获取数据表信息
	//        log.Println("visit homepage")
	//        c.String(http.StatusOK, "Welcome to RenPro...")
	//    })
	//  router.Static("/index", "static")
	var filterExts []string
	router.Use(tango.Static(tango.StaticOptions{
		RootPath:   "./static",
		ListDir:    false,
		FilterExts: filterExts,
	}))

	router.Post("/login", new(Login))

	// router.GET("/tenant/:id",new(Tenant).Get)

	//    router.GET("/tenant",new(Tenant).Get)
	//    router.GET("/rentdetail",new(RentDetail).Get)
	//    router.GET("/utilitybill",new(UtilityBill).Get)

	//   router.POST("/loginJSON",new(Login).LoginJSON)
	//    router.GET("/getbill", GetBill)//测试,获取昨天数据表信息

	//获取服务器时间
	//    router.GET("/time", func(c *gin.Context) {
	//        c.String(http.StatusOK,time.Now().Format("2006-01-02 15:04:05"))
	//    })

	//    router.POST("/upload",uploaddata)//批量上传数据
	//    router.POST("/uploadone",uploaddataone)//上传单条数据
	port := "8088"
	if len(os.Args) > 1 {
		port = os.Args[1]
	}
	// port := os.Args[1]
	// gin.SetMode(gin.DebugMode)
	router.Run(":" + port)
}
Ejemplo n.º 7
0
func main() {
	err := conf.Init()
	if err != nil {
		fmt.Println(err)
		return
	}

	err = models.Init()
	if err != nil {
		fmt.Println(err)
		return
	}

	t := tango.Classic()
	t.Use(
		tango.Static(tango.StaticOptions{
			RootPath: conf.BooksRootPath,
			Prefix:   "read",
		}),
		renders.New(renders.Options{
			Reload:    true,
			Directory: "templates",
			//Extensions: []string{".html"},
			Vars: renders.T{
				"AppUrl":  "/",
				"AppLogo": "/",
			},
		}),
	)

	t.Get("/", new(routers.Home))
	t.Any("/github", new(routers.GithubPush))
	//t.Get("/github/auth", new(routers.Github))
	//t.Get("/github/callback", new(routers.GithubCallback))

	t.Run(conf.Listen)
}
Ejemplo n.º 8
0
Archivo: web.go Proyecto: goftp/ftpd
func Web(listen, static, templates, admin, pass string, tls bool, certFile, keyFile string) {
	_, err := DB.GetUser(admin)
	if err != nil {
		if err == leveldb.ErrNotFound {
			err = DB.AddUser(admin, pass)
		}
	}
	if err != nil {
		panic(err)
	}
	adminUser = admin

	t := tango.Classic()
	sess := session.New(session.Options{
		MaxAge: timeout,
	})
	t.Use(
		tango.Static(tango.StaticOptions{
			RootPath: static,
		}),
		renders.New(renders.Options{
			Reload:    true, // if reload when template is changed
			Directory: templates,
		}),
		sess,
		auth(),
		binding.Bind(),
		xsrf.New(timeout),
		flash.Flashes(sess),
	)

	t.Get("/", new(MainAction))
	t.Any("/login", new(LoginAction))
	t.Get("/logout", new(LogoutAction))
	t.Get("/down", new(DownAction))
	t.Group("/user", func(g *tango.Group) {
		g.Get("/", new(UserAction))
		g.Any("/chgpass", new(ChgPassAction))
		g.Any("/add", new(UserAddAction))
		g.Any("/edit", new(UserEditAction))
		g.Any("/del", new(UserDelAction))
	})

	t.Group("/group", func(g *tango.Group) {
		g.Get("/", new(GroupAction))
		g.Get("/add", new(GroupAddAction))
		g.Get("/edit", new(GroupEditAction))
		g.Get("/del", new(GroupDelAction))
	})
	t.Group("/perm", func(g *tango.Group) {
		g.Get("/", new(PermAction))
		g.Any("/add", new(PermAddAction))
		g.Any("/edit", new(PermEditAction))
		g.Any("/del", new(PermDelAction))
		g.Any("/updateOwner", new(PermUpdateOwner))
		g.Any("/updateGroup", new(PermUpdateGroup))
		g.Any("/updatePerm", new(PermUpdatePerm))
	})

	if tls {
		t.RunTLS(certFile, keyFile, listen)
		return
	}

	t.Run(listen)
}
Ejemplo n.º 9
0
func (is *BootstrapService) Init(v interface{}) (*Result, error) {
	opt, ok := v.(BootstrapInitOption)
	if !ok {
		return nil, ErrServiceFuncNeedType(is.Init, opt)
	}
	var err error
	if opt.Config {
		core.Cfg = core.NewConfig()
		if err = core.Cfg.Sync(core.ConfigFile); err != nil {
			return nil, err
		}
	}
	if core.Cfg != nil && opt.Database { // database depends on config
		log.SetLevelByString("error")
		tidb.Debug = false // close tidb debug
		core.Db, err = xorm.NewEngine(core.Cfg.Db.Driver, core.Cfg.Db.DSN)
		if err != nil {
			return nil, err
		}
		core.Db.SetLogger(nil)
		// core.Db.ShowDebug = true
		// core.Db.ShowSQL = true
	}
	if core.Cfg != nil && opt.Server { // server depends on config
		core.Server = tango.New([]tango.Handler{
			tango.Return(),
			tango.Param(),
			tango.Contexts(),
		}...)
		core.Server.Use(tango.Static(tango.StaticOptions{
			RootPath: core.StaticDirectory,
			Prefix:   core.StaticPrefix,
		}))
		core.Server.Use(tango.Static(tango.StaticOptions{
			RootPath: core.ThemeDirectory,
			Prefix:   core.ThemePrefix,
		}))
		core.Server.Use(renders.New(renders.Options{
			Reload:     true,
			Directory:  core.ThemeDirectory,
			Extensions: []string{".tmpl"},
			Funcs: template.FuncMap{
				"TimeUnixFormat":  utils.TimeUnixFormat,
				"TimeUnixFriend":  utils.TimeUnixFriend,
				"Mardown2Str":     utils.Markdown2String,
				"Markdown2HTML":   utils.Markdown2HTML,
				"Nl2BrHTML":       utils.Nl2Br,
				"Nl2BrString":     utils.Nl2BrString,
				"BytesSizeFriend": utils.FriendBytesSize,
			},
		}))
		sessions := session.New(session.Options{
			SessionIdName: core.SessionName,
		})
		core.Server.Use(xsrf.New(time.Hour))
		core.Server.Use(binding.Bind())
		core.Server.Use(sessions)
		core.Server.Use(flash.Flashes(sessions))
	}
	return nil, nil
}