99 lines
2.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
"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-12 21:31:45 +00:00
Router *router.Router[Handler]
Config Configuration
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(),
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) {
ctx := newContext(request, response)
2024-03-12 19:35:19 +00:00
defer contextPool.Put(ctx)
2024-03-12 21:31:45 +00:00
handler := server.Router.LookupNoAlloc(request.Method, request.URL.Path, ctx.addParameter)
2023-07-18 16:02:57 +00:00
if handler == nil {
response.WriteHeader(http.StatusNotFound)
2023-07-21 21:23:49 +00:00
response.(io.StringWriter).WriteString(http.StatusText(http.StatusNotFound))
2023-07-18 16:02:57 +00:00
return
}
err := handler(ctx)
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-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
}