mux

package module
v0.0.0-...-8c52ecd Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

Mir.Mux

Mir.Mux module provide mir.Engine implement backend by Mux.

Usage
package main

import(
	"github.com/alimy/mir"
	"github.com/gorilla/mux"
	"log"
	"net/http"
	
	mirE "github.com/alimy/mir/module/mux"
)

type site struct {
	count    uint32
	
	Group mir.Group     `mir:"v1"`
	index mir.Get       `mir:"/index/"`
	articles mir.Get    `mir:"//{subdomain}.example.com/articles/{category}/{id:[0-9]+}?filter={filter}&foo=bar&id={id:[0-9]+}#GetArticles"`
}

// Index handler of the index field that in site struct, the struct tag indicate
// this handler will register to path "/index/" and method is http.MethodGet.
func (h *site) Index(rw http.ResponseWriter, r *http.Request) {
	h.count++
	rw.Write([]byte("Index"))
}

// GetArticles handler of articles indicator that contains Host/Path/Queries/Handler info.
// Host info is the first segment start with '//'(eg:{subdomain}.domain.com)
// Path info is the second or first(if no host info) segment start with '/'(eg: /articles/{category}/{id:[0-9]+}?{filter})
// Queries info is the third info start with '?' and delimiter by '&'(eg: {filter}&{pages})
// Handler info is forth info start with '#' that indicate real handler method name(eg: GetArticles).if no handler info will
// use field name capital first char as default handler name(eg: if articles had no #GetArticles then the handler name will
// is Articles) 
func (h *site) GetArticles(rw http.ResponseWriter, r *http.Request) {
	rw.Write([]byte("GetArticles"))
}

func main() {
	// Create a new mux router instance
	r := mux.NewRouter()
	
	// Instance a mir engine to register handler for mux router by mir
	mirE.Register(r, &site{}) 
	
	// Bind to a port and pass our router in
	log.Fatal(http.ListenAndServe(":8013", r)) 
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Mir

func Mir(r *mux.Router) mir.Engine

Mir return mir.Engine interface implements instance. Used to register routes to mux router with struct tag string's information.

func Register

func Register(r *mux.Router, entries ...interface{}) error

Register use entries's info to register handler to mux router.

Types

This section is empty.

Jump to

Keyboard shortcuts

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