142 lines
3.4 KiB
Go
Raw Normal View History

2023-07-18 19:48:38 +00:00
package server
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
}
// New creates a new HTTP server.
func New() Server {
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 11:52:03 +00:00
handler := c.(*ctx).server.router.LookupNoAlloc(c.Method(), c.Path(), c.(*ctx).addParameter)
2024-03-13 19:18:01 +00:00
if handler == nil {
return c.Status(http.StatusNotFound).String(http.StatusText(http.StatusNotFound))
}
return handler(c)
},
},
2024-03-14 11:52:03 +00:00
errorHandler: func(ctx Context, err error) {
ctx.WriteString(err.Error())
log.Println(ctx.Path(), err)
},
}
s.pool.New = func() any {
return &ctx{server: s}
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-14 11:52:03 +00:00
func (s *server) ServeHTTP(response http.ResponseWriter, request *http.Request) {
ctx := s.pool.Get().(*ctx)
2024-03-13 23:06:20 +00:00
ctx.request = request
ctx.response = response
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
ctx.paramCount = 0
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
}