-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (70 loc) · 2.25 KB
/
main.go
File metadata and controls
85 lines (70 loc) · 2.25 KB
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
package main // import "github.com/devlucky/maporable-api"
import (
"fmt"
"net/http"
"log"
"github.com/julienschmidt/httprouter"
"github.com/devlucky/maporable-api/api"
"github.com/devlucky/maporable-api/adapters/trip_repo"
"github.com/devlucky/maporable-api/config"
"golang.org/x/oauth2"
"golang.org/x/oauth2/facebook"
"encoding/json"
"io/ioutil"
)
const userConfFilename string = "conf.json"
// TODO: Move this to API
func Ping(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprint(w, "pong")
}
func InjectConfig(a *config.Config, f func (http.ResponseWriter, *http.Request, httprouter.Params, *config.Config)) (func (http.ResponseWriter, *http.Request, httprouter.Params)) {
return func (w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
f(w, r, ps, a)
}
}
type UserConfig struct {
FbClientID string `json:"_fb_client_id"`
FbClientSecret string `json:"fb_client_secret"`
FbRedirectURL string `json:"_fb_redirect_url"`
FbState string `json:"_fb_state "`
}
func GetConfigVars() (*UserConfig) {
var uConf UserConfig
conf, err := ioutil.ReadFile(userConfFilename)
if err != nil {
log.Fatalf("Error reading from file %s", userConfFilename)
}
err = json.Unmarshal(conf, uConf)
if err != nil {
log.Fatalf("Error unmarshaling conf in %s", userConfFilename)
}
return uConf
}
func CurrentConfig(uConf *UserConfig) (*config.Config) {
return &config.Config{
TripRepo: trip_repo.NewInMemory(),
FacebookOAuth: &oauth2.Config{
ClientID: uConf.FbClientID,
ClientSecret: uConf.FbClientSecret,
RedirectURL: uConf.FbRedirectURL,
Scopes: []string{"public_profile"},
Endpoint: facebook.Endpoint,
},
FacebookOAuthState: uCong.FbState,
}
}
func main() {
conf := CurrentConfig(GetConfigVars())
router := httprouter.New()
// Ping-pong
router.GET("/", Ping)
// Authentication endpoints
router.POST("/login", InjectConfig(conf, api.Login))
router.POST("/login/facebook", InjectConfig(conf, api.LoginWithFacebook))
// Trips endpoints
router.GET("/trips", InjectConfig(conf, api.GetTripsList))
router.POST("/trips", InjectConfig(conf, api.CreateTrip))
router.GET("/trips/:id", InjectConfig(conf, api.GetTrip))
log.Println("Listening on 8080")
log.Fatal(http.ListenAndServe(":8080", router))
}