main.go 2.67 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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 {
15
	GatewayURL string
Steven Allen's avatar
Steven Allen committed
16
	Listing  []directoryItem
Kevin Neaton's avatar
Kevin Neaton committed
17
	Size     string
Steven Allen's avatar
Steven Allen committed
18
	Path     string
Kevin Neaton's avatar
Kevin Neaton committed
19
	Breadcrumbs []breadcrumb
Steven Allen's avatar
Steven Allen committed
20 21 22 23 24 25 26 27
	BackLink string
	Hash     string
}

type directoryItem struct {
	Size string
	Name string
	Path string
Kevin Neaton's avatar
Kevin Neaton committed
28
	Hash string
29
	ShortHash string
Steven Allen's avatar
Steven Allen committed
30 31
}

Kevin Neaton's avatar
Kevin Neaton committed
32 33 34 35 36 37
type breadcrumb struct {
	Name string
	Path string
}

var testPath = "/ipfs/QmFooBarQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7/a/b/c"
Steven Allen's avatar
Steven Allen committed
38
var testData = listingTemplateData{
39
	GatewayURL: "//localhost:3000",
Steven Allen's avatar
Steven Allen committed
40 41 42
	Listing: []directoryItem{{
		Size: "25 MiB",
		Name: "short-film.mov",
Kevin Neaton's avatar
Kevin Neaton committed
43
		Path: testPath + "/short-film.mov",
Kevin Neaton's avatar
Kevin Neaton committed
44
		Hash: "QmQuxBarQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7",
45
		ShortHash: "QmQu\u2026xDu7",
Steven Allen's avatar
Steven Allen committed
46 47
	}, {
		Size: "1 KiB",
Jessica Schilling's avatar
Jessica Schilling committed
48
		Name: "this-piece-of-papers-got-47-words-37-sentences-58-words-we-wanna-know.txt",
Kevin Neaton's avatar
Kevin Neaton committed
49
		Path: testPath + "/this-piece-of-papers-got-47-words-37-sentences-58-words-we-wanna-know.txt",
Kevin Neaton's avatar
Kevin Neaton committed
50
		Hash: "QmquXbaRQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7",
51
		ShortHash: "Qmqu\u2026xDu7",
Steven Allen's avatar
Steven Allen committed
52
	}},
Kevin Neaton's avatar
Kevin Neaton committed
53
	Size: "25 MiB",
Kevin Neaton's avatar
Kevin Neaton committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	Path: testPath,
	Breadcrumbs: []breadcrumb{{
		Name: "ipfs",
	}, {
		Name: "QmFooBarQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7",
		Path: testPath + "/../../..",
	}, {
		Name: "a",
		Path: testPath + "/../..",
	}, {
		Name: "b",
		Path: testPath + "/..",
	}, {
		Name: "c",
		Path: testPath,
	}},
	BackLink: testPath + "/..",
	Hash:     "QmFooBazBar2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7",
Steven Allen's avatar
Steven Allen committed
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
}

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)
}