package main import ( "log" "os" "io" "strings" "encoding/json" "html/template" "net/http" "git.sr.ht/~jordyn/imagination/figtree" ) func main() { fs := http.FileServer(http.Dir(figtree.FIGS)) http.Handle("/imagination/", http.StripPrefix("/imagination/", fs)) css := http.FileServer(http.Dir("css/")) http.Handle("/style/", http.StripPrefix("/style/", css)) http.HandleFunc("/", homeHandler) http.HandleFunc("/fig/", figHandler) http.HandleFunc("/all/", seeFigs) http.HandleFunc("/dry/", dryHandler) log.Fatal(http.ListenAndServe(":8080", nil)) } func homeHandler(w http.ResponseWriter, r *http.Request) { page := template.Must(template.ParseFiles("pages/home.html")) if err := page.Execute(w, nil); err != nil { panic(err) } } func figHandler(w http.ResponseWriter, r *http.Request) { r.ParseMultipartForm(32 << 20) var figstuff io.Reader file, _, err := r.FormFile("newfigfile") if err != nil { figtxt := r.FormValue("newfigtxt") if len(figtxt) == 0 { panic(err) } figstuff = strings.NewReader(figtxt) } else { defer file.Close() } fighome, newfig, err := figtree.HarvestFig( r.FormValue("name"), r.FormValue("desc"), r.FormValue("type"), r.FormValue("ext"), r.FormValue("birth")) log.Println("blooming " + fighome) f, err := os.Create(fighome) if err != nil { panic(err) } err = os.Chmod(fighome, 0666) if err != nil { panic(err) } defer f.Close() if file != nil { io.Copy(f, file) } else { io.Copy(f, figstuff) } figtree.DryFig(newfig) page := template.Must(template.ParseFiles("pages/driedfig.html")) fancyfig := newfig.Fancy() if err := page.Execute(w, fancyfig); err != nil { panic(err) } } func allHandler(w http.ResponseWriter, r *http.Request) { figs, _ := figtree.AllFigsFancy() json.NewEncoder(w).Encode(figs) } func seeFigs(w http.ResponseWriter, r *http.Request) { figs, _ := figtree.AllFigsFancy() page := template.Must(template.ParseFiles("pages/allfigs.html")) if err := page.Execute(w, figs); err != nil { panic(err) } } func dryHandler(w http.ResponseWriter, r *http.Request) { page := template.Must(template.ParseFiles("pages/dryfig.html")) if err := page.Execute(w, nil); err != nil { panic(err) } }