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.

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