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.

98 lines
2.2 KiB

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