main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "github.com/andreanidouglas/auth-go/dao"
  8. "github.com/andreanidouglas/auth-go/internals"
  9. model "github.com/andreanidouglas/auth-go/model"
  10. )
  11. type ResponseLogin struct {
  12. Authenticated bool `json:"authenticated"`
  13. User model.AuthUser `json:"user,omitempty"`
  14. }
  15. type ResponseSignup struct {
  16. Created bool `json:"created"`
  17. User model.AuthUser `json:"user,omitempty"`
  18. }
  19. func (r *ResponseSignup) JsonResponseSignInSetAndWrite(rw http.ResponseWriter, authenticated bool, u *model.AuthUser) error {
  20. r.Created = authenticated
  21. u.PasswordHash = ""
  22. r.User = *u
  23. b, err := json.Marshal(r)
  24. if err != nil {
  25. return err
  26. }
  27. _, err = rw.Write(append(b, '\n'))
  28. return err
  29. }
  30. func (r *ResponseLogin) JsonResponseSetAndWrite(rw http.ResponseWriter, authenticated bool, u *model.AuthUser) error {
  31. r.Authenticated = authenticated
  32. u.PasswordHash = ""
  33. r.User = *u
  34. b, err := json.Marshal(r)
  35. if err != nil {
  36. return err
  37. }
  38. _, err = rw.Write(append(b, '\n'))
  39. return err
  40. }
  41. func setupRoute() {
  42. dao := dao.NewUserRepository()
  43. crypt := internals.NewCrypt()
  44. http.HandleFunc("/all", func(rw http.ResponseWriter, r *http.Request) {
  45. var authUser model.AuthUser
  46. if r.Method == "POST" {
  47. rw.WriteHeader(http.StatusMethodNotAllowed)
  48. return
  49. }
  50. all, err := authUser.GetAllUsers(dao)
  51. if err != nil {
  52. rw.WriteHeader(http.StatusInternalServerError)
  53. return
  54. }
  55. jsonRes, err := json.Marshal(all)
  56. if err != nil {
  57. rw.WriteHeader(http.StatusInternalServerError)
  58. return
  59. }
  60. rw.WriteHeader(http.StatusOK)
  61. rw.Write(jsonRes)
  62. })
  63. http.HandleFunc("/signup", func(rw http.ResponseWriter, r *http.Request) {
  64. var authUser model.AuthUser
  65. res := &ResponseSignup{Created: false}
  66. if r.Method == "GET" {
  67. rw.WriteHeader(http.StatusMethodNotAllowed)
  68. return
  69. }
  70. reqBody, err := ioutil.ReadAll(r.Body)
  71. if err != nil {
  72. rw.WriteHeader(http.StatusBadRequest)
  73. return
  74. }
  75. err = json.Unmarshal(reqBody, &authUser)
  76. if err != nil {
  77. rw.WriteHeader(http.StatusBadRequest)
  78. return
  79. }
  80. id, err := authUser.CreateUser(dao, crypt)
  81. if err != nil {
  82. rw.WriteHeader(http.StatusBadRequest)
  83. res.JsonResponseSignInSetAndWrite(rw, false, &model.AuthUser{})
  84. return
  85. }
  86. authUser.Id = id
  87. rw.WriteHeader(http.StatusOK)
  88. rw.Header().Add("Content-Type", "application/json")
  89. if err = res.JsonResponseSignInSetAndWrite(rw, true, &authUser); err != nil {
  90. log.Fatalf("could not write back to client: %v", err)
  91. }
  92. })
  93. http.HandleFunc("/login", func(rw http.ResponseWriter, r *http.Request) {
  94. var authUser model.AuthUser
  95. res := &ResponseLogin{Authenticated: false}
  96. if r.Method == "GET" {
  97. rw.WriteHeader(http.StatusMethodNotAllowed)
  98. return
  99. }
  100. reqBody, err := ioutil.ReadAll(r.Body)
  101. if err != nil {
  102. rw.WriteHeader(http.StatusBadRequest)
  103. return
  104. }
  105. err = json.Unmarshal(reqBody, &authUser)
  106. if err != nil {
  107. rw.WriteHeader(http.StatusBadRequest)
  108. return
  109. }
  110. id, err := authUser.AuthenticateUser(dao, crypt)
  111. if err != nil {
  112. log.Printf("could not authenticate user: %v", err)
  113. rw.WriteHeader(http.StatusUnauthorized)
  114. res.JsonResponseSetAndWrite(rw, false, &model.AuthUser{})
  115. return
  116. }
  117. authUser.Id = id
  118. rw.WriteHeader(http.StatusOK)
  119. rw.Header().Add("Content-Type", "application/json")
  120. if err = res.JsonResponseSetAndWrite(rw, true, &authUser); err != nil {
  121. log.Fatalf("could not write back to client: %v", err)
  122. }
  123. })
  124. }
  125. func main() {
  126. setupRoute()
  127. log.Fatal(http.ListenAndServe("localhost:8080", nil))
  128. }