main.go 2.74 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 {
Kevin Neaton's avatar
Kevin Neaton committed
15 16 17 18
	GatewayURL  string
	Listing     []directoryItem
	Size        string
	Path        string
Kevin Neaton's avatar
Kevin Neaton committed
19
	Breadcrumbs []breadcrumb
Kevin Neaton's avatar
Kevin Neaton committed
20 21
	BackLink    string
	Hash        string
Steven Allen's avatar
Steven Allen committed
22 23 24
}

type directoryItem struct {
Kevin Neaton's avatar
Kevin Neaton committed
25 26 27 28
	Size      string
	Name      string
	Path      string
	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
	Listing: []directoryItem{{
Kevin Neaton's avatar
Kevin Neaton committed
41 42 43 44
		Size:      "25 MiB",
		Name:      "short-film.mov",
		Path:      testPath + "/short-film.mov",
		Hash:      "QmQuxBarQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7",
45
		ShortHash: "QmQu\u2026xDu7",
Steven Allen's avatar
Steven Allen committed
46
	}, {
Kevin Neaton's avatar
Kevin Neaton committed
47 48 49 50
		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:      "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)
}