generated from jackyzha0/quartz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
129 lines (113 loc) · 2.8 KB
/
server.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
package main
import (
"flag"
"fmt"
"log"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
var allowedExtensions = map[string]bool{
".html": true,
".css": true,
".js": true,
".json": true,
".xml": true,
".mp4": true,
".webp": true,
".txt": true,
".md": true,
".jpg": true,
".jpeg": true,
".png": true,
".gif": true,
".svg": true,
".ico": true,
".pdf": true,
".woff": true,
".woff2": true,
".go": true,
".py": true,
}
func main() {
port := flag.Int("port", 8080, "Port to serve on")
dir := flag.String("dir", "public", "Directory to serve files from")
flag.Parse()
publicDir, err := filepath.Abs(*dir)
if err != nil {
log.Fatal(err)
}
if _, err := os.Stat(publicDir); os.IsNotExist(err) {
log.Fatalf("Directory %s does not exist", publicDir)
}
// Register custom MIME types
mime.AddExtensionType(".woff2", "font/woff2")
mime.AddExtensionType(".woff", "font/woff")
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Clean the URL path
urlPath := path.Clean(r.URL.Path)
// Remove trailing slash except for root
if urlPath != "/" {
urlPath = strings.TrimSuffix(urlPath, "/")
}
// Construct full file path
fullPath := filepath.Join(publicDir, filepath.FromSlash(urlPath))
// Get file info
fi, err := os.Stat(fullPath)
if err != nil {
if os.IsNotExist(err) {
// Try adding .html extension
htmlPath := fullPath + ".html"
if fi, err = os.Stat(htmlPath); err == nil {
fullPath = htmlPath
} else {
// Try index.html for directory-style URLs
indexPath := filepath.Join(fullPath, "index.html")
if fi, err = os.Stat(indexPath); err == nil {
fullPath = indexPath
} else {
http.NotFound(w, r)
return
}
}
} else {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
// Check if path is a directory
if fi.IsDir() {
indexPath := filepath.Join(fullPath, "index.html")
if _, err := os.Stat(indexPath); err == nil {
fullPath = indexPath
} else {
http.NotFound(w, r)
return
}
}
// Verify file extension
ext := strings.ToLower(filepath.Ext(fullPath))
if !allowedExtensions[ext] {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
// Set appropriate headers
w.Header().Set("Cache-Control", "public, max-age=31536000")
w.Header().Set("Vary", "Accept-Encoding")
// Detect and set content type
contentType := mime.TypeByExtension(ext)
if contentType != "" {
w.Header().Set("Content-Type", contentType)
}
// Serve the file
http.ServeFile(w, r, fullPath)
})
addr := fmt.Sprintf(":%d", *port)
log.Printf("Serving files from %s on http://localhost%s", publicDir, addr)
if err := http.ListenAndServe(addr, handler); err != nil {
log.Fatal(err)
}
}