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
|
|
|
"io"
|
|
|
|
"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"
|
|
|
|
"syscall"
|
2023-07-18 16:02:57 +00:00
|
|
|
|
|
|
|
"git.akyoto.dev/go/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server represents a single web service.
|
|
|
|
type Server struct {
|
2024-03-13 19:18:01 +00:00
|
|
|
Router *router.Router[Handler]
|
|
|
|
Config Configuration
|
|
|
|
handlers []Handler
|
2023-07-18 16:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new server.
|
|
|
|
func New() *Server {
|
|
|
|
return &Server{
|
2024-03-12 21:31:45 +00:00
|
|
|
Router: router.New[Handler](),
|
|
|
|
Config: defaultConfig(),
|
2024-03-13 19:18:01 +00:00
|
|
|
handlers: []Handler{
|
|
|
|
func(c Context) error {
|
|
|
|
handler := c.(*ctx).server.Router.LookupNoAlloc(c.Method(), c.Path(), c.(*ctx).addParameter)
|
|
|
|
|
|
|
|
if handler == nil {
|
|
|
|
return c.Status(http.StatusNotFound).String(http.StatusText(http.StatusNotFound))
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler(c)
|
|
|
|
},
|
|
|
|
},
|
2023-07-18 16:02:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get registers your function to be called when the given GET path has been requested.
|
|
|
|
func (server *Server) Get(path string, handler Handler) {
|
2024-03-12 21:31:45 +00:00
|
|
|
server.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.
|
|
|
|
func (server *Server) Post(path string, handler Handler) {
|
2024-03-12 21:31:45 +00:00
|
|
|
server.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.
|
|
|
|
func (server *Server) Delete(path string, handler Handler) {
|
2024-03-12 21:31:45 +00:00
|
|
|
server.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.
|
|
|
|
func (server *Server) Put(path string, handler Handler) {
|
2024-03-12 21:31:45 +00:00
|
|
|
server.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.
|
|
|
|
func (server *Server) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
2024-03-13 23:06:20 +00:00
|
|
|
ctx := contextPool.Get().(*ctx)
|
|
|
|
ctx.request = request
|
|
|
|
ctx.response = response
|
|
|
|
ctx.server = server
|
2024-03-13 19:18:01 +00:00
|
|
|
err := server.handlers[0](ctx)
|
2023-07-18 16:02:57 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-07-21 21:23:49 +00:00
|
|
|
response.(io.StringWriter).WriteString(err.Error())
|
|
|
|
log.Println(request.URL, err)
|
2023-07-18 16:02:57 +00:00
|
|
|
}
|
2024-03-13 23:06:20 +00:00
|
|
|
|
|
|
|
ctx.paramCount = 0
|
|
|
|
ctx.handlerCount = 0
|
|
|
|
contextPool.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-13 15:57:36 +00:00
|
|
|
func (server *Server) Run(address string) error {
|
2024-03-12 21:31:45 +00:00
|
|
|
srv := &http.Server{
|
|
|
|
Addr: address,
|
|
|
|
Handler: server,
|
|
|
|
ReadTimeout: server.Config.Timeout.Read,
|
|
|
|
WriteTimeout: server.Config.Timeout.Write,
|
|
|
|
IdleTimeout: server.Config.Timeout.Idle,
|
|
|
|
ReadHeaderTimeout: server.Config.Timeout.ReadHeader,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), server.Config.Timeout.Shutdown)
|
|
|
|
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
|
|
|
|
|
|
|
// Use adds handlers to your handlers chain.
|
|
|
|
func (server *Server) Use(handlers ...Handler) {
|
|
|
|
last := server.handlers[len(server.handlers)-1]
|
|
|
|
server.handlers = append(server.handlers[:len(server.handlers)-1], handlers...)
|
|
|
|
server.handlers = append(server.handlers, last)
|
|
|
|
}
|