wildcard_router

package module
v0.0.0-...-56710e5 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2017 License: MIT Imports: 2 Imported by: 14

README

WildcardRouter

WildcardRouter handles dynamic routes.

GoDoc

Usage

Suppose you have a model, Page, that handles requests by ServeHTTP function based on a URL in a database.

import (
  "github.com/qor/wildcard_router"
)

type PageHandler struct{}

type Page struct {
	URL  string
	Body string
}

// Page's records in database:

// Record1(URL: /page1, Content: "Page1")
// Record2(URL: /page2, Content: "Page2")

func (PageHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	var page Page
	if !db.First(&page, "url = ?", req.URL.Path).RecordNotFound() {
		w.Write([]byte(page.Body))
	}
}

And you have another model, FAQ, which also handles requests by ServeHTTP function based on a URL in a database...

type FAQHandler struct{}

type FAQ struct {
	URL      string
	Question string
	Answer   string
}

// FAQ's records in database:

// Record1(URL: /faq1, Question: "FAQ1", Answer: "Answer1")
// Record2(URL: /faq2, Question: "FAQ2", Answer: "Answer2")

func (FAQHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	var faq FAQ
	if !db.First(&faq, "url = ?", req.URL.Path).RecordNotFound() {
		w.Write([]byte(fmt.Sprintf("%v: %v", faq.Question, faq.Answer)))
	}
}

Those URLs could be anything, with no rule... Let's initialize WildcardRouter and mount it.

func main() {
	mux := http.NewServeMux()

	wildcardRouter := wildcard_router.New()
	wildcardRouter.MountTo("/", mux)
}

AddHandler to WildcardRouter and any model that implements method ServeHTTP can be routed to as a handler.

    wildcardRouter.AddHandler(PageHandler{})
    wildcardRouter.AddHandler(FAQHandler{})

If you would like to customize your 404 page, you could set handlefunc to NoRoute

  wildcardRouter.NoRoute(func(w http.ResponseWriter, req *http.Request) {
      // You need to set Content-Type to `text/html` if you would like brower recognize as HTML
      w.Header().Set("Content-Type", "text/html; charset=utf-8")
      // You need to set Status as NotFound too if you would like status code is 404
      w.WriteHeader(http.StatusNotFound)
      w.Write([]byte("Sorry, this page was gone!"))
  })

The behavior will be:

// Visit "/page1"   will return "Page1"
// Visit "/page2"   will return "Page2"
// Visit "/faq1"    will return "FAQ1: Answer1"
// Visit "/faq2"    will return "FAQ2: Answer2"
// Visit "/unknown" will return "Sorry, this page was gone!" with statu code 404

License

Released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WildcardRouter

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

WildcardRouter holds registered route handlers

func New

func New() *WildcardRouter

New return a new WildcardRouter

func (*WildcardRouter) AddHandler

func (w *WildcardRouter) AddHandler(handler http.Handler)

AddHandler will append new handler to Handlers

func (*WildcardRouter) MountTo

func (w *WildcardRouter) MountTo(mountTo string, mux *http.ServeMux)

MountTo mount the service into mux (HTTP request multiplexer) with given path

func (*WildcardRouter) NoRoute

func (w *WildcardRouter) NoRoute(handler http.HandlerFunc)

NoRoute will set handler to handle 404

func (*WildcardRouter) ServeHTTP

func (w *WildcardRouter) ServeHTTP(writer http.ResponseWriter, req *http.Request)

ServeHTTP serve http for wildcard router

func (*WildcardRouter) Use

func (w *WildcardRouter) Use(middleware func(writer http.ResponseWriter, request *http.Request))

Use will append new middleware

type WildcardRouterWriter

type WildcardRouterWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

WildcardRouterWriter will used to capture status

func (WildcardRouterWriter) Status

func (w WildcardRouterWriter) Status() int

Status will return request's status code

func (*WildcardRouterWriter) Write

func (w *WildcardRouterWriter) Write(data []byte) (int, error)

Write only set content when not 404

func (*WildcardRouterWriter) WriteHeader

func (w *WildcardRouterWriter) WriteHeader(statusCode int)

WriteHeader only set status code when not 404

Jump to

Keyboard shortcuts

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