Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 9 roky
před 9 roky
před 9 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "html/template"
  6. "net/http"
  7. "path/filepath"
  8. "git.thevis.us/skepto/env"
  9. "git.thevis.us/skepto/rex"
  10. "git.thevis.us/skepto/rex/livereload"
  11. )
  12. type User struct {
  13. Username string
  14. }
  15. // -------------------- HTML Template --------------------
  16. func Index(w http.ResponseWriter, r *http.Request) {
  17. if html, err := template.ParseFiles(filepath.Join("views", "index.html")); err != nil {
  18. http.Error(w, err.Error(), http.StatusInternalServerError)
  19. } else {
  20. w.Header().Set("Content-Type", "text/html")
  21. var user = User{Username: env.String("USER", "guest")}
  22. html.Execute(w, user)
  23. }
  24. }
  25. // -------------------- JSON Template --------------------
  26. type Response struct {
  27. Code int
  28. Text string
  29. }
  30. func JSON(next http.Handler) http.Handler {
  31. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  32. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  33. next.ServeHTTP(w, r)
  34. })
  35. }
  36. func fetch(w http.ResponseWriter, r *http.Request) {
  37. w.WriteHeader(http.StatusOK)
  38. var buffer = new(bytes.Buffer)
  39. var response = Response{Code: http.StatusOK, Text: http.StatusText(http.StatusOK)}
  40. if err := json.NewEncoder(buffer).Encode(response); err == nil {
  41. w.Write(buffer.Bytes())
  42. } else {
  43. http.Error(w, err.Error(), http.StatusBadGateway)
  44. }
  45. }
  46. func create(w http.ResponseWriter, r *http.Request) {
  47. w.WriteHeader(http.StatusCreated)
  48. var buffer = new(bytes.Buffer)
  49. var response = Response{Code: http.StatusCreated, Text: http.StatusText(http.StatusCreated)}
  50. if err := json.NewEncoder(buffer).Encode(response); err == nil {
  51. w.Write(buffer.Bytes())
  52. } else {
  53. http.Error(w, err.Error(), http.StatusBadGateway)
  54. }
  55. }
  56. func update(w http.ResponseWriter, r *http.Request) {
  57. w.WriteHeader(http.StatusAccepted)
  58. var buffer = new(bytes.Buffer)
  59. var response = Response{Code: http.StatusAccepted, Text: http.StatusText(http.StatusAccepted)}
  60. if err := json.NewEncoder(buffer).Encode(response); err == nil {
  61. w.Write(buffer.Bytes())
  62. } else {
  63. http.Error(w, err.Error(), http.StatusBadGateway)
  64. }
  65. }
  66. func remove(w http.ResponseWriter, r *http.Request) {
  67. w.WriteHeader(http.StatusGone)
  68. var buffer = new(bytes.Buffer)
  69. var response = Response{Code: http.StatusGone, Text: http.StatusText(http.StatusGone)}
  70. if err := json.NewEncoder(buffer).Encode(response); err == nil {
  71. w.Write(buffer.Bytes())
  72. } else {
  73. http.Error(w, err.Error(), http.StatusBadGateway)
  74. }
  75. }
  76. func main() {
  77. app := rex.New()
  78. app.Use(livereload.Middleware)
  79. app.Get("/", Index)
  80. api := app.Group("/v1/")
  81. api.Use(JSON)
  82. api.Get("/", fetch)
  83. api.Post("/", create)
  84. api.Put("/", update)
  85. api.Delete("/", remove)
  86. app.Run()
  87. }