예제 #1
0
파일: app.go 프로젝트: webx-top/webx
func NewApp(name string, domain string, s *Server, middlewares ...echo.Middleware) (a *App) {
	a = &App{
		Server:      s,
		Name:        name,
		Domain:      domain,
		controllers: make(map[string]*Wrapper),
	}
	if s.TemplateEngine != nil {
		a.Renderer = s.TemplateEngine
	}
	if a.Domain == "" {
		var prefix string
		if name != "" {
			prefix = `/` + name
			a.Dir = prefix + `/`
		} else {
			a.Dir = `/`
		}
		a.Url = a.Dir
		if s.Url != `/` {
			a.Url = strings.TrimSuffix(s.Url, `/`) + a.Url
		}
		a.Group = s.Core.Group(prefix)
		a.Group.Use(middlewares...)
	} else {
		e := echo.NewWithContext(s.InitContext)
		e.Use(s.DefaultMiddlewares...)
		e.Use(middlewares...)
		a.Handler = e
		a.Url = `http://` + a.Domain + `/`
		a.Dir = `/`
	}
	return
}
예제 #2
0
파일: server.go 프로젝트: webx-top/webx
func NewServer(name string, middlewares ...echo.Middleware) (s *Server) {
	s = &Server{
		Name:               name,
		Apps:               make(map[string]*App),
		apps:               make(map[string]*App),
		DefaultMiddlewares: []echo.Middleware{webxHeader(), mw.Log(), mw.Recover()},
		TemplateDir:        `template`,
		Url:                `/`,
		MaxUploadSize:      10 * 1024 * 1024,
		CookiePrefix:       "webx_" + name + "_",
		CookieHttpOnly:     true,
	}
	s.InitContext = func(e *echo.Echo) interface{} {
		return NewContext(s, echo.NewContext(nil, nil, e))
	}

	s.CookieAuthKey = string(codec.GenerateRandomKey(32))
	s.CookieBlockKey = string(codec.GenerateRandomKey(32))
	s.SessionStoreEngine = `cookie`
	s.SessionStoreConfig = s.CookieAuthKey
	s.Codec = codec.New([]byte(s.CookieAuthKey), []byte(s.CookieBlockKey))
	s.Core = echo.NewWithContext(s.InitContext)
	s.URL = NewURL(name, s)
	s.Core.Use(s.DefaultMiddlewares...)
	s.Core.Use(middlewares...)
	servs.Set(name, s)
	return
}
예제 #3
0
파일: main.go 프로젝트: webx-top/webx
func main() {
	mode := flag.String("m", "clean2", "port of your app.")
	port := flag.String("p", "8080", "port of your app.")
	flag.Parse()

	var lang = language.NewLanguage()
	lang.Set(`zh-cn`, true, true)
	lang.Set(`en`, true)

	Cfg.HtmlCacheRules[`index:`] = []interface{}{
		`index`, /*/保存名称
		func(tmpl string, c echo.Context) string { //自定义保存名称
			return tmpl
		},
		func(tmpl string, c echo.Context) (mtime int64,expired bool) { //判断缓存是否过期
			return
		},*/
	}
	Cfg.HtmlCacheRules[`test:`] = []interface{}{
		`test`,
	}
	var s *X.Server
	if *mode == `clean` {
		// ===============================================================
		// benchmark测试(不使用任何中间件,特别是log中间件,比较影响速度)
		// ===============================================================
		s = X.Serv()
		//s.ResetTmpl().Pprof().Debug(true)
		s.DefaultMiddlewares = []echo.Middleware{}
		s.Core = echo.NewWithContext(s.InitContext)
	} else {
		s = X.Serv().ResetTmpl().Pprof().Debug(true)
	}

	//==================================
	//测试多语言切换和session
	//==================================
	app := s.NewApp("", Cfg.Middleware())
	s.Core.PreUse(lang.Middleware())
	indexController = &Index{}
	//测试session
	app.R("/session", func(c *X.Context) error {
		var count int
		v := c.Session().Get("count")

		if v == nil {
			count = 0
		} else {
			count = v.(int)
			count += 1
		}

		c.Session().Set("count", count).Save()

		return c.String(http.StatusOK, fmt.Sprintf(`Hello world.Count:%v.Language: %v`, count, c.Language))
	}).
		R("/t", func(c *X.Context) error {
			return c.Render(http.StatusOK, `index`, nil)
		}, `GET`).
		//测试Before和After以及全页面html缓存
		Reg(indexController).Auto()

	//=======================================
	//测试以中间件形式实现的全页面缓存功能
	//=======================================
	s.NewApp("test", Cfg.Middleware()).
		R("", func(c *X.Context) error {
			return c.Display(`index2`)
		}, `GET`)

	//=======================================
	//测试无任何中间件时是否正常
	//=======================================
	s.NewApp("ping").R("", func(c *X.Context) error {
		return c.String(200, "pong")
	})

	s.Run(":" + *port)
}