main.go 3.3 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1 2 3 4 5 6 7 8 9 10 11 12
package main

import (
	"fmt"
	"net/http"
	"net/url"
	"os"
	"text/template"
)

const templateFile = "../dir-index.html"

tavit ohanian's avatar
tavit ohanian committed
13
// Copied from go-dms3/core/corehttp/gateway_indexPage.go
Steven Allen's avatar
Steven Allen committed
14
type listingTemplateData struct {
Kevin Neaton's avatar
Kevin Neaton committed
15
	GatewayURL  string
Marcin Rataj's avatar
Marcin Rataj committed
16
	DNSLink     bool
Kevin Neaton's avatar
Kevin Neaton committed
17 18 19
	Listing     []directoryItem
	Size        string
	Path        string
Kevin Neaton's avatar
Kevin Neaton committed
20
	Breadcrumbs []breadcrumb
Kevin Neaton's avatar
Kevin Neaton committed
21 22
	BackLink    string
	Hash        string
Steven Allen's avatar
Steven Allen committed
23 24 25
}

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

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

tavit ohanian's avatar
tavit ohanian committed
38
var testPath = "/dms3/QmFooBarQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7/a/b/c"
Steven Allen's avatar
Steven Allen committed
39
var testData = listingTemplateData{
40
	GatewayURL: "//localhost:3000",
Marcin Rataj's avatar
Marcin Rataj committed
41
	DNSLink:    true,
Steven Allen's avatar
Steven Allen committed
42
	Listing: []directoryItem{{
Kevin Neaton's avatar
Kevin Neaton committed
43 44 45
		Size:      "25 MiB",
		Name:      "short-film.mov",
		Path:      testPath + "/short-film.mov",
Marcin Rataj's avatar
Marcin Rataj committed
46 47 48 49 50 51 52 53
		Hash:      "QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR",
		ShortHash: "QmbW\u2026sMnR",
	}, {
		Size:      "23 KiB",
		Name:      "250pxيوسف_الوزاني_صورة_ملتقطة_بواسطة_مرصد_هابل_الفضائي_توضح_سديم_السرطان،_وهو_بقايا_مستعر_أعظم._.jpg",
		Path:      testPath + "/250pxيوسف_الوزاني_صورة_ملتقطة_بواسطة_مرصد_هابل_الفضائي_توضح_سديم_السرطان،_وهو_بقايا_مستعر_أعظم._.jpg",
		Hash:      "QmUwrKrMTrNv8QjWGKMMH5QV9FMPUtRCoQ6zxTdgxATQW6",
		ShortHash: "QmUw\u2026TQW6",
Steven Allen's avatar
Steven Allen committed
54
	}, {
Kevin Neaton's avatar
Kevin Neaton committed
55 56 57
		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",
Marcin Rataj's avatar
Marcin Rataj committed
58 59
		Hash:      "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi",
		ShortHash: "bafy\u2026bzdi",
Steven Allen's avatar
Steven Allen committed
60
	}},
Kevin Neaton's avatar
Kevin Neaton committed
61
	Size: "25 MiB",
Kevin Neaton's avatar
Kevin Neaton committed
62 63
	Path: testPath,
	Breadcrumbs: []breadcrumb{{
tavit ohanian's avatar
tavit ohanian committed
64
		Name: "dms3",
Kevin Neaton's avatar
Kevin Neaton committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	}, {
		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
80 81 82 83 84 85 86 87 88 89 90
}

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 {
tavit ohanian's avatar
tavit ohanian committed
91
				return "dms3-_blank" // place-holder
Steven Allen's avatar
Steven Allen committed
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
			},
			"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)
}