Example #1
0
// 运行服务
func (s *Server) Run(args ...interface{}) {
	var eng engine.Engine
	var arg interface{}
	if len(args) > 0 {
		arg = args[0]
	}
	switch arg.(type) {
	case string:
		eng = standard.New(arg.(string))
	case engine.Engine:
		eng = args[0].(engine.Engine)
	default:
		eng = standard.New(`:80`)
	}
	defer func() {
		events.GoEvent(`webx.serverExit`, nil, func(_ bool) {})
	}()
	s.Core.Logger().Infof(`Server "%v" has been launched.`, s.Name)

	eng.SetHandler(s.ServeHTTP)
	eng.SetLogger(s.Core.Logger())
	eng.Start()

	s.Core.Logger().Infof(`Server "%v" has been closed.`, s.Name)
}
Example #2
0
func main() {
	e := echo.New()

	// Create a limiter struct.
	limiter := tollbooth.NewLimiter(1, time.Second)

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

	e.Run(standard.New(":4444"))
}
Example #3
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"))
}
Example #4
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"))

	}

}