gorouter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2019 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GORouter Build 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 := router.GetRouter(routes(), []string{})
	log.Fatal(http.ListenAndServe(":80", router))
}

func routes() router.Routes {
	return router.Routes{
		List: []router.Route{
			router.Route{
				Method: "GET",
				Path:   "/robots.txt",
				Handler: router.PlainTextHandler([]string{
					"User-agent: *",
					"Disallow: /",
				}),
			},
			router.Route{
				Method:  "GET",
				Path:    "/healthz",
				Handler: router.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() string {
	return `{"status":"up"}`
}

Router with middlewares

package somepackge

import "github.com/Illuminasy/gorouter"

func startServer() {
	mc := middleware.MiddlewareConfig{
		Bugsnag: middleware.BugsnagConfig{
			APIKey:       "apikey",
			ReleaseStage: "testing",
			ProjectPackages: []string{
				"main",
			},
			NotifyReleaseStages: []string{
				"testing",
			},
			AppVersion: "0.1.0",
		},
		Newrelic: middleware.NewrelicConfig{
			AppName: "Testing",
			License: "newrelickey",
		},
	}

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

func routes() router.Routes {
	return router.Routes{
		List: []router.Route{
			router.Route{
				Method: "GET",
				Path:   "/robots.txt",
				Handler: router.PlainTextHandler([]string{
					"User-agent: *",
					"Disallow: /",
				}),
			},
			router.Route{
				Method:  "GET",
				Path:    "/healthz",
				Handler: router.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() string {
	return `{"status":"up"}`
}

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 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.MiddlewareConfig, routes Routes, additionalHeaders []string) http.Handler

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

func JsonHandler

func JsonHandler(handler func() string) 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

Types

type Route

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

type Routes

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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