148 lines
3.5 KiB
Go
Raw Normal View History

2024-03-22 14:08:24 +00:00
package web
2023-07-18 16:02:57 +00:00
import (
2024-03-12 21:31:45 +00:00
"context"
2023-07-21 21:23:49 +00:00
"log"
2024-03-12 21:31:45 +00:00
"net"
2023-07-18 16:02:57 +00:00
"net/http"
2024-03-12 21:31:45 +00:00
"os"
"os/signal"
2024-03-14 11:52:03 +00:00
"sync"
2024-03-12 21:31:45 +00:00
"syscall"
2023-07-18 16:02:57 +00:00
"git.akyoto.dev/go/router"
)
2024-03-14 11:52:03 +00:00
// Server is the interface for an HTTP server.
type Server interface {
http.Handler
Delete(path string, handler Handler)
Get(path string, handler Handler)
Post(path string, handler Handler)
Put(path string, handler Handler)
Router() *router.Router[Handler]
Run(address string) error
Use(handlers ...Handler)
2023-07-18 16:02:57 +00:00
}
2024-03-14 11:52:03 +00:00
// server is an HTTP server.
type server struct {
pool sync.Pool
handlers []Handler
router router.Router[Handler]
errorHandler func(Context, error)
config Configuration
}
2024-03-22 14:08:24 +00:00
// NewServer creates a new HTTP server.
func NewServer() Server {
2024-03-14 11:52:03 +00:00
s := &server{
router: router.Router[Handler]{},
config: defaultConfig(),
2024-03-13 19:18:01 +00:00
handlers: []Handler{
func(c Context) error {
2024-03-14 18:22:39 +00:00
ctx := c.(*ctx)
2024-03-16 14:22:47 +00:00
method := ctx.request.Method()
path := ctx.request.Path()
2024-03-14 18:22:39 +00:00
handler := ctx.server.router.LookupNoAlloc(method, path, ctx.addParameter)
2024-03-13 19:18:01 +00:00
if handler == nil {
2024-03-14 18:22:39 +00:00
return ctx.Status(http.StatusNotFound).String(http.StatusText(http.StatusNotFound))
2024-03-13 19:18:01 +00:00
}
return handler(c)
},
},
2024-03-14 11:52:03 +00:00
errorHandler: func(ctx Context, err error) {
2024-03-16 14:22:47 +00:00
ctx.Response().WriteString(err.Error())
log.Println(ctx.Request().Path(), err)
2024-03-14 11:52:03 +00:00
},
}
s.pool.New = func() any {
2024-03-14 14:11:00 +00:00
return &ctx{
server: s,
2024-03-15 09:06:17 +00:00
params: make([]router.Parameter, 0, 8),
2024-03-14 14:11:00 +00:00
}
2023-07-18 16:02:57 +00:00
}
2024-03-14 11:52:03 +00:00
return s
2023-07-18 16:02:57 +00:00
}
// Get registers your function to be called when the given GET path has been requested.
2024-03-14 11:52:03 +00:00
func (s *server) Get(path string, handler Handler) {
s.Router().Add(http.MethodGet, path, handler)
2023-07-22 09:36:28 +00:00
}
// Post registers your function to be called when the given POST path has been requested.
2024-03-14 11:52:03 +00:00
func (s *server) Post(path string, handler Handler) {
s.Router().Add(http.MethodPost, path, handler)
2023-07-22 09:36:28 +00:00
}
// Delete registers your function to be called when the given DELETE path has been requested.
2024-03-14 11:52:03 +00:00
func (s *server) Delete(path string, handler Handler) {
s.Router().Add(http.MethodDelete, path, handler)
2023-07-22 09:36:28 +00:00
}
// Put registers your function to be called when the given PUT path has been requested.
2024-03-14 11:52:03 +00:00
func (s *server) Put(path string, handler Handler) {
s.Router().Add(http.MethodPut, path, handler)
2024-03-12 19:35:19 +00:00
}
2023-07-18 16:02:57 +00:00
// ServeHTTP responds to the given request.
2024-03-16 14:22:47 +00:00
func (s *server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
2024-03-14 11:52:03 +00:00
ctx := s.pool.Get().(*ctx)
2024-03-16 14:22:47 +00:00
ctx.request = request{req}
ctx.response = response{res}
2024-03-14 11:52:03 +00:00
err := s.handlers[0](ctx)
2023-07-18 16:02:57 +00:00
if err != nil {
2024-03-14 11:52:03 +00:00
s.errorHandler(ctx, err)
2023-07-18 16:02:57 +00:00
}
2024-03-13 23:06:20 +00:00
2024-03-14 14:11:00 +00:00
ctx.params = ctx.params[:0]
2024-03-13 23:06:20 +00:00
ctx.handlerCount = 0
2024-03-14 11:52:03 +00:00
s.pool.Put(ctx)
2023-07-18 16:02:57 +00:00
}
2024-03-12 21:31:45 +00:00
// Run starts the server on the given address.
2024-03-14 11:52:03 +00:00
func (server *server) Run(address string) error {
2024-03-12 21:31:45 +00:00
srv := &http.Server{
Addr: address,
Handler: server,
2024-03-14 11:52:03 +00:00
ReadTimeout: server.config.Timeout.Read,
WriteTimeout: server.config.Timeout.Write,
IdleTimeout: server.config.Timeout.Idle,
ReadHeaderTimeout: server.config.Timeout.ReadHeader,
2024-03-12 21:31:45 +00:00
}
listener, err := net.Listen("tcp", address)
if err != nil {
2024-03-13 15:57:36 +00:00
return err
2024-03-12 21:31:45 +00:00
}
go srv.Serve(listener)
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
2024-03-14 11:52:03 +00:00
ctx, cancel := context.WithTimeout(context.Background(), server.config.Timeout.Shutdown)
2024-03-12 21:31:45 +00:00
defer cancel()
2024-03-13 15:57:36 +00:00
return srv.Shutdown(ctx)
2024-03-12 21:31:45 +00:00
}
2024-03-13 19:18:01 +00:00
2024-03-14 11:52:03 +00:00
// Router returns the router used by the server.
func (s *server) Router() *router.Router[Handler] {
return &s.router
}
2024-03-13 19:18:01 +00:00
// Use adds handlers to your handlers chain.
2024-03-14 11:52:03 +00:00
func (s *server) Use(handlers ...Handler) {
last := s.handlers[len(s.handlers)-1]
s.handlers = append(s.handlers[:len(s.handlers)-1], handlers...)
s.handlers = append(s.handlers, last)
2024-03-13 19:18:01 +00:00
}