router

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 12 Imported by: 0

README

router

A tiny HTTP router with grouping and middleware support.

Installation

go get github.com/go-ind/router

Usage

package main

import (
    "fmt"
    "net/http"

    "github.com/go-ind/router"
)

func main() {
    r := router.SetupDefaultRouter()

    r.Get("/", func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprint(w, "hello")
    })

    api := r.Group("/api")
    api.Get("/ping", func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprint(w, "pong")
    })

    // simple middleware example
    r.Use(func(next http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            w.Header().Set("X-Demo", "middleware")
            next.ServeHTTP(w, r)
        })
    })

    http.ListenAndServe(":8080", r)
}

Documentation

Index

Constants

View Source
const (
	LogKey = Key(48)

	COMPLETE_LOGING   = "FULLY_LOGGING"
	INCOMPLETE_LOGING = "INCOMPLETE_LOGING"
	NON_LOGING        = "NON_LOGING"
)
View Source
const HttpMethodDelete = "DELETE"
View Source
const HttpMethodGet = "GET"
View Source
const HttpMethodPatch = "PATCH"
View Source
const HttpMethodPost = "POST"
View Source
const HttpMethodPut = "PUT"

Variables

This section is empty.

Functions

func DumpRequest

func DumpRequest(req *http.Request) string

DumpRequest returns the http request as a string.

func JSONMarshal

func JSONMarshal(t interface{}) ([]byte, error)

JSONMarshal marshals the given type without HTML escaping.

func Logger

func Logger(ctx context.Context, response string, statuscode int)

func Output

func Output(out *Data, level string)

Output prints structured log data.

func ResponseJSON

func ResponseJSON(w http.ResponseWriter, ctx context.Context, code int, status bool, message string, rs, pagination interface{})

func ResponseXML

func ResponseXML(w http.ResponseWriter, ctx context.Context, code int, status bool, message string, rs, pagination interface{})

func StartRecord

func StartRecord(req *http.Request, start time.Time) *http.Request

Types

type Data

type Data struct {
	RequestID     string    `json:"RequestID"`
	TimeStart     time.Time `json:"TimeStart"`
	UserCode      string    `json:"UserCode"`
	Device        string    `json:"Device"`
	Host          string    `json:"Host"`
	Endpoint      string    `json:"Endpoint"`
	RequestMethod string    `json:"RequestMethod"`
	RequestHeader string    `json:"RequestHeader"`
	StatusCode    int       `json:"StatusCode"`
	Response      string    `json:"Response"`
	ExecTime      float64   `json:"ExecutionTime"`
	Messages      []string  `json:"Messages"`
}

Data is data standard output

type Group

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

Group represents a collection of routes sharing the same prefix.

func (*Group) Delete

func (g *Group) Delete(path string, handler func(http.ResponseWriter, *http.Request))

func (*Group) Get

func (g *Group) Get(path string, handler func(http.ResponseWriter, *http.Request))

func (*Group) Patch

func (g *Group) Patch(path string, handler func(http.ResponseWriter, *http.Request))

func (*Group) Post

func (g *Group) Post(path string, handler func(http.ResponseWriter, *http.Request))

func (*Group) Put

func (g *Group) Put(path string, handler func(http.ResponseWriter, *http.Request))

type Key

type Key int

Key context

type Responseservice

type Responseservice struct {
	Status       int         `json:"status"`
	ErrorMessage string      `json:"error_message"`
	Data         interface{} `json:"data"`
	Pagination   interface{} `json:"pagination"`
	Message      string      `json:"message"`
}

type Router

type Router struct {
	Routes      map[string]map[string]func(http.ResponseWriter, *http.Request)
	SettingLogs string
	// contains filtered or unexported fields
}

Router is a tiny HTTP request router with grouping and middleware support.

func SetupDefaultRouter

func SetupDefaultRouter() *Router

SetupDefaultRouter returns router with logging enabled.

func SetupWithNoLogging

func SetupWithNoLogging() *Router

SetupWithNoLogging returns router without logging.

func (*Router) Delete

func (r *Router) Delete(path string, handler func(http.ResponseWriter, *http.Request))

func (*Router) Get

func (r *Router) Get(path string, handler func(http.ResponseWriter, *http.Request))

func (*Router) Group

func (r *Router) Group(prefix string) *Group

Group creates a new route group with the given prefix.

func (*Router) Patch

func (r *Router) Patch(path string, handler func(http.ResponseWriter, *http.Request))

func (*Router) Post

func (r *Router) Post(path string, handler func(http.ResponseWriter, *http.Request))

func (*Router) Put

func (r *Router) Put(path string, handler func(http.ResponseWriter, *http.Request))

func (*Router) ServeHTTP

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

ServeHTTP implements http.Handler and dispatches the request to the matching route.

func (*Router) Use

func (r *Router) Use(middlewares ...func(http.Handler) http.Handler)

Use appends middlewares to the router. They are executed in the order they are added.

type UTCFormatter

type UTCFormatter struct {
	logrus.Formatter
}

UTCFormatter ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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