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.

101 lines
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/env"
  9. "github.com/goanywhere/rex"
  10. "github.com/goanywhere/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. }