-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
90 lines (74 loc) · 2.63 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"text/template"
"github.com/WeTrustPlatform/blockform/model"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/joho/godotenv"
goji "goji.io"
"goji.io/pat"
)
// CloudProvider abstracts the behaviour of a cloud provider like AWS, Azure or
// Google Cloud. It exposes functions to create a virtual machine, install
// an ethereum node on it, and delete a virtual machine.
type CloudProvider interface {
CreateNode(context.Context, model.Node, func(string, string), func(error))
DeleteNode(context.Context, model.Node, func(), func(error))
}
var providers map[string]CloudProvider
var db *gorm.DB
var wg sync.WaitGroup
var tmpl *template.Template
func main() {
var err error
// prevent sideload .env in production
// https://github.com/joho/godotenv/issues/40
if os.Getenv("APP_ENV") != "production" {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
db, err = gorm.Open("postgres", os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
//db.DropTableIfExists(&model.Node{})
//db.DropTableIfExists(&model.Event{})
db.AutoMigrate(&model.Node{})
db.AutoMigrate(&model.Event{})
// terminate goroutines on sigint/sighup
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go checkSignals(sigs)
tmpl = template.Must(template.ParseGlob("templates/*"))
providers = makeProviders()
mux := goji.NewMux()
amux := goji.SubMux() // authenticated mux
amux.Use(basicAuth)
mux.HandleFunc(pat.Post("/node/:id/event/:apikey"), handleEvent)
mux.Handle(pat.New("/*"), amux)
fs := http.FileServer(http.Dir("static"))
amux.Handle(pat.Get("/static/*"), http.StripPrefix("/static/", fs))
amux.HandleFunc(pat.Get("/"), handleDashboard)
amux.HandleFunc(pat.Get("/create"), handleCreateForm)
amux.HandleFunc(pat.Post("/create"), handleCreate)
amux.HandleFunc(pat.Get("/node/:id/delete"), handleNodeDelete)
amux.HandleFunc(pat.Get("/node/:id/reboot"), handleReboot)
amux.HandleFunc(pat.Get("/node/:id/upgrade"), handleUpgrade)
amux.HandleFunc(pat.Get("/node/:id/status/:unit"), handleNodeStatus)
amux.HandleFunc(pat.Get("/node/:id/logs/:unit"), handleNodeLogs)
amux.HandleFunc(pat.Get("/node/:id/version"), handleNodeVersion)
amux.HandleFunc(pat.Post("/node/:id/certbot"), handleCertbot)
amux.HandleFunc(pat.Get("/node/:id"), handleNode)
amux.HandleFunc(pat.Get("/node/:id/:tab"), handleNodeTab)
amux.HandleFunc(pat.Get("/node/:nodeid/explorer/:class/:id"), handleNodeExplorer)
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), mux))
}