main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. "log"
  4. "os"
  5. "io"
  6. "encoding/json"
  7. "html/template"
  8. "net/http"
  9. "git.sr.ht/~jordyn/imagination/figtree"
  10. )
  11. func main() {
  12. fs := http.FileServer(http.Dir(figtree.FIGS))
  13. http.Handle("/imagination/", http.StripPrefix("/imagination/", fs))
  14. css := http.FileServer(http.Dir("css/"))
  15. http.Handle("/style/", http.StripPrefix("/style/", css))
  16. http.HandleFunc("/", homeHandler)
  17. http.HandleFunc("/fig/", figHandler)
  18. http.HandleFunc("/all/", seeFigs)
  19. http.HandleFunc("/dry/", dryHandler)
  20. log.Fatal(http.ListenAndServe(":8080", nil))
  21. }
  22. func homeHandler(w http.ResponseWriter, r *http.Request) {
  23. page := template.Must(template.ParseFiles("pages/home.html"))
  24. if err := page.Execute(w, nil); err != nil {
  25. panic(err)
  26. }
  27. }
  28. func figHandler(w http.ResponseWriter, r *http.Request) {
  29. r.ParseMultipartForm(32 << 20)
  30. file, _, err := r.FormFile("newfig")
  31. if err != nil {
  32. panic(err)
  33. }
  34. defer file.Close()
  35. fighome, newfig, err := figtree.HarvestFig(
  36. r.FormValue("name"),
  37. r.FormValue("desc"),
  38. r.FormValue("type"),
  39. r.FormValue("ext"),
  40. r.FormValue("birth"))
  41. log.Println("blooming " + fighome)
  42. f, err := os.Create(fighome)
  43. if err != nil {
  44. panic(err)
  45. }
  46. err = os.Chmod(fighome, 0666)
  47. if err != nil {
  48. panic(err)
  49. }
  50. defer f.Close()
  51. io.Copy(f, file)
  52. figtree.DryFig(newfig)
  53. page := template.Must(template.ParseFiles("pages/driedfig.html"))
  54. fancyfig := newfig.Fancy()
  55. if err := page.Execute(w, fancyfig); err != nil {
  56. panic(err)
  57. }
  58. }
  59. func allHandler(w http.ResponseWriter, r *http.Request) {
  60. figs, _ := figtree.AllFigsFancy()
  61. json.NewEncoder(w).Encode(figs)
  62. }
  63. func seeFigs(w http.ResponseWriter, r *http.Request) {
  64. figs, _ := figtree.AllFigsFancy()
  65. page := template.Must(template.ParseFiles("pages/allfigs.html"))
  66. if err := page.Execute(w, figs); err != nil {
  67. panic(err)
  68. }
  69. }
  70. func dryHandler(w http.ResponseWriter, r *http.Request) {
  71. page := template.Must(template.ParseFiles("pages/dryfig.html"))
  72. if err := page.Execute(w, nil); err != nil {
  73. panic(err)
  74. }
  75. }