-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (63 loc) · 1.81 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
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"time"
"github.com/boltdb/bolt"
mailgun "gopkg.in/mailgun/mailgun-go.v1"
"github.com/robfig/cron"
"golang.org/x/oauth2"
"golang.org/x/oauth2/fitbit"
)
type config struct {
ClientID string `json:"clientId"`
ClientSecret string `json:"clientSecret"`
RedirectURL string `json:"redirectUrl"`
CronSchedule string `json:"cronSchedule"`
MailgunPrivateKey string `json:"mailgunPrivateKey"`
MailgunPublicKey string `json:"mailgunPublicKey"`
MailgunDomain string `json:"mailgunDomain"`
}
func genConfig() config {
cfgFile, err := os.Open("config.json")
defer cfgFile.Close()
if err != nil {
log.Fatalf("Could not open config.json file: %v\n", err)
}
var cfg config
err = json.NewDecoder(cfgFile).Decode(&cfg)
if err != nil {
log.Fatalf("Error reading config file: %v\n", err)
}
return cfg
}
func main() {
cfg := genConfig()
oAuthConf := &oauth2.Config{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
Scopes: []string{"profile", "sleep"},
Endpoint: fitbit.Endpoint,
RedirectURL: cfg.RedirectURL,
}
db, err := bolt.Open("users.db", 0600, &bolt.Options{Timeout: 5 * time.Second})
if err != nil {
log.Fatalf("Error opening database connection: %v\n", err)
}
store := NewBoltStore(db)
env := &Env{
OAuthConf: oAuthConf,
DB: store,
}
mg := mailgun.NewMailgun(cfg.MailgunDomain, cfg.MailgunPrivateKey, cfg.MailgunPublicKey)
c := cron.New()
setupCron(c, cfg.CronSchedule, oAuthConf, store, mg)
c.Start()
http.Handle("/", NewHandler(env, index))
http.Handle("/login", NewHandler(env, fitbitLogin))
http.Handle("/fitbitCallback", NewHandler(env, fitbitCallback))
log.Println("Started running on http://127.0.0.1:8080")
log.Println(http.ListenAndServe(":8080", nil))
}