-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (119 loc) · 4.28 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
package main
import (
"fmt"
"github.com/FabLabBerlin/localmachines/database/connect"
"github.com/FabLabBerlin/localmachines/models/invoices/invutil"
"github.com/FabLabBerlin/localmachines/models/machine"
"github.com/FabLabBerlin/localmachines/models/metrics/heatmap"
"github.com/FabLabBerlin/localmachines/routers"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/toolbox"
_ "github.com/go-sql-driver/mysql"
)
var runMode string
func main() {
runMode = beego.AppConfig.String("RunMode")
beego.Info("beego RunMode:", runMode)
configClients()
configDatabase()
routers.Init()
setupTasks()
toolbox.StartTask()
defer toolbox.StopTask()
// Config automatic API docs
beego.AppConfig.Set("DirectoryIndex", "true")
if runMode == "dev" {
beego.SetStaticPath("/swagger", "swagger")
}
// Config default files directory
beego.SetStaticPath("/files", "files")
// Routing https
beego.InsertFilter("/", beego.BeforeRouter, RedirectHttp) // for http://mysite
beego.Run()
}
var RedirectHttp = func(ctx *context.Context) {
HttpsEnabled, err := beego.AppConfig.Bool("EnableHttpTLS")
if HttpsEnabled && err == nil {
if !ctx.Input.IsSecure() {
url := "https://" + ctx.Input.Domain() + ":" + beego.AppConfig.String("HttpsPort") + ctx.Input.URI()
ctx.Redirect(302, url)
}
}
}
func configClients() {
// Allow access index.html file
beego.AppConfig.Set("DirectoryIndex", "true")
beego.Trace(runMode)
// Serve self-contained Angular JS applications depending on runmode
if runMode == "dev" {
beego.SetStaticPath("/admin/assets", "clients/admin/dev/assets")
beego.SetStaticPath("/admin/bower_components", "clients/admin/dev/bower_components")
beego.SetStaticPath("/admin/ng-components", "clients/admin/dev/ng-components")
beego.SetStaticPath("/admin/ng-main.js", "clients/admin/dev/ng-main.js")
beego.SetStaticPath("/admin/ng-modules", "clients/admin/dev/ng-modules")
beego.SetStaticPath("/machines/assets", "clients/machines/dev")
beego.SetStaticPath("/signup", "clients/signup/dev")
beego.SetStaticPath("/user", "clients/user/dev")
beego.SetStaticPath("/landing", "../localmachines-web")
} else { // prod and any other runmode
beego.SetStaticPath("/admin/assets", "clients/admin/prod/assets")
beego.SetStaticPath("/admin/ng-modules", "clients/admin/prod/ng-modules")
beego.SetStaticPath("/machines/assets", "clients/machines/prod")
beego.SetStaticPath("/signup", "clients/signup/prod")
beego.SetStaticPath("/user", "clients/user/prod")
beego.SetStaticPath("/landing", "../localmachines-web")
}
}
func configDatabase() {
// Get MySQL config from environment variables
mysqlUser := beego.AppConfig.String("mysqluser")
if mysqlUser == "" {
panic("Please set mysqluser in app.conf")
}
mysqlPass := beego.AppConfig.String("mysqlpass")
if mysqlPass == "" {
panic("Please set mysqlpass in app.conf")
}
mysqlHost := beego.AppConfig.String("mysqlhost")
if mysqlHost == "" {
mysqlHost = "localhost"
}
mysqlPort := beego.AppConfig.String("mysqlport")
if mysqlPort == "" {
mysqlPort = "3306"
}
mysqlDb := beego.AppConfig.String("mysqldb")
if mysqlDb == "" {
panic("Please set mysqldb in app.conf")
}
connect.Connect(mysqlUser, mysqlPass, mysqlHost, mysqlPort, mysqlDb)
}
// Setup Beego toolbox tasks. They are kind of cron jobs.
func setupTasks() {
fmt.Println("Starting tasks")
autoextendUserMembership := toolbox.NewTask("Autoextend User Membership",
" 0 0/53 * * * *",
invutil.TaskAutoExtend)
fetchLocalIps := toolbox.NewTask("Fetch Local IPs",
"0 0/2 * * * *",
machine.FetchLocalIpsTask)
calculateTotals := toolbox.NewTask("Calculate Invoice Totals",
" 0 0/50 * * * *",
invutil.TaskCalculateTotals)
fastbillSync := toolbox.NewTask("Sync Fastbill",
" 0 0/59 * * * *",
invutil.TaskFastbillSync)
pingNetswitches := toolbox.NewTask("Ping Netswitches",
" 0 0/10 * * * *",
machine.TaskPingNetswitches)
geoCodeUsers := toolbox.NewTask("Geocode",
" 0 0/2 * * * *",
heatmap.TaskGeoCodeUsers)
toolbox.AddTask("Autoextend User Membership", autoextendUserMembership)
toolbox.AddTask("Calculate Invoice Totals", calculateTotals)
toolbox.AddTask("Fetch Local IPs", fetchLocalIps)
toolbox.AddTask("Sync Fastbill", fastbillSync)
toolbox.AddTask("Ping Netswitches", pingNetswitches)
toolbox.AddTask("Geocode", geoCodeUsers)
}