Skip to content

Commit 93a4ddd

Browse files
committed
feat: add index.html
1 parent fd8e3b0 commit 93a4ddd

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

cmd/booster-http/server.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func (s *HttpServer) Start(ctx context.Context) {
6262
handler := http.NewServeMux()
6363
handler.HandleFunc(s.payloadBasePath(), s.handleByPayloadCid)
6464
handler.HandleFunc(s.pieceBasePath(), s.handleByPieceCid)
65+
handler.HandleFunc("/", s.handleIndex)
66+
handler.HandleFunc("/index.html", s.handleIndex)
6567
s.server = &http.Server{
6668
Addr: listenAddr,
6769
Handler: handler,
@@ -84,6 +86,40 @@ func (s *HttpServer) Stop() error {
8486
return s.server.Close()
8587
}
8688

89+
const idxPage = `
90+
<html>
91+
<body>
92+
<h4>Booster HTTP Server</h4>
93+
Endpoints:
94+
<table>
95+
<tbody>
96+
<tr>
97+
<td>
98+
Download a CAR file by payload CID
99+
</td>
100+
<td>
101+
<a href="/payload/payloadcid">/payload/<payload cid></a>
102+
</td>
103+
</tr>
104+
<tr>
105+
<td>
106+
Download a CAR file by piece CID
107+
</td>
108+
<td>
109+
<a href="/piece/piececid">/piece/<piece cid></a>
110+
</td>
111+
</tr>
112+
</tbody>
113+
</table>
114+
</body>
115+
</html>
116+
`
117+
118+
func (s *HttpServer) handleIndex(w http.ResponseWriter, r *http.Request) {
119+
w.WriteHeader(http.StatusOK)
120+
w.Write([]byte(idxPage)) //nolint:errcheck
121+
}
122+
87123
func (s *HttpServer) handleByPayloadCid(w http.ResponseWriter, r *http.Request) {
88124
prefixLen := len(s.payloadBasePath())
89125
if len(r.URL.Path) <= prefixLen {
@@ -92,7 +128,8 @@ func (s *HttpServer) handleByPayloadCid(w http.ResponseWriter, r *http.Request)
92128
return
93129
}
94130

95-
payloadCidStr := r.URL.Path[prefixLen:]
131+
fileName := r.URL.Path[prefixLen:]
132+
payloadCidStr := strings.Replace(fileName, ".car", "", 1)
96133
payloadCid, err := cid.Parse(payloadCidStr)
97134
if err != nil {
98135
msg := fmt.Sprintf("parsing payload CID '%s': %s", payloadCidStr, err.Error())

0 commit comments

Comments
 (0)