This repository has been archived by the owner on Jan 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
258 lines (213 loc) · 8.05 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/aljo242/chef"
"github.com/aljo242/ip_util"
"github.com/aljo242/shmeeload.xyz/handlers"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
)
const (
// DefaultConfigFile is the name of the user's config JSON file
DefaultConfigFile string = "sample/sample_config.json"
// TemplateBaseDir is where HTML template files are located to be
// executed and copied to the res dir
TemplateBaseDir string = "./web_res"
// TemplateOutputDir is the directory all outputs of SetupTemplates will fall under
TemplateOutputDir string = "./static"
)
var configFile string
func init() {
flag.StringVar(&configFile, "c", DefaultConfigFile, "Full path to JSON configuration file")
}
// SetupTemplates builds the template output directory, executes HTML templates,
// and copies all web resource files to the template output directory (.js, .ts, .js.map, .css, .html)
func SetupTemplates(cfg chef.ServerConfig) ([]string, error) {
files := make([]string, 0)
log.Debug().Msg("setting up templates")
log.Debug().Msg("cleaning output directory")
// clean static output dir
err := os.RemoveAll(TemplateOutputDir)
if err != nil {
return nil,
fmt.Errorf("error cleaning output directory %v : %w", TemplateOutputDir, err)
}
log.Debug().Str("OutputDir", TemplateOutputDir).Msg("creating new output directories")
// Create/ensure output directory
if err = EnsureDir(TemplateOutputDir); err != nil {
return nil, err
}
// Create subdirs
htmlOutputDir := filepath.Join(TemplateOutputDir, "html")
if err = EnsureDir(htmlOutputDir); err != nil {
return nil, err
}
jsOutputDir := filepath.Join(TemplateOutputDir, "js")
if err = EnsureDir(jsOutputDir); err != nil {
return nil, err
}
cssOutputDir := filepath.Join(TemplateOutputDir, "css")
if err = EnsureDir(cssOutputDir); err != nil {
return nil, err
}
tsOutputDir := filepath.Join(TemplateOutputDir, "src")
if err = EnsureDir(tsOutputDir); err != nil {
return nil, err
}
imgOutputDir := filepath.Join(TemplateOutputDir, "img")
if err = EnsureDir(imgOutputDir); err != nil {
return nil, err
}
modelOutputDir := filepath.Join(TemplateOutputDir, "model")
if err = EnsureDir(modelOutputDir); err != nil {
return nil, err
}
miscFilesOutputDir := filepath.Join(TemplateOutputDir, "files")
if err = EnsureDir(miscFilesOutputDir); err != nil {
return nil, err
}
log.Debug().Str("BaseDir", TemplateBaseDir).Msg("ensuring template base directory exists")
// Ensure base template directory exists
if !Exists(TemplateBaseDir) {
return nil,
fmt.Errorf("base Dir %v does not exist", TemplateBaseDir)
}
// walk through all files in the template resource dir
err = filepath.Walk(TemplateBaseDir,
func(path string, info os.FileInfo, err error) error {
// skip certain directories
if info.IsDir() && info.Name() == "node_modules" {
return filepath.SkipDir
}
handleCopyFileErr := func(err error) {
if err != nil {
log.Fatal().Err(err).Msg("error copying file")
}
}
handleExecuteTemlateErr := func(err error) {
if err != nil {
log.Fatal().Err(err).Msg("error executing HTML template")
}
}
switch filepath.Ext(path) {
case ".html":
newPath := filepath.Join(htmlOutputDir, filepath.Base(path))
log.Debug().Str("fromPath", path).Str("toPath", newPath).Msg("moving static web resources")
handleExecuteTemlateErr(ExecuteTemplateHTML(cfg, path, newPath))
case ".js", ".map":
newPath := filepath.Join(jsOutputDir, filepath.Base(path))
if filepath.Base(path) == "serviceWorker.js" || filepath.Base(path) == "serviceWorker.js.map" {
newPath = filepath.Join("./", filepath.Base(path))
}
log.Debug().Str("fromPath", path).Str("toPath", newPath).Msg("moving static web resources")
handleCopyFileErr(CopyFile(path, newPath))
case ".css":
newPath := filepath.Join(cssOutputDir, filepath.Base(path))
log.Debug().Str("fromPath", path).Str("toPath", newPath).Msg("moving static web resources")
handleCopyFileErr(CopyFile(path, newPath))
case ".ts":
newPath := filepath.Join(tsOutputDir, filepath.Base(path))
log.Debug().Str("fromPath", path).Str("toPath", newPath).Msg("moving static web resources")
handleCopyFileErr(CopyFile(path, newPath))
case ".ico", ".png", ".jpg", ".svg", ".gif":
newPath := filepath.Join(imgOutputDir, filepath.Base(path))
log.Debug().Str("fromPath", path).Str("toPath", newPath).Msg("moving static web resources")
handleCopyFileErr(CopyFile(path, newPath))
case ".pdf", ".doc", ".docx", ".xml":
newPath := filepath.Join(miscFilesOutputDir, filepath.Base(path))
log.Debug().Str("fromPath", path).Str("toPath", newPath).Msg("moving static web resources")
handleCopyFileErr(CopyFile(path, newPath))
case ".dae", ".obj", ".gltf":
newPath := filepath.Join(modelOutputDir, filepath.Base(path))
log.Debug().Str("fromPath", path).Str("toPath", newPath).Msg("moving static web resources")
handleCopyFileErr(CopyFile(path, newPath))
}
return nil
})
if err != nil {
return nil,
fmt.Errorf("error walking %v : %w", TemplateBaseDir, err)
}
log.Debug().Msg("template setup complete.")
return files, nil
}
func initServer() *chef.Server {
log.Printf("loading configuration in file: %v", configFile)
cfg, err := chef.LoadConfig(configFile)
if err != nil {
log.Fatal().Err(err).Msg("error loading config")
return nil
}
setupLogger(cfg)
cfg.Print()
var hostIP string
if cfg.ChooseIP {
h, err := ip_util.HostInfo()
if err != nil {
log.Fatal().Err(err).Msg("error creating Host Struct")
return nil
}
hostIP, err = ip_util.SelectHost(h.InternalIPs)
if err != nil {
log.Fatal().Err(err).Msg("error chosing host IP")
return nil
}
} else {
hostIP = cfg.IP
}
_, err = SetupTemplates(cfg)
if err != nil {
log.Fatal().Err(err).Msg("error setting up templates")
return nil
}
hub := newHub()
go hub.run()
addr := hostIP + ":" + cfg.Port
// generate/execute resource templates
// create new gorilla mux router
r := mux.NewRouter()
// attach pather with handler
r.HandleFunc("/home", handlers.HomeHandler(cfg.CacheMaxAge))
r.HandleFunc("/", handlers.RedirectHome())
r.HandleFunc("/static/js/{scriptname}", handlers.ScriptsHandler(cfg.CacheMaxAge))
r.HandleFunc("/static/css/{filename}", handlers.CSSHandler(cfg.CacheMaxAge))
r.HandleFunc("/static/html/{filename}", handlers.HTMLHandler(cfg.CacheMaxAge))
r.HandleFunc("/static/src/{filename}", handlers.TypeScriptHandler(cfg.CacheMaxAge))
r.HandleFunc("/static/img/{filename}", handlers.ImageHandler(cfg.CacheMaxAge))
r.HandleFunc("/static/model/{filename}", handlers.ModelHandler(cfg.CacheMaxAge))
r.HandleFunc("/manifest.json", handlers.ManifestHandler(cfg.CacheMaxAge))
r.HandleFunc("/serviceWorker.js", handlers.ServiceWorkerHandler(cfg.CacheMaxAge))
r.HandleFunc("/serviceWorker.js.map", handlers.ServiceWorkerHandler(cfg.CacheMaxAge))
r.HandleFunc("/tunes/home", handlers.RedirectConstructionHandler())
r.HandleFunc("/shop/home", handlers.RedirectConstructionHandler())
// r.HandleFunc("/chat/{name}", handlers.ChatHomeHandler("", cfg.DebugLog))
// CHAT HANDLERs
r.HandleFunc("/chat/home", handlers.ChatHomeHandler(cfg.CacheMaxAge))
r.HandleFunc("/chat/ws", serveWs(hub))
r.HandleFunc("/chat/signup", handlers.RedirectConstructionHandler())
r.HandleFunc("/chat/signin", handlers.RedirectConstructionHandler())
// file handler
r.HandleFunc("/files/{filename}", handlers.MiscFileHandler(cfg.CacheMaxAge))
// RESUME HANDLER
r.HandleFunc("/resume/home", handlers.ResumeHomeHandler(cfg.CacheMaxAge))
// UNDER CONSTRUCTION
r.HandleFunc("/under-construction", handlers.ConstructionHandler(cfg.CacheMaxAge))
// HALL OF ART
r.HandleFunc("/hall-of-art/home", handlers.HallofArtHomeHandler(cfg.CacheMaxAge))
// DONATE PAGES
r.HandleFunc("/donate/{cryptoname}", handlers.DonateHandler(cfg.CacheMaxAge))
fmt.Printf("\n")
log.Printf("starting Server at: %v...", addr)
srv := chef.NewServer(cfg, r)
return srv
}
func main() {
flag.Parse()
log.Printf("main: starting HTTP server...")
srv := initServer()
running := make(chan struct{})
srv.Run(running)
}