generated from ipfs/ipfs-repository-template
/
main.go
205 lines (184 loc) · 5.6 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"embed"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"github.com/ipfs/boxo/gateway/assets"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime/multicodec"
"github.com/ipld/go-ipld-prime/node/basicnode"
mc "github.com/multiformats/go-multicodec"
// Ensure basic codecs are registered.
_ "github.com/ipld/go-ipld-prime/codec/cbor"
_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
_ "github.com/ipld/go-ipld-prime/codec/dagjson"
_ "github.com/ipld/go-ipld-prime/codec/json"
_ "github.com/ipld/go-ipld-prime/codec/raw"
)
//go:embed dag/*.block
var embeds embed.FS
const (
testPath = "/ipfs/QmFooBarQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7/a/b/c"
)
var directoryTestData = assets.DirectoryTemplateData{
GlobalData: assets.GlobalData{
Menu: []assets.MenuItem{{
URL: "http://example.com",
Title: "Support",
}},
GatewayURL: "//localhost:3000",
DNSLink: true,
},
Listing: []assets.DirectoryItem{{
Size: "25 MiB",
Name: "short-film.mov",
Path: testPath + "/short-film.mov",
Hash: "QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR",
ShortHash: "QmbW\u2026sMnR",
}, {
Size: "23 KiB",
Name: "250pxيوسف_الوزاني_صورة_ملتقطة_بواسطة_مرصد_هابل_الفضائي_توضح_سديم_السرطان،_وهو_بقايا_مستعر_أعظم._.jpg",
Path: testPath + "/250pxيوسف_الوزاني_صورة_ملتقطة_بواسطة_مرصد_هابل_الفضائي_توضح_سديم_السرطان،_وهو_بقايا_مستعر_أعظم._.jpg",
Hash: "QmUwrKrMTrNv8QjWGKMMH5QV9FMPUtRCoQ6zxTdgxATQW6",
ShortHash: "QmUw\u2026TQW6",
}, {
Size: "1 KiB",
Name: "this-piece-of-papers-got-47-words-37-sentences-58-words-we-wanna-know.txt",
Path: testPath + "/this-piece-of-papers-got-47-words-37-sentences-58-words-we-wanna-know.txt",
Hash: "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
ShortHash: "bafy\u2026bzdi",
}},
Size: "25 MiB",
Path: testPath,
Breadcrumbs: []assets.Breadcrumb{{
Name: "ipfs",
}, {
Name: "QmFooBarQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7",
Path: testPath + "/../../..",
}, {
Name: "a",
Path: testPath + "/../..",
}, {
Name: "b",
Path: testPath + "/..",
}, {
Name: "c",
Path: testPath,
}},
BackLink: testPath + "/..",
Hash: "QmFooBazBar2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7",
}
var dagTestData = map[string]*assets.DagTemplateData{}
func loadDagTestData() {
entries, err := embeds.ReadDir("dag")
if err != nil {
panic(err)
}
for _, entry := range entries {
cidStr := strings.TrimSuffix(entry.Name(), ".block")
cid, err := cid.Decode(cidStr)
if err != nil {
panic(err)
}
f, err := embeds.Open("dag/" + entry.Name())
if err != nil {
panic(err)
}
codec := cid.Prefix().Codec
decoder, err := multicodec.LookupDecoder(codec)
if err != nil {
panic(err)
}
node := basicnode.Prototype.Any.NewBuilder()
err = decoder(node, f)
if err != nil {
panic(err)
}
cidCodec := mc.Code(cid.Prefix().Codec)
dag, err := assets.ParseNode(node.Build())
if err != nil {
panic(err)
}
dagTestData[cid.String()] = &assets.DagTemplateData{
GlobalData: assets.GlobalData{
Menu: []assets.MenuItem{{
URL: "http://example.com",
Title: "Support",
}},
GatewayURL: "//localhost:3000",
DNSLink: true,
},
Path: "/ipfs/" + cid.String(),
CID: cid.String(),
CodecName: cidCodec.String(),
CodecHex: fmt.Sprintf("0x%x", uint64(cidCodec)),
Node: dag,
}
}
}
func init() {
// Append all types so we can preview the icons for all file types.
for ext := range assets.KnownIcons {
directoryTestData.Listing = append(directoryTestData.Listing, assets.DirectoryItem{
Size: "1 MiB",
Name: "file" + ext,
Path: testPath + "/" + "file" + ext,
Hash: "QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR",
ShortHash: "QmbW\u2026sMnR",
})
}
loadDagTestData()
}
func runTemplate(w http.ResponseWriter, filename string, data interface{}) {
fs := os.DirFS(".")
tpl, err := assets.BuildTemplate(fs, filename)
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError)
return
}
err = tpl.Execute(w, data)
if err != nil {
_, _ = w.Write([]byte(fmt.Sprintf("error during body generation: %v", err)))
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/dag":
cid := r.URL.Query().Get("cid")
if cid == "" {
cid = "bafyreiaocls5bt2ha5vszv5pwz34zzcdf3axk3uqa56bgsgvlkbezw67hq"
}
runTemplate(w, "dag.html", dagTestData[cid])
case "/directory":
runTemplate(w, "directory.html", directoryTestData)
case "/error":
statusCode, err := strconv.Atoi(r.URL.Query().Get("code"))
if err != nil {
statusCode = 500
}
runTemplate(w, "error.html", &assets.ErrorTemplateData{
GlobalData: assets.GlobalData{
Menu: []assets.MenuItem{{
URL: "http://example.com",
Title: "Support",
}},
},
StatusCode: statusCode,
StatusText: http.StatusText(statusCode),
Error: "this is the verbatim error: lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
})
case "/":
html := `<p>Test paths: <a href="/dag">DAG</a>, <a href="/directory">Directory</a>, <a href="/error?code=500">Error</a>.`
_, _ = w.Write([]byte(html))
default:
http.Redirect(w, r, "/", http.StatusSeeOther)
}
})
fmt.Printf("listening on http://localhost:3000/\n")
_ = http.ListenAndServe("localhost:3000", mux)
}