Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
dir-index-html
Commits
7ad16db0
Commit
7ad16db0
authored
4 years ago
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add a test server
fixes #30
parent
b08a78ab
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
91 additions
and
0 deletions
+91
-0
README.md
README.md
+12
-0
test/go.mod
test/go.mod
+3
-0
test/main.go
test/main.go
+76
-0
No files found.
README.md
View file @
7ad16db0
...
...
@@ -17,6 +17,18 @@ This repo is not be used standalone. It's used by the gateway code in go-ipfs. I
1.
Make changes to _both_ dir-index.html and dir-index-uncat.html.
2.
Follow the instructions in
[
go-ipfs
](
https://github.com/ipfs/go-ipfs/tree/master/assets#updating-dir-index-html
)
for updating the directory index.
## Testing
1.
Install
[
go
](
https://golang.org/dl/
)
.
2.
Run the test server:
```
bash
>
cd test
>
go run .
```
This will listen on
`localhost:3000`
and re-load the template every time you refresh the page.
## Contribute
Feel free to join in. All welcome. A good place to start is
[
the issues
](
https://github.com/ipfs/dir-index-html/issues
)
.
...
...
This diff is collapsed.
Click to expand it.
test/go.mod
0 → 100644
View file @
7ad16db0
module github.com/ipfs/dir-index-html/test
go 1.14
This diff is collapsed.
Click to expand it.
test/main.go
0 → 100644
View file @
7ad16db0
package
main
import
(
"fmt"
"net/http"
"net/url"
"os"
"text/template"
)
const
templateFile
=
"../dir-index.html"
// Copied from go-ipfs/core/corehttp/gateway_indexPage.go
type
listingTemplateData
struct
{
Listing
[]
directoryItem
Path
string
BackLink
string
Hash
string
}
type
directoryItem
struct
{
Size
string
Name
string
Path
string
}
var
testData
=
listingTemplateData
{
Listing
:
[]
directoryItem
{{
Size
:
"25 MiB"
,
Name
:
"short-film.mov"
,
Path
:
"short-film.mov"
,
},
{
Size
:
"1 KiB"
,
Name
:
"description.txt"
,
Path
:
"description.txt"
,
}},
Path
:
"/ipfs/QmFoo"
,
BackLink
:
"/.."
,
Hash
:
"QmFoo"
,
}
func
main
()
{
mux
:=
http
.
NewServeMux
()
mux
.
HandleFunc
(
"/"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
r
.
URL
.
Path
!=
"/"
{
http
.
Error
(
w
,
"Ha-ha, tricked you! There are no files here!"
,
http
.
StatusNotFound
)
return
}
listingTemplate
,
err
:=
template
.
New
(
"dir-index.html"
)
.
Funcs
(
template
.
FuncMap
{
"iconFromExt"
:
func
(
name
string
)
string
{
return
"ipfs-_blank"
// place-holder
},
"urlEscape"
:
func
(
rawUrl
string
)
string
{
pathUrl
:=
url
.
URL
{
Path
:
rawUrl
}
return
pathUrl
.
String
()
},
})
.
ParseFiles
(
templateFile
)
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"failed to parse template file: %s"
,
err
),
http
.
StatusInternalServerError
)
return
}
err
=
listingTemplate
.
Execute
(
w
,
&
testData
)
if
err
!=
nil
{
http
.
Error
(
w
,
fmt
.
Sprintf
(
"failed to execute template: %s"
,
err
),
http
.
StatusInternalServerError
)
return
}
w
.
WriteHeader
(
http
.
StatusOK
)
})
if
_
,
err
:=
os
.
Stat
(
templateFile
);
err
!=
nil
{
wd
,
_
:=
os
.
Getwd
()
fmt
.
Printf
(
"could not open template file %q, relative to %q: %s
\n
"
,
templateFile
,
wd
,
err
)
os
.
Exit
(
1
)
}
fmt
.
Printf
(
"listening on localhost:3000
\n
"
)
http
.
ListenAndServe
(
"localhost:3000"
,
mux
)
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment