Skip to content
/ serve Public

Run prod-ready Go servers with a single function call: httpserver.Serve(ctx, routes, opts…)

License

Notifications You must be signed in to change notification settings

A2Y-D5L/serve

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go servers with a single function.

Go Reference Go Report Card GitHub go.mod Go version License

Install

go get -u github.com/A2Y-D5L/serve

Usage

HTTP Server

Run an HTTP server

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)
    }
}

Configuring the HTTP Server

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)
    }
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Run prod-ready Go servers with a single function call: httpserver.Serve(ctx, routes, opts…)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages