forked from refiito/pipes-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
123 lines (109 loc) · 2.94 KB
/
server.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
package main
import (
"code.google.com/p/goauth2/oauth"
"encoding/json"
"fmt"
"github.com/bugsnag/bugsnag-go"
"github.com/tambet/oauthplain"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
)
var (
urls = struct {
ReturnURL map[string]string `json:"return_url"`
TogglAPIHost map[string]string `json:"toggl_api_host"`
PipesAPIHost map[string]string `json:"pipes_api_host"`
CorsWhitelist map[string][]string `json:"cors_whitelist"`
}{}
availableAuthorizations = map[string]string{}
oAuth2Configs map[string]*oauth.Config
oAuth1Configs map[string]*oauthplain.Config
availableIntegrations []*Integration
pipeType *regexp.Regexp
serviceType *regexp.Regexp
)
func main() {
InitFlags()
runtime.GOMAXPROCS(runtime.NumCPU())
bugsnag.Configure(bugsnag.Configuration{
APIKey: bugsnagAPIKey,
ReleaseStage: environment,
NotifyReleaseStages: []string{"production", "staging"},
// more configuration options
})
db = connectDB(dbConnString)
defer db.Close()
loadIntegrations()
b, err := ioutil.ReadFile(filepath.Join(workdir, "config", "urls.json"))
if err != nil {
log.Fatal(err)
}
if err := json.Unmarshal(b, &urls); err != nil {
log.Fatal(err)
}
b, err = ioutil.ReadFile(filepath.Join(workdir, "config", "oauth2.json"))
if err != nil {
log.Fatal(err)
}
if err := json.Unmarshal(b, &oAuth2Configs); err != nil {
log.Fatal(err)
}
b, err = ioutil.ReadFile(filepath.Join(workdir, "config", "oauth1.json"))
if err != nil {
log.Fatal(err)
}
if err := json.Unmarshal(b, &oAuth1Configs); err != nil {
log.Fatal(err)
}
for _, integration := range availableIntegrations {
availableAuthorizations[integration.ID] = integration.AuthType
}
rand.Seed(time.Now().Unix())
if environment == "production" {
go autoSyncRunner()
}
if environment == "staging" {
go autoSyncRunnerStub()
}
go autoSyncQueuer()
listenAddress := fmt.Sprintf(":%d", port)
log.Printf(
"pipes (PID: %d) is starting on %s\n=> Ctrl-C to shutdown server\n",
os.Getpid(),
listenAddress)
log.Fatal(http.ListenAndServe(listenAddress, http.DefaultServeMux))
}
func loadIntegrations() {
b, err := ioutil.ReadFile(filepath.Join(workdir, "config", "integrations.json"))
if err != nil {
log.Fatal(err)
}
if err := json.Unmarshal(b, &availableIntegrations); err != nil {
log.Fatal(err)
}
ids := make([]string, len(availableIntegrations))
for i := range availableIntegrations {
ids = append(ids, availableIntegrations[i].ID)
}
serviceType = regexp.MustCompile(strings.Join(ids, "|"))
pipeType = regexp.MustCompile("users|projects|todolists|todos|tasks|timeentries")
}
func isWhiteListedCorsOrigin(r *http.Request) (string, bool) {
origin := r.Header.Get("Origin")
if allowedDomains, exist := urls.CorsWhitelist[environment]; exist {
for _, s := range allowedDomains {
if s == origin {
return origin, true
}
}
}
return "", false
}