Mir.httprouter
Mir.httprouter module provide mir.Engine implement backend by httprouter.
Usage
package main
import(
"github.com/alimy/mir"
"github.com/julienschmidt/httprouter"
"log"
"net/http"
mirE "github.com/alimy/mir/module/httprouter"
)
type site struct {
index mir.Get `mir:"/index/"`
articles mir.Get `mir:"/articles/:category/#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, p httprouter.Params) {
rw.WriteHeader(http.StatusOK)
rw.Header().Add("Content-Type", "text/plain; charset=utf-8")
rw.Write([]byte("Index"))
}
// GetArticles handler of articles indicator that contains Host/Path/Handler info.
// Path info is the second or first(if no host info) segment start with '/'(eg: /articles/:category)
// 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, p httprouter.Params) {
rw.WriteHeader(http.StatusOK)
rw.Header().Add("Content-Type", "text/plain; charset=utf-8")
rw.Write([]byte("GetArticles"))
}
func main() {
// Create a new httprouter instance
r := httprouter.New()
// Register handler to engine by mir
mirE.Register(r, &site{})
// Start http serve
log.Fatal(http.ListenAndServe(":8080", r))
}