-
Notifications
You must be signed in to change notification settings - Fork 0
/
carshare-back.go
121 lines (103 loc) · 3.47 KB
/
carshare-back.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
RESTful API for the car share system
*/
package main
import (
"fmt"
"net/http"
"os"
"github.com/LewisWatson/carshare-back/model"
"github.com/LewisWatson/carshare-back/resource"
"github.com/LewisWatson/carshare-back/storage/mongodb"
"github.com/LewisWatson/firebase-jwt-auth"
"github.com/alecthomas/kingpin"
"github.com/benbjohnson/clock"
"github.com/gin-gonic/gin"
"github.com/manyminds/api2go"
"github.com/manyminds/api2go/routing"
"github.com/op/go-logging"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/mgo.v2"
)
var (
port = kingpin.Flag("port", "Set port to bind to").Default("31415").Envar("CARSHARE_PORT").Int()
mgoURL = kingpin.Flag("mgoURL", "URL to MongoDB server or seed server(s) for clusters").Default("localhost").Envar("CARSHARE_MGO_URL").URL()
firebaseProjectID = kingpin.Flag("firebase", "Firebase project to use for authentication").Default("ridesharelogger").Envar("CARSHARE_FIREBASE_PROJECT").String()
acao = kingpin.Flag("cors", "Enable HTTP Access Control (CORS) for the specified URI").PlaceHolder("URI").Envar("CARSHARE_CORS_URI").String()
log = logging.MustGetLogger("main")
format = logging.MustStringFormatter(
`%{color}%{time:2006-01-02T15:04:05.999} %{level:.4s} %{id:03x}%{color:reset} %{message}`,
)
userStorage = &mongodb.UserStorage{}
carShareStorage = &mongodb.CarShareStorage{}
tripStorage = &mongodb.TripStorage{}
)
func init() {
logging.SetBackend(logging.NewBackendFormatter(logging.NewLogBackend(os.Stderr, "", 0), format))
kingpin.UsageTemplate(kingpin.CompactUsageTemplate).Version("0.4.0").Author("Lewis Watson")
kingpin.CommandLine.Help = "API for tracking car shares"
kingpin.Parse()
}
func main() {
log.Infof("connecting to mongodb server %s%s", (*mgoURL).Host, (*mgoURL).Path)
db, err := mgo.Dial((*mgoURL).String())
if err != nil {
log.Fatalf("error connecting to mongodb server: %s", err)
}
log.Infof("using firebase project \"%s\" for authentication", *firebaseProjectID)
tokenVerifier, err := fireauth.New(*firebaseProjectID)
if err != nil {
log.Fatal(err)
}
r := gin.Default()
api := api2go.NewAPIWithRouting(
"v0",
api2go.NewStaticResolver("/"),
routing.Gin(r),
)
if *acao != "" {
log.Infof("enabling CORS access for %s", *acao)
}
api.UseMiddleware(
func(c api2go.APIContexter, w http.ResponseWriter, r *http.Request) {
// ensure the db connection is always available in the context
c.Set("db", db)
if *acao != "" {
w.Header().Set("Access-Control-Allow-Origin", *acao)
w.Header().Set("Access-Control-Allow-Headers", "Authorization,content-type")
w.Header().Set("Access-Control-Allow-Methods", "GET,PATCH,DELETE,OPTIONS")
}
},
)
api.AddResource(
model.User{},
resource.UserResource{
UserStorage: userStorage,
CarShareStorage: carShareStorage,
TokenVerifier: tokenVerifier,
},
)
api.AddResource(
model.Trip{},
resource.TripResource{
TripStorage: tripStorage,
UserStorage: userStorage,
CarShareStorage: carShareStorage,
TokenVerifier: tokenVerifier,
Clock: clock.New(),
},
)
api.AddResource(
model.CarShare{},
resource.CarShareResource{
CarShareStorage: carShareStorage,
TripStorage: tripStorage,
UserStorage: userStorage,
TokenVerifier: tokenVerifier,
},
)
// handler for metrics
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
log.Infof("Listening and serving HTTP on :%d", *port)
log.Fatal(r.Run(fmt.Sprintf(":%d", *port)))
}