No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

99 líneas
2.6KB

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