Esempio n. 1
0
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
}
Esempio n. 2
0
func main() {
	e := echo.New()
	e.Use(mw.Log(), mw.Recover())
	e.Use(markdown.Markdown(&markdown.Options{
		Path:   "/book/",
		Root:   filepath.Join(os.Getenv(`GOPATH`), `src`, `github.com/admpub/gopl-zh`),
		Browse: true,
	}))

	e.Get("/", echo.HandlerFunc(func(c echo.Context) error {
		return c.String(200, "Hello, World!")
	}))

	// FastHTTP
	// e.Run(fasthttp.New(":4444"))

	// Standard
	e.Run(standard.New(":4444"))
}
Esempio n. 3
0
func main() {
	engine := "fasthttp"

	e := echo.New()
	e.SetDebug(true)
	// ==========================
	// 添加中间价
	// ==========================
	e.Use(func(h echo.Handler) echo.HandlerFunc {
		return func(c echo.Context) error {
			fmt.Println(`==========before===========`)
			err := h.Handle(c)
			fmt.Println(`===========after===========`)
			fmt.Println(`===========response content:`)
			fmt.Println(string(c.Response().Body()))
			return err
		}
	})

	e.Use(mw.Log())
	e.Use(mw.Recover())
	e.Use(mw.Gzip())

	// ==========================
	// 设置路由
	// ==========================
	e.Get("/", func(c echo.Context) error {
		return c.String(200, "Hello, World!\n"+fmt.Sprintf("%+v", c.Request().Form().All()))
	})
	e.Post("/", func(c echo.Context) error {
		return c.String(200, "Hello, World!\n"+fmt.Sprintf("%+v", c.Request().Form().All()))
	})
	e.Post("/bind", func(c echo.Context) error {
		m := &FormData{}
		c.Bind(m)
		return c.String(200, "Bind data:\n"+fmt.Sprintf("%+v", m))
	})
	e.Get("/v2", func(c echo.Context) error {
		return c.String(200, "Echo v2")
	})
	e.Get("/ping", func(c echo.Context) error {
		return c.String(200, "pong")
	})
	e.Get("/std", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`standard net/http handleFunc`))
		w.WriteHeader(200)
	})

	// ==========================
	// 创建子路由
	// ==========================
	g := e.Group("/admin")
	g.Get("", func(c echo.Context) error {
		return c.String(200, "Hello, Group!\n"+fmt.Sprintf("%+v", c.Request().Form().All()))
	})
	g.Post("", func(c echo.Context) error {
		return c.String(200, "Hello, Group!\n"+fmt.Sprintf("%+v", c.Request().Form().All()))
	})
	g.Get("/ping", func(c echo.Context) error {
		return c.String(200, "pong -- Group")
	})

	// ==========================
	// 启动服务
	// ==========================
	switch engine {

	case "fasthttp":
		// FastHTTP
		e.Run(fasthttp.New(":4444"))

	default:
		// Standard
		e.Run(standard.New(":4444"))

	}

}