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.

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