package nf import ( "fmt" "net/http" "strings" ) type App struct { *RouterGroup router *router groups []*RouterGroup } func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) { c := newContext(writer, request) for _, group := range a.groups { if strings.HasPrefix(request.URL.Path, group.prefix) { c.handlers = append(c.handlers, group.middlewares...) } } if err := a.router.handle(c); err != nil { writer.WriteHeader(500) _, _ = writer.Write([]byte(err.Error())) } } func (a *App) Get(path string, handlers ...HandlerFunc) { verifyHandlers(path, handlers...) a.router.addRoute(http.MethodGet, path, handlers...) } func (a *App) Post(path string, handlers ...HandlerFunc) { verifyHandlers(path, handlers...) a.router.addRoute(http.MethodPost, path, handlers...) } func (a *App) Delete(path string, handlers ...HandlerFunc) { verifyHandlers(path, handlers...) a.router.addRoute(http.MethodDelete, path, handlers...) } func (a *App) Put(path string, handlers ...HandlerFunc) { verifyHandlers(path, handlers...) a.router.addRoute(http.MethodPut, path, handlers...) } func (a *App) Patch(path string, handlers ...HandlerFunc) { verifyHandlers(path, handlers...) a.router.addRoute(http.MethodPatch, path, handlers...) } func (a *App) Head(path string, handlers ...HandlerFunc) { verifyHandlers(path, handlers...) a.router.addRoute(http.MethodHead, path, handlers...) } func (a *App) Run(address string) error { fmt.Println(banner + "nf serve at: " + address + "\n") return http.ListenAndServe(address, a) }