-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (43 loc) · 1.14 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"github.com/ant0ine/go-json-rest/rest"
"github.com/servomac/todo-api-go/bundles/todo"
)
func main() {
config := getConfig()
db := todo.Database{}
db.InitDB(config.Database)
db.InitSchema()
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
api.Use(&rest.CorsMiddleware{
RejectNonCorsRequests: false,
OriginValidator: func(origin string, request *rest.Request) bool {
return origin == "http://localhost:3000"
},
AllowedMethods: []string{"GET", "POST", "PUT"},
AllowedHeaders: []string{
"Accept", "Content-Type", "X-Custom-Header", "Origin"},
AccessControlAllowCredentials: true,
AccessControlMaxAge: 3600,
})
router, err := rest.MakeRouter(
rest.Get("/todos", db.GetTodos),
rest.Post("/todos", db.PostTodo),
rest.Get("/todos/:id", db.GetTodo),
rest.Put("/todos/:id", db.PutTodo),
rest.Delete("/todos/:id", db.DeleteTodo),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(initServer(config, api))
}
func initServer(c Config, api *rest.Api) error {
addr := fmt.Sprintf(":%v", c.Port)
return http.ListenAndServe(addr, api.MakeHandler())
}