arnaucube
arnaucube/darkID-prototypePublic Self-hosted repository
darkID-prototype/darkID-library-login-example/restConfig.go
darkID-library-login-example/restConfig.goRaw
 1package main
 2
 3import (
 4	"log"
 5	"net/http"
 6	"time"
 7
 8	"github.com/gorilla/mux"
 9)
10
11type Route struct {
12	Name        string
13	Method      string
14	Pattern     string
15	HandlerFunc http.HandlerFunc
16}
17
18func Logger(inner http.Handler, name string) http.Handler {
19	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20		start := time.Now()
21
22		inner.ServeHTTP(w, r)
23
24		log.Printf(
25			"%s\t%s\t%s\t%s",
26			r.Method,
27			r.RequestURI,
28			name,
29			time.Since(start),
30		)
31	})
32}
33func NewRouter() *mux.Router {
34	router := mux.NewRouter().StrictSlash(true)
35	for _, route := range routes {
36		var handler http.Handler
37		handler = route.HandlerFunc
38		handler = Logger(handler, route.Name)
39
40		router.
41			Methods(route.Method).
42			Path(route.Pattern).
43			Name(route.Name).
44			Handler(handler)
45	}
46	return router
47}