You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
3.0 KiB
Go
127 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
)
|
|
|
|
//easily put attrs in a tag
|
|
type htmlAttrs map[string]string
|
|
func (attrs htmlAttrs) String() string {
|
|
var ret string
|
|
for k, v := range attrs {
|
|
ret += fmt.Sprintf("%s=\"%s\" ", k, url.QueryEscape(v))
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|
|
func wrapWithTagAttrs(str, tag string, a htmlAttrs) string {
|
|
return fmt.Sprintf("<%s %s>%s</%s>", tag, a.String(), str, tag)
|
|
}
|
|
|
|
func wrapWithTag(str string, tag string) string {
|
|
return fmt.Sprintf("<%s>%s</%s>", tag, str, tag)
|
|
}
|
|
|
|
//cleaner than making an attr for one href
|
|
func wrapWithTagHref(str string, tag string) string {
|
|
taggedString := fmt.Sprintf("<%s href=%s>%s</%s>", tag, url.QueryEscape(str), str, tag)
|
|
}
|
|
|
|
func serveStylesheet(w http.ResponseWriter, r *http.Request) {
|
|
sheet, err := os.Open("style.css")
|
|
if err != nil {
|
|
http.Error(w, "File not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
defer sheet.Close()
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename=style.css")
|
|
w.Header().Set("Content-Type", "text/css")
|
|
|
|
_, err = io.Copy(w, sheet)
|
|
if err != nil {
|
|
http.Error(w, "Error sending file", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
}
|
|
|
|
func main() {
|
|
port := flag.String("p", "6969", "port to serve on")
|
|
directory := flag.String("d", ".", "the directory of static file to host")
|
|
flag.Parse()
|
|
|
|
var contents string
|
|
entries, err := os.ReadDir(*directory)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
for _, e := range entries {
|
|
new := e.Name()
|
|
http.HandleFunc("/" + url.QueryEscape(new), func(w http.ResponseWriter, r *http.Request) {
|
|
log.Println("Request for resource: ", r.RequestURI)
|
|
|
|
//could also generate the css at runtime
|
|
if r.RequestURI == "/style.css" {
|
|
serveStylesheet(w, r)
|
|
return
|
|
}
|
|
|
|
file, err := os.Open(*directory + "/" + new)
|
|
if err != nil {
|
|
http.Error(w, "File not found.", http.StatusNotFound)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename=" + new)
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
_, err = io.Copy(w, file)
|
|
if err != nil {
|
|
http.Error(w, "Error sending file.", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
|
|
gridAttrs := htmlAttrs {
|
|
"class": "grid-item",
|
|
}
|
|
|
|
hrefAttrs := htmlAttrs {
|
|
"href": e.Name(),
|
|
}
|
|
|
|
var href string
|
|
href = wrapWithTagAttrs(e.Name(), "a", hrefAttrs)
|
|
contents += wrapWithTagAttrs(href, "div", gridAttrs)
|
|
contents += "\n"
|
|
}
|
|
|
|
gridContainerAttrs := htmlAttrs {
|
|
"class": "grid-container",
|
|
}
|
|
|
|
contents = wrapWithTagAttrs(contents, "div", gridContainerAttrs)
|
|
contents = wrapWithTag(contents, "pre")
|
|
contents = wrapWithTag("<link rel=\"stylesheet\" href=\"style.css\">", "head") + contents
|
|
contents = wrapWithTag(contents, "html")
|
|
contents = "<!DOCTYPE html>" + contents
|
|
|
|
handler := func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte(contents))
|
|
}
|
|
|
|
http.Handle("/", http.HandlerFunc(handler))
|
|
log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
|
|
log.Fatal(http.ListenAndServe(":"+*port, nil))
|
|
}
|