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.

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