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
|
package render
import (
"fmt"
"log/slog"
"net/http"
"strings"
settings "physick.ru/Settings"
)
const indexTemplateName string = "index.html"
func RegisterEndpoints() {
// http.Handle("static/", http.FileServer(http.FS(os.DirFS(settings.Current.StaticLocation))))
http.HandleFunc("/", mux)
}
func mux(w http.ResponseWriter, r *http.Request) {
slog.Info("Request", slog.String("URL", r.URL.String()))
if r.URL.String() == "/favicon.ico" {
http.ServeFile(w, r, fmt.Sprintf("%s/favicon.ico", settings.Current.StaticLocation))
}
if strings.HasPrefix(r.URL.String(), "/static") {
var filePath = strings.Replace(r.URL.String(), "/static", settings.Current.StaticLocation, 1)
http.ServeFile(w, r, filePath)
return
}
render(w, r)
}
func render(w http.ResponseWriter, r *http.Request) {
var indexTempl, readErr = getTemplateString(indexTemplateName)
if readErr != nil {
slog.Warn("Failed to load template", slog.Any("Error", readErr))
w.WriteHeader(http.StatusInternalServerError)
return
}
slog.Info("Requested a template")
w.Write([]byte(indexTempl))
}
|