web.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package web
  2. import (
  3. "errors"
  4. "fmt"
  5. "html/template"
  6. "net/http"
  7. "net/url"
  8. "path/filepath"
  9. "strconv"
  10. "strings"
  11. )
  12. var (
  13. counter int = 1
  14. )
  15. func getContacts() map[string]any {
  16. return map[string]any{
  17. "Joe": "joe@example.com",
  18. "Sarah": "sarah@example.com",
  19. "Fred": "fred@example.com",
  20. }
  21. }
  22. func makeContacts1(templates *template.Template) http.HandlerFunc {
  23. return func(w http.ResponseWriter, r *http.Request) {
  24. templates.ExecuteTemplate(w, "contacts", map[string]any{
  25. "contacts": getContacts(),
  26. "counter": counter,
  27. })
  28. counter++
  29. }
  30. }
  31. func makeNav(templates *template.Template) http.HandlerFunc {
  32. return func(w http.ResponseWriter, r *http.Request) {
  33. path := strings.SplitAfterN(r.URL.Path, "/", 3)[2]
  34. htmlPath := r.Header.Get("Hx-Current-Url")
  35. u, err := url.Parse(htmlPath)
  36. if err != nil {
  37. http.Error(w, err.Error(), http.StatusBadRequest)
  38. return
  39. }
  40. htmlPath = strings.SplitAfterN(u.Path, "/", 2)[1]
  41. htmlPath = htmlPath[0 : len(htmlPath)-len(filepath.Ext(htmlPath))]
  42. num, err := strconv.Atoi(htmlPath)
  43. if err != nil {
  44. http.Error(w, err.Error(), http.StatusBadRequest)
  45. return
  46. }
  47. switch path {
  48. case "nav":
  49. templates.ExecuteTemplate(w, "nav", map[string]any{
  50. "num": num,
  51. "prev": fmt.Sprintf("%02d", num-1),
  52. "curr": fmt.Sprintf("%2d", num),
  53. "next": fmt.Sprintf("%02d", num+1),
  54. })
  55. default:
  56. http.Error(w, path, http.StatusNotFound)
  57. return
  58. }
  59. }
  60. }
  61. func makeSearch4(templates *template.Template) http.HandlerFunc {
  62. return func(w http.ResponseWriter, r *http.Request) {
  63. q := r.FormValue("q")
  64. state := r.FormValue("state")
  65. raw := getContacts()
  66. filtered := map[string]any{}
  67. for k, v := range raw {
  68. if strings.Contains(strings.ToUpper(k), strings.ToUpper(q)) {
  69. filtered[k] = v
  70. }
  71. }
  72. templates.ExecuteTemplate(w, "contacts", map[string]any{
  73. "contacts": filtered,
  74. "state": state,
  75. })
  76. }
  77. }
  78. func SetupRoutes(mux *http.ServeMux, templates *template.Template) {
  79. mux.Handle("/", http.FileServer(http.Dir("./web/public/")))
  80. mux.HandleFunc("/autonum/", makeNav(templates))
  81. mux.HandleFunc("/contacts", makeContacts1(templates))
  82. mux.HandleFunc("/search", makeSearch4(templates))
  83. }
  84. func UI(addr string) error {
  85. templates := template.Must(template.ParseGlob("./web/public/*.gohtml"))
  86. mux := http.NewServeMux()
  87. SetupRoutes(mux, templates)
  88. if err := http.ListenAndServe(addr, mux); err != nil {
  89. if !errors.Is(err, http.ErrServerClosed) {
  90. return fmt.Errorf("http server error %w", err)
  91. }
  92. }
  93. return nil
  94. }