-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
49 lines (35 loc) · 1.01 KB
/
main.go
File metadata and controls
49 lines (35 loc) · 1.01 KB
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
package main
import (
"net/http"
"strconv"
"gofr.dev/pkg/gofr"
"zop.dev/static-server/internal/config"
)
const defaultStaticFilePath = `./static`
const indexHTML = "/index.html"
const htmlExtension = ".html"
const rootPath = "/"
func main() {
app := gofr.New()
staticFilePath := app.Config.GetOrDefault("STATIC_DIR_PATH", defaultStaticFilePath)
spaMode, _ := strconv.ParseBool(app.Config.GetOrDefault("SPA_MODE", "false"))
defaultExtension := app.Config.GetOrDefault("DEFAULT_EXTENSION", htmlExtension)
handler := &staticFileHandler{
staticFilePath: staticFilePath,
spaMode: spaMode,
defaultExtension: defaultExtension,
}
app.OnStart(func(ctx *gofr.Context) error {
handler.fs = ctx.File
if err := config.HydrateFile(ctx.File, app.Config); err != nil {
ctx.Logger.Error(err.Error())
}
return nil
})
app.UseMiddleware(func(next http.Handler) http.Handler {
handler.next = next
return http.HandlerFunc(handler.ServeHTTP)
})
app.AddStaticFiles("/", staticFilePath)
app.Run()
}