MCPcopy
hub / github.com/chubin/wttr.in / staticFilesHandler

Function staticFilesHandler

internal/server/server.go:58–100  ·  view source on GitHub ↗

staticFilesHandler serves embedded files under /files/

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

56
57// staticFilesHandler serves embedded files under /files/
58func staticFilesHandler(w http.ResponseWriter, r *http.Request) {
59 // Remove /files/ prefix
60 filePath := strings.TrimPrefix(r.URL.Path, "/files/")
61 if filePath == "" || filePath == "/" {
62 http.NotFound(w, r)
63 return
64 }
65
66 // Clean the path to prevent directory traversal
67 filePath = path.Clean(filePath)
68 if strings.HasPrefix(filePath, "..") {
69 http.NotFound(w, r)
70 return
71 }
72
73 embedPath := "share/static/" + filePath
74 data, err := assets.GetFile(embedPath)
75 if err != nil {
76 log.Println(err)
77 http.NotFound(w, r)
78 return
79 }
80
81 // Set proper Content-Type
82 contentType := "application/octet-stream"
83 switch path.Ext(filePath) {
84 case ".css":
85 contentType = "text/css; charset=utf-8"
86 case ".js":
87 contentType = "application/javascript; charset=utf-8"
88 case ".png":
89 contentType = "image/png"
90 case ".ico":
91 contentType = "image/x-icon"
92 case ".html":
93 contentType = "text/html; charset=utf-8"
94 }
95
96 w.Header().Set("Content-Type", contentType)
97 w.Header().Set("Cache-Control", "public, max-age=86400")
98 w.WriteHeader(http.StatusOK)
99 _, _ = w.Write(data)
100}
101
102// panicRecovery is a middleware that recovers from panics in handlers
103func panicRecovery(next http.HandlerFunc) http.HandlerFunc {

Callers

nothing calls this directly

Calls 3

GetFileFunction · 0.92
WriteMethod · 0.80
SetMethod · 0.65

Tested by

no test coverage detected