Contains the Concourse pipeline definition for building a line-server container
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.

128 lines
3.2 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/flosch/pongo2"
  8. "github.com/zenazn/goji/web"
  9. )
  10. type RespType int
  11. const (
  12. RespPLAIN RespType = iota
  13. RespJSON
  14. RespHTML
  15. RespAUTO
  16. )
  17. func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  18. err := renderTemplate(Templates["index.html"], pongo2.Context{
  19. "maxsize": Config.maxSize,
  20. "expirylist": listExpirationTimes(),
  21. "forcerandom": Config.forceRandomFilename,
  22. }, r, w)
  23. if err != nil {
  24. http.Error(w, err.Error(), http.StatusInternalServerError)
  25. }
  26. }
  27. func pasteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  28. err := renderTemplate(Templates["paste.html"], pongo2.Context{
  29. "expirylist": listExpirationTimes(),
  30. "forcerandom": Config.forceRandomFilename,
  31. }, r, w)
  32. if err != nil {
  33. oopsHandler(c, w, r, RespHTML, "")
  34. }
  35. }
  36. func apiDocHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  37. err := renderTemplate(Templates["API.html"], pongo2.Context{
  38. "siteurl": getSiteURL(r),
  39. "forcerandom": Config.forceRandomFilename,
  40. }, r, w)
  41. if err != nil {
  42. oopsHandler(c, w, r, RespHTML, "")
  43. }
  44. }
  45. func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  46. w.WriteHeader(404)
  47. err := renderTemplate(Templates["404.html"], pongo2.Context{}, r, w)
  48. if err != nil {
  49. oopsHandler(c, w, r, RespHTML, "")
  50. }
  51. }
  52. func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request, rt RespType, msg string) {
  53. if msg == "" {
  54. msg = "Oops! Something went wrong..."
  55. }
  56. if rt == RespHTML {
  57. w.WriteHeader(500)
  58. renderTemplate(Templates["oops.html"], pongo2.Context{"msg": msg}, r, w)
  59. return
  60. } else if rt == RespPLAIN {
  61. w.WriteHeader(500)
  62. fmt.Fprintf(w, "%s", msg)
  63. return
  64. } else if rt == RespJSON {
  65. js, _ := json.Marshal(map[string]string{
  66. "error": msg,
  67. })
  68. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  69. w.WriteHeader(500)
  70. w.Write(js)
  71. return
  72. } else if rt == RespAUTO {
  73. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  74. oopsHandler(c, w, r, RespJSON, msg)
  75. } else {
  76. oopsHandler(c, w, r, RespHTML, msg)
  77. }
  78. }
  79. }
  80. func badRequestHandler(c web.C, w http.ResponseWriter, r *http.Request, rt RespType, msg string) {
  81. if rt == RespHTML {
  82. w.WriteHeader(http.StatusBadRequest)
  83. err := renderTemplate(Templates["400.html"], pongo2.Context{"msg": msg}, r, w)
  84. if err != nil {
  85. http.Error(w, err.Error(), http.StatusInternalServerError)
  86. }
  87. return
  88. } else if rt == RespPLAIN {
  89. w.WriteHeader(http.StatusBadRequest)
  90. fmt.Fprintf(w, "%s", msg)
  91. return
  92. } else if rt == RespJSON {
  93. js, _ := json.Marshal(map[string]string{
  94. "error": msg,
  95. })
  96. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  97. w.WriteHeader(http.StatusBadRequest)
  98. w.Write(js)
  99. return
  100. } else if rt == RespAUTO {
  101. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  102. badRequestHandler(c, w, r, RespJSON, msg)
  103. } else {
  104. badRequestHandler(c, w, r, RespHTML, msg)
  105. }
  106. }
  107. }
  108. func unauthorizedHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  109. w.WriteHeader(401)
  110. err := renderTemplate(Templates["401.html"], pongo2.Context{}, r, w)
  111. if err != nil {
  112. http.Error(w, err.Error(), http.StatusInternalServerError)
  113. }
  114. }