-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (133 loc) · 4.3 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"
"time"
console "imuslab.com/arozos/mod/console"
)
/*
wdos
author: tobychui
To edit startup flags, see main.flag.go
To edit main routing logic, see main.router.go
To edit startup sequence, see startup.go
P.S. Try to keep this file < 300 lines
*/
// Close handler, close db and clearn up everything before exit
func SetupCloseHandler() {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
executeShutdownSequence()
}()
}
func executeShutdownSequence() {
//Shutdown authAgent
systemWideLogger.PrintAndLog("System", "<!> Shutting down auth gateway", nil)
authAgent.Close()
//Shutdown all storage pools
systemWideLogger.PrintAndLog("System", "<!> Shutting down storage pools", nil)
closeAllStoragePools()
//Shutdown Logger
systemWideLogger.Close()
//Shutdown database
systemWideLogger.PrintAndLog("System", "<!> Shutting down database", nil)
sysdb.Close()
//Shutdown network services
StopNetworkServices()
//Shutdown FTP Server
if FTPManager != nil {
systemWideLogger.PrintAndLog("System", "<!> Shutting down FTP Server", nil)
FTPManager.StopFtpServer()
}
//Cleaning up tmp files
systemWideLogger.PrintAndLog("System", "<!> Cleaning up tmp folder", nil)
os.RemoveAll(*tmp_directory)
//Do other things
os.Exit(0)
}
func main() {
//Parse startup flags and paramters
flag.Parse()
//Handle version printing
if *show_version {
fmt.Println("WDOS " + build_version + " Revision " + internal_version)
fmt.Println("Developed by tobychui and other co-developers, Licensed to " + deviceVendor)
//fmt.Println("THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.")
os.Exit(0)
}
//Handle flag assignments
max_upload_size = int64(*max_upload) << 20 //Parse the max upload size
if *demo_mode { //Disable hardware man under demo mode
enablehw := false
allow_hardware_management = &enablehw
}
//Clean up previous tmp files
final_tmp_directory := filepath.Clean(*tmp_directory) + "/tmp/"
tmp_directory = &final_tmp_directory
os.RemoveAll(*tmp_directory)
os.Mkdir(*tmp_directory, 0777)
//Print copyRight information
log.Println("WDOS(C) " + strconv.Itoa(time.Now().Year()) + " " + deviceVendor + ".")
log.Println("WDOS " + build_version + " Revision " + internal_version)
/*
New Implementation of the ArOZ Online System, Sept 2020
*/
RunStartup()
/*
Development build test execution
*/
Run_Test()
//Initiate all the static files transfer
fs := http.FileServer(http.Dir("./app"))
/*
if *enable_gzip {
//Gzip enabled. Always serve with gzip if header exists
//http.Handle("/", gzipmiddleware.Compress(mrouter(fs)))
http.Handle("/", mrouter(fs))
} else {
//Normal file server without gzip
http.Handle("/", mrouter(fs))
}
*/
//Updates 2022-09-06: Gzip handler moved inside the master router
http.Handle("/", mrouter(fs))
//Set database read write to ReadOnly after startup if demo mode
if *demo_mode {
sysdb.UpdateReadWriteMode(true)
}
//Setup handler for Ctrl +C
SetupCloseHandler()
//Start http server
go func() {
if *use_tls {
if !*disable_http {
go func() {
log.Println("Standard (HTTP) Web server listening at :" + strconv.Itoa(*listen_port))
http.ListenAndServe(":"+strconv.Itoa(*listen_port), nil)
}()
}
log.Println("Secure (HTTPS) Web server listening at :" + strconv.Itoa(*tls_listen_port))
http.ListenAndServeTLS(":"+strconv.Itoa(*tls_listen_port), *tls_cert, *tls_key, nil)
} else {
log.Println("Web server listening at :" + strconv.Itoa(*listen_port))
http.ListenAndServe(":"+strconv.Itoa(*listen_port), nil)
}
}()
if *enable_console == true {
//Startup interactive shell for debug and basic controls
Console := console.NewConsole(consoleCommandHandler)
Console.ListenAndHandle()
} else {
//Just do a blocking loop here
select {}
}
}