-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
executable file
·75 lines (63 loc) · 1.63 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"log"
"net/http"
"time"
"os"
"github.com/bmizerany/pat"
"github.com/codegangsta/negroni"
"github.com/spf13/viper"
)
var Settings map[string]*viper.Viper
var environments string
func newServer() *http.Server {
n := negroni.New()
// Middlewares
if Settings["environments"].GetBool("log") {
n.Use(negroni.NewLogger())
}
n.UseFunc(recovery())
n.Use(negroni.NewStatic(http.Dir("./public")))
// Setup routes
router := pat.New()
router.Get("/api/v1/:env/driver/:ID", http.HandlerFunc(getDriverLocation))
//router.Post("/api/v1/<resource>", http.HandlerFunc(/*handle func*/))
// Add alive endpoint
// router.Get("/alive", http.HandlerFunc(alive))
// Add the router action
n.UseHandler(router)
Server := &http.Server{
Addr: ":" + Settings["environments"].GetString("server.port"),
Handler: n,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
MaxHeaderBytes: 1 << 20,
}
return Server
}
func InitConfigs() {
environments = os.Getenv("ENV")
if environments == "" {
environments = "development"
}
Settings = make(map[string]*viper.Viper)
Settings["environments"] = viper.New()
Settings["environments"].SetConfigType("yml")
Settings["environments"].AddConfigPath("config/environments")
Settings["environments"].SetConfigName(environments)
err := Settings["environments"].ReadInConfig()
if err != nil {
fmt.Errorf("Fatal error config file: %s \n", err)
}
}
func main() {
InitConfigs()
InitApp()
Server := newServer()
log.Printf("Start serving on %s", Server.Addr)
log.Println(Server.ListenAndServe())
}
func InitApp() {
// add here plugins and middleware
}