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.

75 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. default:
  30. fmt.Printf("Path: %s\n", fname)
  31. }
  32. }
  33. func (ctx *RootHandler) Init() error {
  34. // Initialize the static content object for the index page
  35. index := &static.StaticContent{Content: "index.html"}
  36. err := index.Init()
  37. if err != nil {
  38. return err
  39. }
  40. ctx.index = index
  41. // Initialize the static content object for the css
  42. css := &static.StaticContent{Content: "qurl.css"}
  43. err = css.Init()
  44. if err != nil {
  45. return err
  46. }
  47. ctx.css = css
  48. // Initialize the static content object for the css
  49. favi := &static.StaticContent{Content: "favicon.ico"}
  50. err = favi.Init()
  51. if err != nil {
  52. return err
  53. }
  54. ctx.favi = favi
  55. // Initialize submit page template
  56. ctx.submit = template.New("submit.html")
  57. _, err = ctx.submit.Parse(string(static.Assets["submit.html"]))
  58. if err != nil {
  59. return err
  60. }
  61. return nil
  62. }