This repository has been archived by the owner on Apr 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (70 loc) · 2.82 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"github.com/craigfurman/woodhouse-ci/builds"
"github.com/craigfurman/woodhouse-ci/db"
"github.com/craigfurman/woodhouse-ci/jobs"
"github.com/craigfurman/woodhouse-ci/runner"
"github.com/craigfurman/woodhouse-ci/vcs"
"github.com/craigfurman/woodhouse-ci/web"
"github.com/codegangsta/negroni"
)
func main() {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
must(err)
distBase := filepath.Join(dir, "..")
port := flag.Uint("port", 8080, "port to listen on")
templateDir := flag.String("templateDir", filepath.Join(distBase, "web", "templates"), "path to html templates")
storeDir := flag.String("storeDir", filepath.Join(distBase, "db"), "directory for saving persistent data")
buildsDir := flag.String("buildsDir", filepath.Join(distBase, "builds"), "directory for saving build output")
assetsDir := flag.String("assetsDir", filepath.Join(distBase, "web", "assets"), "path to static web assets")
gooseCmd := flag.String("gooseCmd", filepath.Join(distBase, "bin", "goose"), `path to "goose" database migration tool`)
debugMode := flag.Bool("debugMode", false, "do not parse templates up front. Only for development use")
flag.Parse()
bootMsg := ` _ _ _ _ _____ _____
| | | | | | | / __ \_ _|
| | | | ___ ___ __| | |__ ___ _ _ ___ ___ ______| / \/ | |
| |/\| |/ _ \ / _ \ / _` + "`" + ` | '_ \ / _ \| | | / __|/ _ \______| | | |
\ /\ / (_) | (_) | (_| | | | | (_) | |_| \__ \ __/ | \__/\_| |_
\/ \/ \___/ \___/ \__,_|_| |_|\___/ \__,_|___/\___| \____/\___/
`
fmt.Println(bootMsg)
if *debugMode {
log.Println("Starting in debug mode")
}
dbDir := filepath.Join(*storeDir, "sqlite")
must(os.MkdirAll(dbDir, 0755))
migrateCmd := exec.Command(*gooseCmd, "up")
migrateCmd.Dir = filepath.Join(*storeDir, "..")
must(migrateCmd.Run())
jobRepo, err := db.NewJobRepository(filepath.Join(dbDir, "store.db"))
must(err)
// Only Interrupt handled, as this is available on all major platforms and is the most common way of stopping Woodhouse-CI
exitChan := make(chan os.Signal)
signal.Notify(exitChan, os.Interrupt)
go func(c <-chan os.Signal) {
log.Printf("Caught signal %s. Closing database connections. Goodbye!\n", <-c)
must(jobRepo.Close())
os.Exit(0)
}(exitChan)
handler := web.New(&jobs.Service{
JobRepository: jobRepo,
Runner: runner.NewDockerRunner(vcs.GitCloner{}),
BuildRepository: builds.NewRepository(*buildsDir),
}, *templateDir, !*debugMode)
server := negroni.New(negroni.NewRecovery(), negroni.NewLogger(), negroni.NewStatic(http.Dir(*assetsDir)))
server.UseHandler(handler)
server.Run(fmt.Sprintf("0.0.0.0:%d", *port))
}
func must(err error) {
if err != nil {
panic(err)
}
}