-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
57 lines (46 loc) · 1.03 KB
/
server.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
package main
import (
"net/http"
"os"
"github.com/bluele/gcache"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
type Server struct {
router *mux.Router
log *logrus.Logger
port string
cache gcache.Cache
}
func CreateServer() Server {
s := Server{}
err := s.Bootstrap()
if err != nil {
panic(err)
}
return s
}
func (s *Server) Bootstrap() error {
// initialize logger
l := logrus.New()
s.log = l
// set port number
port := os.Getenv("SERVER_PORT")
if port == "" {
port = "8888"
}
s.port = port
// bind routes
s.router = mux.NewRouter()
s.router.HandleFunc("/", s.handleVersionWithFilter("stable"))
s.router.HandleFunc("/all", s.handleVersionWithFilter("all"))
s.router.HandleFunc("/alpha", s.handleVersionWithFilter("alpha"))
s.router.HandleFunc("/rc", s.handleVersionWithFilter("rc"))
// set up cache storage
s.cache = gcache.New(20).LRU().Build()
return nil
}
func (s *Server) Serve() error {
s.log.Info("Starting server on port ", s.port)
return http.ListenAndServe(":"+s.port, s.router)
}