Documentation ¶
Index ¶
- func HealthCheckHandler() http.Handler
- func NewServer(addr string, handler http.Handler) *http.Server
- func Run(ctx context.Context, server *http.Server) error
- func RunServer(ctx context.Context, in RunInput) error
- type Router
- func (r *Router) Handle(method, path string, h http.Handler)
- func (r *Router) HandleFunc(method, path string, h func(w http.ResponseWriter, r *http.Request))
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Sub(path string, fn func(*Router))
- func (r *Router) SubRouter(path string) *Router
- func (r *Router) Use(m ...func(http.Handler) http.Handler)
- type RunInput
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HealthCheckHandler ¶ added in v0.2.0
HealthCheckHandler returns an http.Handler that handles health check requests. It sets the Content-Type header to "text/plain; charset=utf-8" and writes "OK" as the response body.
func NewServer ¶ added in v0.2.0
NewServer creates a new HTTP server with the specified address and handler. The server has a read timeout of 60 seconds, a write timeout of 60 seconds, and an idle timeout of 5 minutes.
func Run ¶
Run runs the HTTP server using the provided context and server configuration. It returns an error if the server fails to start or encounters an error during shutdown.
func RunServer ¶ added in v0.2.0
RunServer runs the server using the provided context and input parameters. It starts a goroutine to execute the Run method of the input object, which accepts the passed server. It waits for the server to complete or for the context to be canceled. If the context is canceled, it stops the server using the Stop method of the input object. Returns an error if the server fails to start or if the context is canceled.
Types ¶
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Example ¶
r := router.New() // Register the HealthCheckHandler to the "/health-check" route, before the middleware. // This will remove the middleware overhead from the HealthCheck endpoint r.Handle(http.MethodGet, "/health-check", router.HealthCheckHandler()) r.Use(middleware.RequestID(), middleware.Logger(slog.LevelInfo)) // All routes will be prefixed with "/api" r.Sub("/api", func(sub *router.Router) { sub.Use(middleware.NoCache()) // Full path /api/posts/:id sub.HandleFunc("GET", "/posts/:id", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("id:" + r.PathValue("id"))) }) }) s := router.NewServer(":3000", r) go func() { <-time.After(100 * time.Millisecond) s.Shutdown(context.Background()) }() err := router.Run(context.Background(), s) fmt.Println(err)
Output: http: Server closed