-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
65 lines (58 loc) · 1.45 KB
/
Copy pathhandler.go
File metadata and controls
65 lines (58 loc) · 1.45 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
package main
import (
"log/slog"
"net/http"
"strings"
)
type webHandler struct {
}
// ServeHTTP 用于HTTP服务
func (p *webHandler) ServeHTTP(rsp http.ResponseWriter, req *http.Request) {
slog.Debug("incoming request", "method", req.Method, "path", req.URL.Path, "remote", req.RemoteAddr)
if len(req.URL.Path) < len(basePath) {
rsp.Header()
http.Redirect(rsp, req, buildPathFromBase("/"), http.StatusFound)
return
}
absPath := getPathFromBase(req.URL.Path)
switch absPath {
case "/health":
handleHealth(rsp, req)
return
case "/":
handleProfileHome(rsp, req)
return
case "/favicon.ico":
rsp.WriteHeader(http.StatusNotFound)
return
}
if strings.HasPrefix(absPath, "/proxy/") {
handleProxy(rsp, req)
return
}
pathParts := strings.Split(absPath, "/")
var profileId string
if len(pathParts) >= 2 {
profileId = pathParts[1]
}
if len(profileId) <= 0 {
rsp.Header()
http.Redirect(rsp, req, buildPathFromBase("/"), http.StatusFound)
return
}
var pathHandleMap any
var ok bool
if pathHandleMap, ok = profileIdPathHandleMap.Load(profileId); !ok {
if !tryLoadProfile(profileId) {
rsp.WriteHeader(http.StatusNotFound)
return
}
if pathHandleMap, ok = profileIdPathHandleMap.Load(profileId); !ok {
slog.Error("handle still missing after load profile", "profileId", profileId)
rsp.WriteHeader(http.StatusNotFound)
return
}
}
handleProfile(rsp, req, profileId, pathHandleMap.(map[string]http.Handler))
return
}