-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
164 lines (133 loc) · 3.71 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
160
161
162
163
164
package main
import (
"context"
"embed"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/JojiiOfficial/ZimWiki/handlers"
"github.com/JojiiOfficial/ZimWiki/zim"
"github.com/pelletier/go-toml"
log "github.com/sirupsen/logrus"
)
var (
//go:embed html/*
WebFS embed.FS
//go:embed locale.zip
LocaleByte []byte
)
type configStruct struct {
libPath string
address string
EnableSearchCache bool
SearchCacheDuration int
}
func main() {
setupLogger()
handlers.WebFS = WebFS
handlers.LocaleByte = LocaleByte
// Default configuration of ZimWiki
defaultConfig, _ := toml.Load(`
[Config]
LibraryPath = "./library"
Address = ":8080"
EnableSearchCache = "true"
SearchCacheDuration = "2"`)
// Load default configuration
libPath := defaultConfig.Get("Config.LibraryPath").(string)
address := defaultConfig.Get("Config.Address").(string)
EnableSearchCache, _ := strconv.ParseBool(defaultConfig.Get("Config.EnableSearchCache").(string))
SearchCacheDuration, _ := strconv.Atoi(defaultConfig.Get("Config.SearchCacheDuration").(string))
// Load configuration file
configData, err := toml.LoadFile("config.toml")
// If the configuration file has been successfully loaded
if err == nil {
// Load the configuration from the configuration file
configDataTree := configData.Get("Config").(*toml.Tree)
libPath = configDataTree.Get("LibraryPath").(string)
address = configDataTree.Get("Address").(string)
EnableSearchCache, _ = strconv.ParseBool(configDataTree.Get("EnableSearchCache").(string))
SearchCacheDuration, _ = strconv.Atoi((configDataTree.Get("SearchCacheDuration")).(string))
} else {
log.Error("Config.toml not found, default configuration will be used.")
}
config := configStruct{libPath: libPath, address: address, EnableSearchCache: EnableSearchCache, SearchCacheDuration: SearchCacheDuration}
handlers.EnableSearchCache = EnableSearchCache
handlers.SearchCacheDuration = SearchCacheDuration
if len(os.Args) > 1 {
config.libPath = os.Args[1]
}
// Verify library path
s, err := os.Stat(config.libPath)
if err != nil {
log.Errorf("Can't use '%s' as library path. %s", config.libPath, err)
return
}
if !s.IsDir() {
log.Error("Library must be a path!")
return
}
service := zim.New(config.libPath)
err = service.Start(config.libPath)
if err != nil {
log.Fatalln(err)
return
}
startServer(service, config)
}
func startServer(zimService *zim.Handler, config configStruct) {
router := NewRouter(zimService)
server := createServer(router, config)
// Start server
go func() {
err := server.ListenAndServe()
if err != http.ErrServerClosed {
log.Fatal(err)
}
}()
log.Info("Server started")
awaitExit(&server)
}
// Build a new Http server
func createServer(router http.Handler, config configStruct) http.Server {
return http.Server{
Addr: config.address,
Handler: router,
}
}
// Shutdown server gracefully
func awaitExit(httpServer *http.Server) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, os.Interrupt, syscall.SIGKILL, syscall.SIGTERM)
// await os signal
<-signalChan
// Create a deadline for the await
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
// Remove that ugly '^C'
fmt.Print("\r")
log.Info("Shutting down server")
if httpServer != nil {
err := httpServer.Shutdown(ctx)
if err != nil {
log.Warn(err)
}
log.Info("HTTP server shutdown complete")
}
log.Info("Shutting down complete")
os.Exit(0)
}
func setupLogger() {
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: false,
TimestampFormat: time.Stamp,
FullTimestamp: true,
ForceColors: true,
DisableColors: false,
})
}