-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopal.go
60 lines (46 loc) · 1.1 KB
/
gopal.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
package main
import (
"log"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/rs/cors"
)
const SERVICE_PORT string = "8080"
type GoPal struct {
Db *Dao
Router *httprouter.Router
}
func New() *GoPal {
settings := GetSettings()
instance := new(GoPal)
instance.Db = NewDao(settings)
// Enforce index usage
instance.Db.EnsureIndex()
var routes = Routes{
Route{
"GET", "/palindrome", PalindromeListHandler(instance.Db),
},
Route{
"POST", "/palindrome", PalindromeAddHandler(instance.Db),
},
Route{
"GET", "/palindrome/:id", PalindromeGetHandler(instance.Db),
},
Route{
"DELETE", "/palindrome/:id", PalindromeDeleteHandler(instance.Db),
},
}
instance.Router = NewRouter(routes)
return instance
}
// Run starts the server
func (g *GoPal) Run() error {
defer g.Db.Close()
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"*"},
})
log.Println("gopal is running under port "+SERVICE_PORT)
return http.ListenAndServe(":"+SERVICE_PORT, c.Handler(g.Router))
}