get_short.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package web
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "strings"
  7. "code.osinet.fr/fgm/kurz/domain"
  8. "github.com/gorilla/mux"
  9. )
  10. /*
  11. Template variables:
  12. - assetsBaseURL http://plusvite-cdn.osinet.eu
  13. - FullyQualifiedShortURL (short string)
  14. - SiteBaseURL http://plusvite.net/
  15. - FullyQualifiedTargetURL (target string)
  16. - RefreshDelay (seconds int)
  17. - SiteName PlusVite
  18. */
  19. type bytes = []byte
  20. func build403(w http.ResponseWriter, short string) {
  21. data := struct {
  22. FullyQualifiedShortURL string
  23. Globals
  24. }{
  25. short,
  26. globals,
  27. }
  28. sw := &strings.Builder{}
  29. err := tmpl.ExecuteTemplate(sw, "403", data)
  30. if err != nil {
  31. w.WriteHeader(http.StatusInternalServerError)
  32. } else {
  33. w.WriteHeader(http.StatusForbidden)
  34. io.Copy(w, strings.NewReader(sw.String()))
  35. }
  36. }
  37. func build404(w http.ResponseWriter, short string) {
  38. data := struct {
  39. FullyQualifiedShortURL string
  40. Globals
  41. }{
  42. short,
  43. globals,
  44. }
  45. sw := &strings.Builder{}
  46. err := tmpl.ExecuteTemplate(sw, "404", data)
  47. if err != nil {
  48. w.WriteHeader(http.StatusInternalServerError)
  49. } else {
  50. w.WriteHeader(http.StatusNotFound)
  51. io.Copy(w, strings.NewReader(sw.String()))
  52. }
  53. }
  54. func build451(w http.ResponseWriter, router *mux.Router, short string) {
  55. hr := router.Get(RouteGetRoot)
  56. _, err := hr.URL()
  57. if err != nil {
  58. fmt.Println(err)
  59. w.WriteHeader(http.StatusInternalServerError)
  60. return
  61. }
  62. data := struct {
  63. FullyQualifiedShortURL string
  64. Globals
  65. }{
  66. short,
  67. globals,
  68. }
  69. sw := &strings.Builder{}
  70. err = tmpl.ExecuteTemplate(sw, "404", data)
  71. if err != nil {
  72. w.WriteHeader(http.StatusInternalServerError)
  73. } else {
  74. w.WriteHeader(http.StatusNotFound)
  75. io.Copy(w, strings.NewReader(sw.String()))
  76. }
  77. }
  78. // handleGetShort handles path /<short>
  79. func handleGetShort(w http.ResponseWriter, r *http.Request, router *mux.Router) {
  80. short, ok := mux.Vars(r)["short"]
  81. if !ok {
  82. w.WriteHeader(http.StatusBadRequest)
  83. return
  84. }
  85. target, err := domain.GetTargetURL(short)
  86. // Happy path.
  87. if err == nil {
  88. w.Header().Set("Location", target)
  89. w.WriteHeader(http.StatusTemporaryRedirect)
  90. return
  91. }
  92. // Very sad path.
  93. domainErr, ok := err.(domain.Error)
  94. if !ok {
  95. // All errors return by the API should be domain-specific errors.
  96. w.WriteHeader(http.StatusInternalServerError)
  97. return
  98. }
  99. // Normal sad paths.
  100. switch domainErr.Kind {
  101. case domain.ShortNotFound:
  102. build404(w, short)
  103. case domain.TargetBlockedError:
  104. build403(w, short)
  105. case domain.TargetCensoredError:
  106. build451(w, router, short)
  107. default:
  108. // TargetInvalid is not supposed to happen in this case, so it is an internal error too.
  109. w.WriteHeader(http.StatusInternalServerError)
  110. }
  111. }