opirouter

package module
v0.0.0-...-879037c Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2022 License: MIT Imports: 9 Imported by: 0

README

opirouter

opinionated router

Features

  • regex routes
  • named url parameters
  • handle each http method
  • custom html include tags
  • default final catch-all route
  • lots of caching

Usage

package main

import (
	"fmt"
	"io"
	"net/http"

	"github.com/dougty/opirouter"
)

var router *opirouter.Router = opirouter.NewRouter()

func main() {
	router.GET("/test/:foo/:bar", testRoute)	
	http.ListenAndServe(":8888", router)
}

// http://localhost:8888/test/abc/123
func testRoute(w http.ResponseWriter, r *http.Request, p opirouter.Params) {
	io.WriteString(w, "hello browser\n")
	fmt.Println(p["foo"], p["bar"])
}

Static files

Static files can be served by passing http.FileServer to opirouter.Handle or opirouter.SetDefaultHandle

router.Handle("/assets", http.FileServer(http.Dir("./static")))
router.SetDefaultHandle(http.FileServer(http.Dir("./static")))

HTML Preprocessing

HTML files can be optionally preprocessed to parse custom include tags

router.EnableHTMLParser("./static")
  • in the above example, all *.html files within the static dir will be preprocessed for includes
  • <!-- include "foobar.html" --> in html will be replaced with the contents of ./static/foobar.html
  • files are preprocessed once before serving to the browser and cached for subsequent requests
  • includes can be nested as much as needed
  • great for Vue component templates!

Disable caching

Caching can be disabled for development purposes with the env var NOCACHE=1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback func(http.ResponseWriter, *http.Request, Params)

type Params

type Params map[string]string

contains url parameters (if any)

type Router

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

func NewRouter

func NewRouter() *Router

create and initialize new router

func (*Router) DELETE

func (router *Router) DELETE(str string, cb Callback)

func (*Router) EnableHTMLParser

func (router *Router) EnableHTMLParser(staticDir string)

enables html parser for preprocessing custom include tags

func (*Router) GET

func (router *Router) GET(str string, cb Callback)

func (*Router) HEAD

func (router *Router) HEAD(str string, cb Callback)

func (*Router) Handle

func (router *Router) Handle(str string, hfunc http.Handler)

GET wrapper for standard http.Handler funcs

func (*Router) PATCH

func (router *Router) PATCH(str string, cb Callback)

func (*Router) POST

func (router *Router) POST(str string, cb Callback)

func (*Router) PUT

func (router *Router) PUT(str string, cb Callback)

func (*Router) ServeHTTP

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

func (*Router) SetDefault

func (router *Router) SetDefault(cb Callback)

default route callback (run after all other routes) note: 404 *must* be handled in here if used!

func (*Router) SetDefaultHandle

func (router *Router) SetDefaultHandle(hfunc http.Handler)

SetDefault wrapper for standard http.Handler funcs

Jump to

Keyboard shortcuts

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