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.

106 lines
2.4 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. }, 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) {
  78. w.WriteHeader(http.StatusBadRequest)
  79. err := renderTemplate(Templates["400.html"], pongo2.Context{}, r, w)
  80. if err != nil {
  81. http.Error(w, err.Error(), http.StatusInternalServerError)
  82. }
  83. }
  84. func unauthorizedHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  85. w.WriteHeader(401)
  86. err := renderTemplate(Templates["401.html"], pongo2.Context{}, r, w)
  87. if err != nil {
  88. http.Error(w, err.Error(), http.StatusInternalServerError)
  89. }
  90. }