forked from daptin/daptin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (76 loc) · 2.66 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
package main
import (
"flag"
"fmt"
"github.com/GeertJohan/go.rice"
"github.com/daptin/daptin/server"
"github.com/gin-gonic/gin"
"github.com/gocraft/health"
"github.com/jamiealquiza/envy"
"log"
"net/http"
"github.com/sadlil/go-trigger"
"os"
"syscall"
)
// Save the stream as a global variable
var stream = health.NewStream()
func init() {
//goagain.Strategy = goagain.Double
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
log.SetPrefix(fmt.Sprintf("Daptin Process ID: %d ", syscall.Getpid()))
}
func main() {
//eventEmitter := &emitter.Emitter{}
var db_type = flag.String("db_type", "sqlite3", "Database to use: sqlite3/mysql/postgres")
var connection_string = flag.String("db_connection_string", "daptin.db", "\n\tSQLite: test.db\n"+
"\tMySql: <username>:<password>@tcp(<hostname>:<port>)/<db_name>\n"+
"\tPostgres: host=<hostname> port=<port> user=<username> password=<password> dbname=<db_name> sslmode=enable/disable")
var webDashboardSource = flag.String("dashboard", "daptinweb/dist", "path to dist folder for daptin web dashboard")
//var assetsSource = flag.String("assets", "assets", "path to folder for assets")
var port = flag.String("port", ":6336", "Daptin port")
var runtimeMode = flag.String("runtime", "debug", "Runtime for Gin: debug, test, release")
gin.SetMode(*runtimeMode)
envy.Parse("DAPTIN") // looks for DAPTIN_PORT, DAPTIN_DASHBOARD, DAPTIN_DB_TYPE, DAPTIN_RUNTIME
flag.Parse()
stream.AddSink(&health.WriterSink{os.Stdout})
//assetsRoot, err := rice.FindBox("assets")
//resource.CheckErr(err, "Failed to open %s/static", assetsSource)
boxRoot1, err := rice.FindBox("daptinweb/dist/")
var boxRoot http.FileSystem
if err != nil {
log.Printf("Try loading web dashboard from: %v", *webDashboardSource)
//assetsStatic = http.Dir(*webDashboardSource + "/static")
boxRoot = http.Dir(*webDashboardSource)
} else {
//assetsStatic = assetsRoot.HTTPBox()
boxRoot = boxRoot1.HTTPBox()
}
db, err := server.GetDbConnection(*db_type, *connection_string)
if err != nil {
panic(err)
}
log.Printf("Connection acquired from database")
var hostSwitch server.HostSwitch
hostSwitch = server.Main(boxRoot, db)
rhs := RestartHandlerServer{
HostSwitch: &hostSwitch,
}
trigger.On("restart", func() {
log.Printf("Trigger restart")
hostSwitch = server.Main(boxRoot, db)
rhs.HostSwitch = &hostSwitch
})
log.Printf("Listening at port: %v", *port)
err = http.ListenAndServe(*port, &rhs)
if err != nil {
panic(err)
}
log.Printf("Why quit now ?")
}
type RestartHandlerServer struct {
HostSwitch *server.HostSwitch
}
func (rhs *RestartHandlerServer) ServeHTTP(rew http.ResponseWriter, req *http.Request) {
rhs.HostSwitch.ServeHTTP(rew, req)
}