a simple url shortener in Go (check it out at qurl.org)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.4 KiB

  1. package pages
  2. import (
  3. "fmt"
  4. "html/template"
  5. "net/http"
  6. "qurl/static"
  7. "qurl/storage"
  8. )
  9. type RootHandler struct {
  10. Storage storage.Storage
  11. index *static.StaticContent
  12. css *static.StaticContent
  13. favi *static.StaticContent
  14. submit *template.Template
  15. }
  16. func (ctx *RootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  17. fname := r.URL.Path
  18. switch fname {
  19. case "/index.html":
  20. fallthrough
  21. case "/":
  22. ctx.index.ServeHTTP(w, r)
  23. case "/submit.html":
  24. ctx.ServeSubmit(w, r)
  25. case "/qurl.css":
  26. ctx.css.ServeHTTP(w, r)
  27. case "/favicon.ico":
  28. ctx.favi.ServeHTTP(w, r)
  29. case "/api/url":
  30. ctx.ServeAPI(w, r)
  31. default:
  32. fmt.Printf("Path: %s\n", fname)
  33. }
  34. }
  35. func (ctx *RootHandler) Init() error {
  36. // Initialize the static content object for the index page
  37. index := &static.StaticContent{Content: "index.html"}
  38. err := index.Init()
  39. if err != nil {
  40. return err
  41. }
  42. ctx.index = index
  43. // Initialize the static content object for the css
  44. css := &static.StaticContent{Content: "qurl.css"}
  45. err = css.Init()
  46. if err != nil {
  47. return err
  48. }
  49. ctx.css = css
  50. // Initialize the static content object for the css
  51. favi := &static.StaticContent{Content: "favicon.ico"}
  52. err = favi.Init()
  53. if err != nil {
  54. return err
  55. }
  56. ctx.favi = favi
  57. // Initialize submit page template
  58. ctx.submit = template.New("submit.html")
  59. _, err = ctx.submit.Parse(string(static.Assets["submit.html"]))
  60. if err != nil {
  61. return err
  62. }
  63. return nil
  64. }