e := echo.New() ⬇️
func New() (e *Echo) { e = &Echo{ Server: new(http.Server), TLSServer: new(http.Server), AutoTLSManager: autocert.Manager{ Prompt: autocert.AcceptTOS, }, Logger: log.New("echo"), colorer: color.New(), maxParam: new(int), } e.Server.Handler = e e.TLSServer.Handler = e e.HTTPErrorHandler = e.DefaultHTTPErrorHandler e.Binder = &DefaultBinder{} e.Logger.SetLevel(log.ERROR) e.StdLogger = stdLog.New(e.Logger.Output(), e.Logger.Prefix()+": ", 0) e.pool.New = func() interface{} { return e.NewContext(nil, nil) } e.router = NewRouter(e) e.routers = map[string]*Router{} return } ⬇️ func NewRouter(e *Echo) *Router { return &Router{ tree: &node{ methodHandler: new(methodHandler), }, routes: map[string]*Route{}, echo: e, } }
Router struct { tree *node routes map[string]*Route echo *Echo }
node struct { kind kind label byte prefix string parent *node children children ppath string pnames []string methodHandler *methodHandler } kind uint8
children []*node
methodHandler struct { connect HandlerFunc delete HandlerFunc get HandlerFunc head HandlerFunc options HandlerFunc patch HandlerFunc post HandlerFunc propfind HandlerFunc put HandlerFunc trace HandlerFunc report HandlerFunc } HandlerFunc func(Context) error
Route struct { Method string `json:"method"` Path string `json:"path"` Name string `json:"name"` }
e.GET("/", hello) ⬇️
func (e *Echo) GET(path string, h HandlerFunc, m ...MiddlewareFunc) *Route { return e.Add(http.MethodGet, path, h, m...) } ️️⬇️ func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route { return e.add("", method, path, handler, middleware...) } ⬇️ func (e *Echo) add(host, method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route { name := handlerName(handler) router := e.findRouter(host) router.Add(method, path, func(c Context) error { h := handler for i := len(middleware) - 1; i >= 0; i-- { h = middleware[i](h) } return h(c) }) r := &Route{ Method: method, Path: path, Name: name, } e.router.routes[method+path] = r return r }
e.Start(":1323")
func (e *Echo) Start(address string) error { e.Server.Addr = address return e.StartServer(e.Server) } ⬇️ func (e *Echo) StartServer(s *http.Server) (err error) { e.colorer.SetOutput(e.Logger.Output()) s.ErrorLog = e.StdLogger s.Handler = e if e.Debug { e.Logger.SetLevel(log.DEBUG) }
if !e.HideBanner { e.colorer.Printf(banner, e.colorer.Red("v"+Version), e.colorer.Blue(website)) }
if s.TLSConfig == nil { if e.Listener == nil { e.Listener, err = newListener(s.Addr) if err != nil { return err } } if !e.HidePort { e.colorer.Printf("⇨ http server started on %s\n", e.colorer.Green(e.Listener.Addr())) } return s.Serve(e.Listener) } if e.TLSListener == nil { l, err := newListener(s.Addr) if err != nil { return err } e.TLSListener = tls.NewListener(l, s.TLSConfig) } if !e.HidePort { e.colorer.Printf("⇨ https server started on %s\n", e.colorer.Green(e.TLSListener.Addr())) } return s.Serve(e.TLSListener) } ⬇️ s.Serve() ⬇️
rw, e := l.Accept() ⬇️
go c.serve(ctx) ⬇️
serverHandler{c.server}.ServeHTTP(w, w.req) ⬇️
handler.ServeHTTP(rw, req) ⬇️ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) { c := e.pool.Get().(*context) c.Reset(r, w)
h := NotFoundHandler
if e.premiddleware == nil { e.findRouter(r.Host).Find(r.Method, getPath(r), c) h = c.Handler() h = applyMiddleware(h, e.middleware...) } else { h = func(c Context) error { e.findRouter(r.Host).Find(r.Method, getPath(r), c) h := c.Handler() h = applyMiddleware(h, e.middleware...) return h(c) } h = applyMiddleware(h, e.premiddleware...) }
if err := h(c); err != nil { e.HTTPErrorHandler(err, c) }
e.pool.Put(c) }
func applyMiddleware(h HandlerFunc, middleware ...MiddlewareFunc) HandlerFunc { for i := len(middleware) - 1; i >= 0; i-- { h = middleware[i](h) } return h }
e.Logger.Fatal()
|