-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
70 lines (57 loc) · 1.56 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/nicolaszein/go-retro/database"
"github.com/nicolaszein/go-retro/handlers"
"github.com/nicolaszein/go-retro/settings"
)
func main() {
port := settings.PORT
if port == "" {
port = "8000"
}
db, err := database.NewPostgres(settings.DATABASE_URL)
if err != nil {
fmt.Println("Failed to connect database with error: ", err)
}
defer db.DB.Close()
fmt.Println("Starting server on port " + port)
r := chi.NewRouter()
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300,
})
r.Use(cors.Handler)
r.Get("/", handlers.HealthCheck)
env := handlers.Env{
DB: db,
}
r.Route("/api/v1", func(r chi.Router) {
r.Route("/teams", func(r chi.Router) {
r.Get("/", env.ListTeams)
r.Get("/{teamID}", env.FetchTeam)
r.Post("/", env.CreateTeam)
})
r.Route("/retrospectives", func(r chi.Router) {
r.Post("/", env.CreateRetrospective)
r.Get("/{retrospectiveID}", env.FetchRetrospective)
// Cards
r.Route("/{retrospectiveID}/cards", func(r chi.Router) {
r.Post("/", env.CreateCard)
r.Post("/{cardID}/votes", env.AddCardVote)
})
})
})
err = http.ListenAndServe(":"+port, r)
if err != nil {
fmt.Println("Error serving:", err)
}
}