-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
106 lines (89 loc) · 2.75 KB
/
Copy pathmain.go
File metadata and controls
106 lines (89 loc) · 2.75 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
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
package main
import (
"log/slog"
"net/http"
"os"
"path"
"printloop/internal/webserver"
"strconv"
)
func main() {
initLogger()
// Initialize translations
err := webserver.LoadTranslations()
if err != nil {
slog.Error("Failed to load translations:", "err", err)
return
}
err = os.MkdirAll("files", 0755)
if err != nil {
slog.Error("Failed to create files directory:", "err", err)
return
}
err = os.MkdirAll("files/uploads", 0755)
if err != nil {
slog.Error("Failed to create files/uploads directory:", "err", err)
return
}
err = os.MkdirAll("files/results", 0755)
if err != nil {
slog.Error("Failed to create files/results directory:", "err", err)
return
}
mux := http.NewServeMux()
// Setup routes
mux.HandleFunc("/", webserver.HomeHandler)
mux.HandleFunc("POST /upload", webserver.UploadHandler)
mux.HandleFunc("/template", webserver.TemplateHandler)
mux.HandleFunc("/hint", webserver.HintHandler)
// Serve static files from embedded FS
mux.Handle("/www/", http.StripPrefix("/www/", webserver.StaticFileServer()))
// Favicon routes - serve from embedded www directory
mux.HandleFunc("/favicon.ico", webserver.FaviconHandler("www/favicon.ico"))
mux.HandleFunc("/favicon-16x16.png", webserver.FaviconHandler("www/favicon-16x16.png"))
mux.HandleFunc("/favicon-32x32.png", webserver.FaviconHandler("www/favicon-32x32.png"))
mux.HandleFunc("/favicon-180x180.png", webserver.FaviconHandler("www/favicon-180x180.png"))
mux.HandleFunc("/favicon-192x192.png", webserver.FaviconHandler("www/favicon-192x192.png"))
mux.HandleFunc("/favicon-512x512.png", webserver.FaviconHandler("www/favicon-512x512.png"))
handler := webserver.CompressionMiddleware(mux)
handler = webserver.LogPageRef(handler)
slog.Info("Server started on port :8080")
slog.Info("Open http://localhost:8080 in your browser")
err = http.ListenAndServe(":8080", handler)
if err != nil {
slog.Error("Server startup error", "err", err)
return
}
}
func initLogger() {
const useJSON = true
var handler slog.Handler
if !useJSON {
handler = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelInfo,
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == slog.SourceKey {
s := a.Value.Any().(*slog.Source)
s.File = path.Base(s.File)
}
return a
},
})
} else {
handler = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelInfo,
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
if a.Key == slog.SourceKey {
s := a.Value.Any().(*slog.Source)
r := slog.String(slog.SourceKey, " "+path.Base(s.File)+":"+strconv.Itoa(s.Line)+" ")
return r
}
return a
},
})
}
logger := slog.New(handler)
slog.SetDefault(logger)
}