forked from applikatoni/applikatoni
/
main.go
155 lines (126 loc) · 4.93 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
149
150
151
152
153
154
155
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
"github.com/applikatoni/applikatoni/deploy"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
const VERSION = "1.0.0"
var (
outputVersion = flag.Bool("v", false, "output the version of Applikatoni")
configurationFilePath = flag.String("conf", "configuration.json", "path to configuration file")
port = flag.String("port", ":8080", "port to listen on")
databasePath = flag.String("db", "./db/development.db", "path to sqlite3 database file")
templatesPath = flag.String("templates", "./assets/templates", "path to template files")
)
var (
logRouter *deploy.LogRouter
config *Configuration
db *sql.DB
sessionStore *sessions.CookieStore
templates map[string]*template.Template
oauthCfg *oauth2.Config
killRegistry *KillRegistry
)
var (
sessionName = "applikatonisession"
templatesFiles = [][]string{
{"layout.tmpl", "hogan_templates.tmpl", "partials.tmpl", "home.tmpl"},
{"layout.tmpl", "hogan_templates.tmpl", "partials.tmpl", "toni_configuration.tmpl"},
{"layout.tmpl", "hogan_templates.tmpl", "partials.tmpl", "application.tmpl"},
{"layout.tmpl", "hogan_templates.tmpl", "partials.tmpl", "deployments.tmpl"},
{"layout.tmpl", "hogan_templates.tmpl", "partials.tmpl", "deployment.tmpl"},
}
)
func main() {
flag.Parse()
if *outputVersion {
fmt.Println(VERSION)
return
}
var err error
config, err = readConfiguration(*configurationFilePath)
if err != nil {
log.Fatal("could not read configuration", err)
}
templates, err = parseTemplates(*templatesPath, templatesFiles)
if err != nil {
log.Fatal("Parsing templates failed", err)
}
db, err = sql.Open("sqlite3", *databasePath)
if err != nil {
log.Fatal("could not open sqlite3 database file", err)
}
defer db.Close()
// If there are deployments in state 'new'/'active' when booting up
// Applikatoni probably crashed with a deployment running. Set these to
// 'failed' so we can start other deployments.
err = failUnfinishedDeployments(db)
if err != nil {
log.Fatal("setting unfinished deployments to 'failed' failed", err)
}
oauthCfg = &oauth2.Config{
ClientID: config.GitHubClientId,
ClientSecret: config.GitHubClientSecret,
Scopes: []string{"user", "repo"},
Endpoint: github.Endpoint,
}
// Setup the killRegistry to connect deployment managers to the kill button
killRegistry = NewKillRegistry()
// Run the daily digest sending in the background
go SendDailyDigests(db)
// Setup session store
sessionStore = sessions.NewCookieStore([]byte(config.SessionSecret))
// Initialize global LogRouter
logRouter = deploy.NewLogRouter()
defer logRouter.Stop()
logRouter.Start()
// Setup a basic listener that prints the logs of all deployments
logRouter.SubscribeAll(deploy.ConsoleLogger)
// Setup the listener that persists all log entries
logRouter.SubscribeAll(newLogEntrySaver(db))
// Setup the bugsnag deployment tracking
logRouter.SubscribeAll(newBugsnagNotifier(db))
// Setup the flowdock deployment notifcation
logRouter.SubscribeAll(newFlowdockNotifier(db))
// Setup the new relic deployment notifcation
logRouter.SubscribeAll(newNewRelicNotifier(db))
// Setup the router and the routes
r := mux.NewRouter()
// Assets
fsServer := http.FileServer(http.Dir("assets/"))
assetsServer := http.StripPrefix("/assets/", fsServer)
r.PathPrefix("/assets/").Handler(assetsServer)
r.Handle("/favicon.ico", fsServer)
// OAuth & Login
r.HandleFunc("/oauth2/authorize", oauth2authorizeHandler)
r.HandleFunc("/oauth2/callback", oauth2callbackHandler)
r.HandleFunc("/oauth2/logout", oauth2logoutHandler)
// Application
r.HandleFunc("/{application}/deployments", requireAuthorizedUser(createDeploymentHandler)).Methods("POST")
r.HandleFunc("/{application}/deployments", requireAuthorizedUser(listDeploymentsHandler)).Methods("GET")
r.HandleFunc("/{application}/deployments/{deploymentId}", requireAuthorizedUser(deploymentHandler)).Methods("GET")
r.HandleFunc("/{application}/deployments/{deploymentId}/log", requireAuthorizedUser(deploymentWsHandler)).Methods("GET")
r.HandleFunc("/{application}/deployments/{deploymentId}/kill", requireAuthorizedUser(killDeploymentHandler)).Methods("POST")
r.HandleFunc("/{application}/pulls", requireAuthorizedUser(pullRequestsHandler)).Methods("GET")
r.HandleFunc("/{application}/branches", requireAuthorizedUser(branchesHandler)).Methods("GET")
r.HandleFunc("/{application}/toni", requireAuthorizedUser(toniConfigurationHandler))
r.HandleFunc("/{application}", requireAuthorizedUser(applicationHandler))
// GET /
r.HandleFunc("/", authenticate(homeHandler))
log.Printf("Listening on localhost%s\n", *port)
err = http.ListenAndServe(*port, handlers.LoggingHandler(os.Stdout, r))
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}