Lightweight Go HTTP router and small server helper.
yagaw provides a minimal routing layer and a Server wrapper built on Go's standard net/http. It focuses on simple route registration, method-based dispatch, and basic parameterized route matching using a {name} syntax.
- Register routes per HTTP method:
GET,POST,PUT,DELETE,PATCH, etc. - Parameterized paths such as
/users/{id}(supports alphanumeric, hyphen and underscore). Serverhelper to run anhttp.Serverbacked by theRouter.- Small dependency: uses
github.com/Pho3b/tiny-loggerfor logging.
yagaw.NewServer(addr string, port int) *Server— create a new server.(*Server).Run()— start the HTTP server (blocking).(*Server).GetRouter() *Router— access the router to register routes.(*Router).RegisterRoute(method HttpRequestMethod, path string, handler RequestHandler)— register a route.(*Router).RegisteredRoutes() *RequestHandlerMap— inspect registered routes.
- Exact path matches are attempted first. If not found, parameterized route patterns (converted into regex at registration time) are tried.
- Parameter patterns are defined with
{name}and are converted to([a-z0-9-_]+)when registered. The router does not automatically inject parameter values into thehttp.Request— handlers can extract values fromreq.URL.Pathusing string-splitting or regex extraction. - Unmatched requests return a plain
404 - Page not foundresponse.
package main
import (
"net/http"
"strings"
"github.com/Algatux/yagaw"
"github.com/Pho3b/tiny-logger/logs/log_level"
)
func main() {
// Optional: configure logger level
yagaw.Log = yagaw.InitLogger(log_level.DebugLvlName)
s := yagaw.NewServer("localhost", 8080)
r := s.GetRouter()
r.RegisterRoute(yagaw.GET, "/hello", func(req *http.Request, params yagaw.Params) *yagaw.HttpResponse {
yagaw.Log.Debug("Hello, yagaw!")
return yagaw.NewHttpResponse(200).
SetHeader("Content-Type", "text/plain")
})
// Parameterized route example — extract parameter manually inside handler
r.RegisterRoute(yagaw.GET, "/users/{id}", func(req *http.Request, params yagaw.Params) *yagaw.HttpResponse {
// simple extraction: split path ("/users/123" -> ["","users","123"])
parts := strings.Split(req.URL.Path, "/")
if len(parts) >= 3 {
id := parts[2]
yagaw.Log.Debug("Received user id:", id)
return yagaw.NewHttpResponse(200)
}
return yagaw.NewHttpResponse(404)
})
s.Run()
}Run unit tests and benchmarks with:
go test ./...
go test -bench=.server.go—Serverwrapper andInitLoggerhelper.router.go— route registration and pattern matching implementation.router_test.go— tests and benchmarks for the router behavior.
Issues and pull requests are welcome. Please run the test suite before submitting changes.
See the LICENSE file in this repository.