-
Notifications
You must be signed in to change notification settings - Fork 66
/
doggy.go
62 lines (54 loc) · 1.57 KB
/
doggy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package doggy
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/hnlq715/doggy/middleware"
"github.com/julienschmidt/httprouter"
"github.com/spf13/viper"
"github.com/urfave/negroni"
)
// New returns a new Negroni instance with no middleware preconfigured.
func New(handlers ...negroni.Handler) *negroni.Negroni {
return negroni.New()
}
// Classic returns a new Negroni instance with the default middleware already
// in the stack.
//
// Recovery - Panic Recovery Middleware
// Logger - Request/Response Logging
// TraceID - Trace ID Middleware
// RealIP - Get Real Client IP
// CloseNotify - Notify Client Close
// Timeout - Stop Process When Timeout
func Classic() *negroni.Negroni {
n := negroni.New()
n.Use(middleware.NewRecovery())
n.Use(middleware.NewTraceID())
logViper := viper.Sub("log")
n.Use(middleware.NewRealIP())
n.Use(middleware.NewLogger(logViper.GetString("level"), logViper.GetString("file")))
n.Use(middleware.NewCloseNotify())
n.Use(middleware.NewTimeout(viper.GetDuration("middleware.timeout")))
n.Use(middleware.NewParseForm())
return n
}
// NewMux returns a new router instance.
func NewMux() *mux.Router {
return mux.NewRouter()
}
// NewHttpRouter returns a new httprouter instance.
func NewHttpRouter() *httprouter.Router {
return httprouter.New()
}
// ListenAndServe always returns a non-nil error.
func ListenAndServe(handler http.Handler) error {
return http.ListenAndServe(viper.GetString("listen"), handler)
}
func init() {
// Load default config
err := loadConfig()
if err != nil {
fmt.Printf("loadConfig failed, err=%s\n", err)
}
}