-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
148 lines (131 loc) · 3.86 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"time"
"gopkg.in/olivere/elastic.v3"
log "github.com/cihub/seelog"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/jpillora/go-ogle-analytics"
httpr "github.com/julienschmidt/httprouter"
"github.com/ok-borg/api/conf"
"github.com/ok-borg/api/endpoints"
"github.com/ok-borg/api/sitemap"
"github.com/ok-borg/api/v"
"github.com/ok-borg/api/v/v1"
"github.com/ok-borg/api/v/v2"
"github.com/rs/cors"
"golang.org/x/oauth2"
)
const (
githubTokenURL = "https://github.com/login/oauth/access_token"
)
var (
esAddr = flag.String("esaddr", "127.0.0.1:9200", "Elastic Search address")
githubClientId = flag.String("github-client-id", "", "Github oauth client id")
githubClientSecret = flag.String("github-client-secret", "", "Github client secret")
sm = flag.String("sitemap", "", "Sitemap location. Leave empty if you don't want a sitemap to be generated")
analytics = flag.String("analytics", "", "Analytics tracking id")
sqlAddr = flag.String("sqladdr", "127.0.0.1:3306", "Mysql address")
sqlIds = flag.String("sqlids", "root:root", "Mysql identifier")
)
var (
client *elastic.Client
analyticsClient *ga.Client
ep *endpoints.Endpoints
db *gorm.DB
)
func initWithConfFile() {
// let's assume the conf file is in the same place than the benary.
c, err := ioutil.ReadFile(".borg.conf.json")
if err != nil {
// just return, there is probably not configuration file
return
}
var conf conf.Conf
if err := json.Unmarshal(c, &conf); err != nil {
panic(fmt.Sprintf("[initWithConfFile] invalid config format: %s", err.Error()))
}
if conf.EsAddr != "" {
*esAddr = conf.EsAddr
}
if conf.Github.ClientId != "" {
*githubClientId = conf.Github.ClientId
}
if conf.Github.ClientSecret != "" {
*githubClientSecret = conf.Github.ClientSecret
}
if conf.Sitemap != "" {
*sm = conf.Sitemap
}
if conf.Analytics != "" {
*analytics = conf.Analytics
}
if conf.Mysql.Addr != "" {
*sqlAddr = conf.Mysql.Addr
}
if conf.Mysql.Ids != "" {
*sqlIds = conf.Mysql.Ids
}
}
func init() {
// read config file before if it exists, so we can replaces the var that was set with the cmdline
// the cmdline is allowed to overwrite the config file.
initWithConfFile()
flag.Parse()
cl, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(fmt.Sprintf("http://%v", *esAddr)))
if err != nil {
panic(err)
}
client = cl
if len(*analytics) > 0 {
acl, err := ga.NewClient(*analytics)
if err != nil {
log.Errorf("Failed to acquire analytics client id: %v", err)
}
analyticsClient = acl
}
}
func main() {
oauthCfg := &oauth2.Config{
ClientID: *githubClientId,
ClientSecret: *githubClientSecret,
Endpoint: oauth2.Endpoint{
TokenURL: githubTokenURL,
},
Scopes: []string{"read:org"},
}
// init mysql
var err error
dsn := fmt.Sprintf("%s@tcp(%s)/borg?parseTime=True", *sqlIds, *sqlAddr)
if db, err = gorm.Open("mysql", dsn); err != nil {
panic(fmt.Sprintf("[init] unable to initialize gorm: %s", err.Error()))
}
defer db.Close()
ep = endpoints.NewEndpoints(oauthCfg, client, analyticsClient, db)
r := httpr.New()
if len(*sm) > 0 {
go sitemapLoop(*sm, client)
}
// decl routes
common.Init(client, analyticsClient, ep, db, *githubClientId)
v1.Init(r, client, analyticsClient, ep, db)
v2.Init(r, client, analyticsClient, ep, db)
handler := cors.New(cors.Options{AllowedHeaders: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"}}).Handler(r)
log.Info("Starting http server")
log.Critical(http.ListenAndServe(fmt.Sprintf(":%v", 9992), handler))
}
func sitemapLoop(path string, client *elastic.Client) {
first := true
for {
if !first {
time.Sleep(30 * time.Minute)
}
first = false
sitemap.GenerateSitemap(path, client)
}
}