forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
135 lines (125 loc) · 3.54 KB
/
server.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
package uadmin
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/PesTospertnyj/uadmin/colors"
)
const welcomeMessage = "" +
` ___ __ _` + "\n" +
colors.FGBlueB + ` __ __` + colors.FGNormal + `/ | ____/ /___ ___ (_)___` + "\n" +
colors.FGBlueB + ` / / / /` + colors.FGNormal + ` /| |/ __ / __ '__ \/ / __ \` + "\n" +
colors.FGBlueB + `/ /_/ /` + colors.FGNormal + ` ___ / /_/ / / / / / / / / / /` + "\n" +
colors.FGBlueB + `\__,_/` + colors.FGNormal + `_/ |_\__,_/_/ /_/ /_/_/_/ /_/` + "\n\n"
// const w2 = `` +
// ` ______ __` + "\n" +
// ` /\ _ \ /\ \ __` + "\n" +
// colors.FGBlueB + ` __ __` + colors.FGNormal + `\ \ \L\ \ \_\ \ ___ ___ /\_\ ___` + "\n" +
// colors.FGBlueB + `/\ \/\ \` + colors.FGNormal + `\ \ __ \ /'_' \ /' __' __'\/\ \ /' _ '\` + "\n" +
// colors.FGBlueB + `\ \ \_\ \` + colors.FGNormal + `\ \ \/\ \/\ \L\ \/\ \/\ \/\ \ \ \/\ \/\ \` + "\n" +
// colors.FGBlueB + ` \ \____/` + colors.FGNormal + ` \ \_\ \_\ \___,_\ \_\ \_\ \_\ \_\ \_\ \_\` + "\n" +
// colors.FGBlueB + ` \/___/ ` + colors.FGNormal + ` \/_/\/_/\/__,_ /\/_/\/_/\/_/\/_/\/_/\/_/` + "\n"
// ServerReady is a variable that is set to true once the server is ready to use
var ServerReady = false
// StartServer !
func StartServer() {
if !registered {
Register()
}
if !settingsSynched {
syncSystemSettings()
}
if !handlersRegistered {
registerHandlers()
}
if val := getBindIP(); val != "" {
BindIP = val
}
if BindIP == "" {
BindIP = "0.0.0.0"
}
// Synch model translation
// Get Global Schema
stat := map[string]int{}
for _, v := range CustomTranslation {
tempStat := syncCustomTranslation(v)
for k, v := range tempStat {
stat[k] += v
}
}
for k := range Schema {
tempStat := syncModelTranslation(Schema[k])
for k, v := range tempStat {
stat[k] += v
}
}
for k, v := range stat {
complete := float64(v) / float64(stat["en"])
if complete != 1 {
Trail(WARNING, "Translation of %s at %.0f%% [%d/%d]", k, complete*100, v, stat["en"])
}
}
Trail(OK, "Server Started: http://%s:%d", BindIP, Port)
Trail(NONE, welcomeMessage)
dbOK = true
ServerReady = true
log.Println(http.ListenAndServe(fmt.Sprintf("%s:%d", BindIP, Port), nil))
}
// StartSecureServer !
func StartSecureServer(certFile, keyFile string) {
if !registered {
Register()
}
if !settingsSynched {
syncSystemSettings()
}
if !handlersRegistered {
registerHandlers()
}
if val := getBindIP(); val != "" {
BindIP = val
}
if BindIP == "" {
BindIP = "0.0.0.0"
}
// Synch model translation
// Get Global Schema
stat := map[string]int{}
for _, v := range CustomTranslation {
tempStat := syncCustomTranslation(v)
for k, v := range tempStat {
stat[k] += v
}
}
for k := range Schema {
tempStat := syncModelTranslation(Schema[k])
for k, v := range tempStat {
stat[k] += v
}
}
for k, v := range stat {
complete := float64(v) / float64(stat["en"])
if complete != 1 {
Trail(WARNING, "Translation of %s at %.0f%% [%d/%d]", k, complete*100, v, stat["en"])
}
}
Trail(OK, "Server Started: https://%s:%d\n", BindIP, Port)
Trail(NONE, welcomeMessage)
dbOK = true
ServerReady = true
log.Println(http.ListenAndServeTLS(fmt.Sprintf("%s:%d", BindIP, Port), certFile, keyFile, nil))
}
func getBindIP() string {
// Check if there is a bind ip file in the source code
ex, _ := os.Executable()
buf, err := ioutil.ReadFile(path.Join(filepath.Dir(ex), ".bindip"))
if err == nil {
return strings.Replace(string(buf), "\n", "", -1)
}
return ""
}