nf/app.go

30 lines
600 B
Go
Raw Normal View History

2024-01-10 20:26:19 +08:00
package nf
import (
"fmt"
"net/http"
)
type App struct {
router *router
server *http.Server
logger Logger
}
func (a *App) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
c := newCtx(a, writer, request)
if err := a.router.handle(c); err != nil {
writer.WriteHeader(500)
_, _ = writer.Write([]byte(err.Error()))
}
}
func (a *App) Get(path string, handlerFunc HandlerFunc) {
a.router.addRoute(http.MethodGet, path, handlerFunc)
}
func (a *App) Run(address string) error {
fmt.Println(banner + "nf serve at: " + address + "\n")
return http.ListenAndServe(address, a)
}