8000 GitHub - poy/go-router: A simple HTTP router for Go
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

poy/go-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-router

A simple HTTP router for Go

Motivation

I wanted a way to create HTTP routes in a codebase using https://github.com/poy/go-dependency-injection

Example

package main

import (
	"context"
	"net/http"

	"github.com/poy/go-dependency-injection/pkg/injection"
	"github.com/poy/go-router/pkg/router"

	// Register the STDOUT logger.
	_ "github.com/poy/go-router/pkg/observability/cli"
)

func main() {
	ctx := injection.WithInjection(context.Background())
	router := injection.Resolve[router.Router](ctx)
	if err := http.ListenAndServe("localhost:8080", router); err != nil {
		panic(err)
	}
}

func init() {
	injection.Register[injection.Group[router.Route]](func(ctx context.Context) injection.Group[router.Route] {
		return injection.AddToGroup[router.Route](ctx, router.Route{
			Method:      http.MethodGet,
			Path:        "/apis/v1alpha1/status",
			Description: `Status endpoint`,
			Handler:     &handler{},
		})
	})
}

type handler struct {
}

func (h *handler) ServeHTTP(_ http.ResponseWriter, _ *http.Request) {
	// NOP. We just want to return a 200.
}

About

A simple HTTP router for Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0