small pixel drawing of a pufferfish useposix.sh

main.go

package main

import (
	"fmt"
	"log"
	"net/http"
	"path/filepath"
	"html/template"
	"os"
)

func main() {
	http.HandleFunc("/", rootHandler)

	log.Println("listening on :7368 tbh")
	err := http.ListenAndServe(":7368", nil)
	if err != nil {
		log.Fatal(err)
	}
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path == "/" {
		r.URL.Path = "index.html"
	}

	t := filepath.Join("bin", filepath.Clean(r.URL.Path))

	_, err := os.Stat(t)
	if err != nil {
		fmt.Fprintf(w, err.Error())
		return
	}
	tmpl, err := template.ParseFiles(t)
	if err != nil {
		fmt.Fprintf(w, err.Error())
		return
	}
	err = tmpl.ExecuteTemplate(w, "wat", nil)
	if err != nil {
		fmt.Fprintf(w, err.Error())
	}
}