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.

125 lines
3.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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. }, r, w)
  22. if err != nil {
  23. http.Error(w, err.Error(), http.StatusInternalServerError)
  24. }
  25. }
  26. func pasteHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  27. err := renderTemplate(Templates["paste.html"], pongo2.Context{
  28. "expirylist": listExpirationTimes(),
  29. }, r, w)
  30. if err != nil {
  31. oopsHandler(c, w, r, RespHTML, "")
  32. }
  33. }
  34. func apiDocHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  35. err := renderTemplate(Templates["API.html"], pongo2.Context{
  36. "siteurl": getSiteURL(r),
  37. }, r, w)
  38. if err != nil {
  39. oopsHandler(c, w, r, RespHTML, "")
  40. }
  41. }
  42. func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  43. w.WriteHeader(404)
  44. err := renderTemplate(Templates["404.html"], pongo2.Context{}, r, w)
  45. if err != nil {
  46. oopsHandler(c, w, r, RespHTML, "")
  47. }
  48. }
  49. func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request, rt RespType, msg string) {
  50. if msg == "" {
  51. msg = "Oops! Something went wrong..."
  52. }
  53. if rt == RespHTML {
  54. w.WriteHeader(500)
  55. renderTemplate(Templates["oops.html"], pongo2.Context{"msg": msg}, r, w)
  56. return
  57. } else if rt == RespPLAIN {
  58. w.WriteHeader(500)
  59. fmt.Fprintf(w, "%s", msg)
  60. return
  61. } else if rt == RespJSON {
  62. js, _ := json.Marshal(map[string]string{
  63. "error": msg,
  64. })
  65. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  66. w.WriteHeader(500)
  67. w.Write(js)
  68. return
  69. } else if rt == RespAUTO {
  70. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  71. oopsHandler(c, w, r, RespJSON, msg)
  72. } else {
  73. oopsHandler(c, w, r, RespHTML, msg)
  74. }
  75. }
  76. }
  77. func badRequestHandler(c web.C, w http.ResponseWriter, r *http.Request, rt RespType, msg string) {
  78. if rt == RespHTML {
  79. w.WriteHeader(http.StatusBadRequest)
  80. err := renderTemplate(Templates["400.html"], pongo2.Context{"msg": msg}, r, w)
  81. if err != nil {
  82. http.Error(w, err.Error(), http.StatusInternalServerError)
  83. }
  84. return
  85. } else if rt == RespPLAIN {
  86. w.WriteHeader(http.StatusBadRequest)
  87. fmt.Fprintf(w, "%s", msg)
  88. return
  89. } else if rt == RespJSON {
  90. js, _ := json.Marshal(map[string]string{
  91. "error": msg,
  92. })
  93. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  94. w.WriteHeader(http.StatusBadRequest)
  95. w.Write(js)
  96. return
  97. } else if rt == RespAUTO {
  98. if strings.EqualFold("application/json", r.Header.Get("Accept")) {
  99. badRequestHandler(c, w, r, RespJSON, msg)
  100. } else {
  101. badRequestHandler(c, w, r, RespHTML, msg)
  102. }
  103. }
  104. }
  105. func unauthorizedHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  106. w.WriteHeader(401)
  107. err := renderTemplate(Templates["401.html"], pongo2.Context{}, r, w)
  108. if err != nil {
  109. http.Error(w, err.Error(), http.StatusInternalServerError)
  110. }
  111. }