30 lines
600 B
Go
30 lines
600 B
Go
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)
|
|
}
|