main.go 2.2 KB

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