-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestserver.go
126 lines (112 loc) · 3.09 KB
/
restserver.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package rest
import (
"context"
"crypto/tls"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/z26100/log-go"
"net/http"
"strings"
"time"
)
type RestServer struct {
r *mux.Router
srv *http.Server
config ServerConfig
}
type TokenHandler func(http.Handler) http.Handler
type ServerConfig struct {
Listen string
PathPrefix string
Cors bool
CertFile, KeyFile string
ReadTimeout, WriteTimeout time.Duration
TlsConfig *tls.Config
Auth bool
TokenHandler TokenHandler
Debug bool
}
type Route struct {
Path string
PathPrefix string
HandlerFc http.HandlerFunc
Methods string
}
/*
****************
REST Server
****************
*/
func RunRestServer(routes []Route, serverConfig ServerConfig) {
log.Infof("listen:\t\t%s", serverConfig.Listen)
log.Infof("prefix:\t\t%s", serverConfig.PathPrefix)
log.Infof("cors:\t\t%t", serverConfig.Cors)
log.Infof("debug:\t\t%t", serverConfig.Debug)
server := NewDefaultServer(routes, serverConfig)
err := server.Listen(serverConfig.PathPrefix, serverConfig.Cors)
if err != nil {
log.Fatal(err)
}
err = server.Shutdown(context.Background())
if err != nil {
log.Fatal(err)
}
}
func NewDefaultServer(routes []Route, config ServerConfig) *RestServer {
s := RestServer{}
log.Printf("Starting mux router with tls = %v", config.CertFile != "" && config.KeyFile != "")
s.r = mux.NewRouter()
for _, item := range routes {
log.Printf("adding route %s ( %s)", item.Path, item.Methods)
if item.Path != "" {
s.r.Path(item.Path).HandlerFunc(item.HandlerFc).Methods(strings.Split(item.Methods, ",")...)
} else if item.PathPrefix != "" {
s.r.PathPrefix(item.PathPrefix).HandlerFunc(item.HandlerFc).Methods(strings.Split(item.Methods, ",")...)
}
}
log.Println("Starting listener")
s.config = config
return &s
}
func (s *RestServer) Listen(pathPrefix string, corsAllowed bool, preHandlers ...func(handler http.Handler) http.Handler) error {
var handler http.Handler
handler = s.r
for _, preHandler := range preHandlers {
handler = preHandler(handler)
}
if s.config.Debug {
log.LogLevel = log.DEBUG
}
if s.config.Auth {
handler = s.config.TokenHandler(handler)
log.Printf("auth enabled")
}
if pathPrefix != "" {
handler = http.StripPrefix(pathPrefix, handler)
log.Printf("path prefix = %s", pathPrefix)
}
if corsAllowed {
log.Println("enable cors")
c := cors.AllowAll()
handler = c.Handler(handler)
}
log.Printf("listening at %s", s.config.Listen)
s.srv = &http.Server{
Addr: s.config.Listen,
Handler: handler,
TLSConfig: s.config.TlsConfig,
ReadTimeout: s.config.ReadTimeout,
WriteTimeout: s.config.WriteTimeout,
}
if s.config.TlsConfig == nil || s.config.CertFile == "" || s.config.KeyFile == "" {
return s.srv.ListenAndServe()
} else {
return s.srv.ListenAndServeTLS(s.config.CertFile, s.config.KeyFile)
}
}
func (s RestServer) Shutdown(ctx context.Context) error {
return s.srv.Shutdown(ctx)
}
func (s RestServer) GetRouter() *mux.Router {
return s.r
}