Go servers with a single function.
go get -u github.com/A2Y-D5L/serve
package main
import (
"context"
"log/slog"
"net/http"
"github.com/A2Y-D5L/serve/httpserver"
)
func handleGETRoot(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}
func main() {
if err := httpserver.Serve(
context.Background(),
httpserver.Routes(
httpserver.Route{
Pattern: "GET /",
Handler: http.HandlerFunc(handleGETRoot),
},
),
); err != nil {
slog.Error("httpserver.Serve error:" + err)
}
}
The httpserver
package exposes several functional options for common server
configurations. If you need further customization, you can
define your own options. For example:
package main
import (
"context"
"log/slog"
"net/http"
"github.com/A2Y-D5L/serve/httpserver"
)
func DisableGeneralOptionsHandler() httpserver.Option {
return func(srv *http.Server) {
srv.DisableGeneralOptionsHandler = true
}
}
func main() {
if err := httpserver.Serve(
context.Background(),
httpserver.Address(":8080"), // provided by the httpserver package
DisableGeneralOptionsHandler(), // custom option
); err != nil {
slog.Error("httpserver.Serve error:" + err)
}
}
This project is licensed under the MIT License - see the LICENSE file for details.