gorouter

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2022 License: MIT Imports: 5 Imported by: 0

README

GORouter Build Status Coverage Status GoDoc License

GoRouter a package which wraps around https://github.com/julienschmidt/httprouter allows to start router with middlewares

Currently supported middlewares:

  1. NewRelic - APM monitoring tool
  2. Bugsnag - Log Management

Usage

Get the library:

$ go get -v github.com/illuminasy/gorouter

Just the router (no middleware)

func startServer() {
    fmt.Println("Listening for http on port 80")
    router := gorouter.GetRouter(routes(), []string{})
    log.Fatal(http.ListenAndServe(":80", router))
}

func routes() gorouter.Routes {
    return gorouter.Routes{
        List: []gorouter.Route{
            gorouter.Route{
                Method: "GET",
                Path:   "/robots.txt",
                Handler: gorouter.PlainTextHandler([]string{
                    "User-agent: *",
                    "Disallow: /",
                }),
            },
            gorouter.Route{
                Method:  "GET",
                Path:    "/healthz",
                Handler: gorouter.JSONHandler(healthzHandler),
            },
            gorouter.Route{
                Method:  "GET",
                Path:    "/test",
                Handler: gorouter.HTMLHandler(healthzHandler),
            },
        },
        PanicHandler: func(w http.ResponseWriter, r *http.Request, err interface{}) {
            w.WriteHeader(http.StatusInternalServerError)
            w.Header().Set("Content-Type", "application/json")
            fmt.Fprint(w, "")
            log.Println(err)
        },
    }
}

func healthzHandler(w http.ResponseWriter, r *http.Request) (string, int) {
    return `{"status":"up"}`, http.StatusOK
}

Router with middlewares

package somepackge

import "github.com/illuminasy/gorouter"
import "github.com/illuminasy/gorouter/middleware"

func startServer() {
    mc := middleware.Config{
        ErrorReportingConfig: middleware.ErrorReportingConfig{
            Enabled:      true,
            Bugsnag:      true,
            APIKey:       "testing",
            AppType:      "router",
            ReleaseStage: "Dev",
            AppVersion:   "0.1.0",
            ProjectPackages: []string{
                "main",
            },
            NotifyReleaseStages: []string{
                "Dev",
            },
            PanicHandler: func() {},
            Hostname: "localhost",
        },
        MetricCollectorConfig: middleware.MetricCollectorConfig{
            Enabled:  true,
            Newrelic: true,
            Debug:    true,
            AppName:  "TestApp",
            License:  "testing",
            Labels: map[string]string{
                "Environment": "Dev",
                "Version":     "0.1.0",
            },
            HostDisplayName: "localhost",
        },
    }

    fmt.Println("Listening for http on port 80")
    router := gorouter.GetRouterWithMiddleware(mc, routes(), []string{})
    log.Fatal(http.ListenAndServe(":80", router))
}

func routes() gorouter.Routes {
    return gorouter.Routes{
        List: []gorouter.Route{
            gorouter.Route{
                Method: "GET",
                Path:   "/robots.txt",
                Handler: gorouter.PlainTextHandler([]string{
                    "User-agent: *",
                    "Disallow: /",
                }),
            },
            gorouter.Route{
                Method:  "GET",
                Path:    "/healthz",
                Handler: gorouter.JSONHandler(healthzHandler),
            },
        },
        PanicHandler: func(w http.ResponseWriter, r *http.Request, err interface{}) {
            w.WriteHeader(http.StatusInternalServerError)
            w.Header().Set("Content-Type", "application/json")
            fmt.Fprint(w, "")
            log.Println(err)
        },
    }
}

func healthzHandler(w http.ResponseWriter, r *http.Request) (string, int) {
    return `{"status":"up"}`, http.StatusOK
}

Gorouter starts a web transaction by wrapping middleware around the handlers but if you still need to create custom transactions (maybe non web stuff)

    // w, r are w http.ResponseWriter, r *http.Request
    // these are optionals, just pass nil instead for non web transactions
    txn := middleware.GetMetricCollectorTransaction("someTxnID", "someTxnName", w, r)
    // Do something
    txn.End()

To create segments within a transaction

    // w, r are w http.ResponseWriter, r *http.Request
    // these are optionals, just pass nil instead
    // if transaction doesnot exist then it creates new one.
    s := middleware.StartMetricCollectorSegment("someTxnID", "someTxnName", "someSegmentName", w, r)
    // Do something
    s.End()

To create datastore segments within a transaction say for mysql

    dataStore := middleware.DataStore {
        Product: "mysql",
        Collection: "users",
        Operation: "INSERT",
        ParameterizedQuery: `INSERT INTO users (name, age) VALUES ($1, $2)"`,
        QueryParameters: map[string]interface{}{
            "name": "Dracula",
            "age": 439,
        },
        Host: "mysql-server-1",
        PortPathOrID: "3306",
        DatabaseName: "my_database",
    }
    // w, r are w http.ResponseWriter, r *http.Request
    // these are optionals, just pass nil instead
    // if transaction doesnot exist then it creates new one.
    s := middleware.StartMetricCollectorDataStoreSegment("someTxnID", "someTxnName", dataStore, w, r)
    // Do something
    s.End()

To report errors to metric collectors

    // w, r are w http.ResponseWriter, r *http.Request
    // these are optionals, just pass nil instead
    // if transaction doesnot exist then it creates new one.
    middleware.MetricCollectorNoticeError("someTxnID", "someTxnName", errors.New("Invalid API config"), w, r)

Send Errors to bugsnag

    middleware.ReportErrorToBugsnag("someErrorClass", errors.New("Invalid API config"))

Thanks to

https://github.com/julienschmidt/httprouter

https://github.com/newrelic/go-agent

https://github.com/bugsnag/bugsnag-go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileHandler added in v0.2.3

func FileHandler(handler func(http.ResponseWriter, *http.Request) (string, int)) httprouter.Handle

FileHandler handles files responses with appropriate headers

func GetRouter

func GetRouter(routes Routes, additionalHeaders []string) *httprouter.Router

GetRouter returns a router, optionally additional headers can be passed to set

func GetRouterWithMiddleware

func GetRouterWithMiddleware(mc middleware.Config, routes Routes, additionalHeaders []string) http.Handler

GetRouterWithMiddleware returns a router with middlewares wrapped around it, optionally additional headers can be passed to set

func HTMLHandler added in v0.1.27

func HTMLHandler(handler func(http.ResponseWriter, *http.Request) (string, int)) httprouter.Handle

HTMLHandler handles html responses with appropriate headers

func JSONHandler added in v0.1.12

func JSONHandler(handler func(http.ResponseWriter, *http.Request) (string, int)) httprouter.Handle

JSONHandler handles json responses with appropriate headers

func PlainTextHandler

func PlainTextHandler(lines []string) httprouter.Handle

PlainTextHandler handles plain text responses with appropriate headers

func StaticFileHandler added in v0.1.27

func StaticFileHandler(path string) httprouter.Handle

StaticFileHandler handles static files responses with appropriate headers

Types

type Route

type Route struct {
	Method  string
	Path    string
	Handler httprouter.Handle
}

Route Each route needs url path, type of request and a handler

type Routes

type Routes struct {
	List         []Route
	PanicHandler func(w http.ResponseWriter, r *http.Request, err interface{})
}

Routes List of routes and panic handler

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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