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

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

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

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

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