yagaw

package module
v0.0.0-...-bbfeeb7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 8 Imported by: 0

README

yagaw

Lightweight Go HTTP router and small server helper.

Overview

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.

Highlights

  • Register routes per HTTP method: GET, POST, PUT, DELETE, PATCH, etc.
  • Parameterized paths such as /users/{id} (supports alphanumeric, hyphen and underscore).
  • Server helper to run an http.Server backed by the Router.
  • Small dependency: uses github.com/Pho3b/tiny-logger for logging.

API Summary

  • 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.

Behavior notes

  • 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 the http.Request — handlers can extract values from req.URL.Path using string-splitting or regex extraction.
  • Unmatched requests return a plain 404 - Page not found response.

Quick example

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

Tests

Run unit tests and benchmarks with:

go test ./...
go test -bench=.

Files of interest

  • server.goServer wrapper and InitLogger helper.
  • router.go — route registration and pattern matching implementation.
  • router_test.go — tests and benchmarks for the router behavior.

Contributing

Issues and pull requests are welcome. Please run the test suite before submitting changes.

License

See the LICENSE file in this repository.

Documentation

Index

Constants

This section is empty.

Variables

Functions

func InitLogger

func InitLogger(logLevel log_level.LogLvlName) *logs.Logger

Types

type HttpMethod

type HttpMethod string
const (
	GET     HttpMethod = `GET`
	HEAD    HttpMethod = `HEAD`
	OPTIONS HttpMethod = `OPTIONS`
	TRACE   HttpMethod = `TRACE`
	PUT     HttpMethod = `PUT`
	DELETE  HttpMethod = `DELETE`
	POST    HttpMethod = `POST`
	PATCH   HttpMethod = `PATCH`
	CONNECT HttpMethod = `CONNECT`
)

type HttpRequestHandler

type HttpRequestHandler func(req *http.Request, params Params) *HttpResponse

type HttpResponse

type HttpResponse struct {
	// contains filtered or unexported fields
}

func NewHttpResponse

func NewHttpResponse(status int) *HttpResponse

func (*HttpResponse) SetBody

func (r *HttpResponse) SetBody(body string) *HttpResponse

func (*HttpResponse) SetHeader

func (r *HttpResponse) SetHeader(key string, value string) *HttpResponse

type Params

type Params map[string]any

type RequestHandlerMap

type RequestHandlerMap map[HttpMethod]map[string]RequestHandlerPackage

type RequestHandlerPackage

type RequestHandlerPackage struct {
	Handler   HttpRequestHandler
	ParamList map[int]string
	Params    Params
}

type Router

type Router struct {
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

----------- CONSTRUCTOR -----------

func (*Router) RegisterRoute

func (r *Router) RegisterRoute(method HttpMethod, path string, handler HttpRequestHandler)

----------- ROUTE REGISTRATION -----------

func (*Router) RegisteredRoutes

func (r *Router) RegisteredRoutes() *RequestHandlerMap

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)

----------- REQUEST ROUTING -----------

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(addr string, port int) *Server

func (*Server) GetRouter

func (s *Server) GetRouter() *Router

func (*Server) Run

func (s *Server) Run()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL