/
main.go
61 lines (51 loc) · 1.32 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
package main
import (
"database/sql"
"log"
"net/http"
"os"
"github.com/jjenkins/heartkit/handlers"
_ "github.com/lib/pq"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"golang.org/x/oauth2"
)
func config(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
conf := &oauth2.Config{
ClientID: os.Getenv("FITBIT_CLIENT_ID"),
ClientSecret: os.Getenv("FITBIT_CLIENT_SECRET"),
Scopes: []string{"activity", "heartrate"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://www.fitbit.com/oauth2/authorize",
TokenURL: "https://api.fitbit.com/oauth2/token",
},
}
c.Env["oauth_config"] = conf
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func database(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
db, _ := sql.Open("postgres", os.Getenv("DATABASE_URL"))
err := db.Ping()
if err != nil {
log.Fatal(err)
}
c.Env["db"] = db
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func main() {
goji.Use(config)
goji.Use(database)
goji.Get("/", handlers.Root)
goji.Get("/authorize", handlers.Authorize)
goji.Get("/connected", handlers.Connected)
goji.Get("/callback", handlers.Callback)
goji.Post("/subscriber", handlers.Subscriber)
goji.NotFound(handlers.NotFound)
goji.Serve()
}