handlers.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "log"
  7. "net/http"
  8. "git.aetherial.dev/aeth/yosai/pkg/daemon"
  9. )
  10. const UserQueryParam = "username"
  11. /*
  12. Run a new webserver
  13. :param port: port number to run the webserver on
  14. */
  15. func RunHttpServer(port int, dbhook DatabaseIO, loggingOut io.Writer) {
  16. execHndl := &ExecutionHandler{DbHook: dbhook, out: loggingOut}
  17. http.HandleFunc("/get-config/{username}", execHndl.GetUserConfiguration)
  18. http.HandleFunc("/update-config/{username}", execHndl.UpdateUserConfiguration)
  19. log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
  20. }
  21. type ExecutionHandler struct {
  22. DbHook DatabaseIO
  23. out io.Writer
  24. }
  25. func (e *ExecutionHandler) Log(msg ...string) {
  26. logMsg := "YOSAI SERVER LOGGER ::: "
  27. for i := range msg {
  28. logMsg = logMsg + " " + msg[i] + "\n"
  29. }
  30. e.out.Write([]byte(logMsg))
  31. }
  32. /*
  33. Handler for the route to retrieve a user configuration
  34. :param w: the http.ResponseWriter to write the response into
  35. :param req: a pointer to the http.Request to parse
  36. */
  37. func (e *ExecutionHandler) GetUserConfiguration(w http.ResponseWriter, req *http.Request) {
  38. e.Log("Recieved request: ", req.URL.Path)
  39. if req.Method != http.MethodGet {
  40. e.Log("Unsupported method: ", req.Method, "to endpoint: ", req.URL.Path)
  41. w.WriteHeader(http.StatusMethodNotAllowed)
  42. return
  43. }
  44. user := req.PathValue(UserQueryParam)
  45. if user == "" {
  46. w.WriteHeader(http.StatusBadRequest)
  47. return
  48. }
  49. e.Log("Called from: ", user)
  50. config, err := e.DbHook.GetConfigByUser(ValidateUsername(user))
  51. if err != nil {
  52. e.Log(err.Error())
  53. w.WriteHeader(http.StatusNotFound)
  54. return
  55. }
  56. e.Log("Config gotten successfully.")
  57. b, err := json.Marshal(config)
  58. if err != nil {
  59. e.Log(err.Error())
  60. w.WriteHeader(http.StatusInternalServerError)
  61. return
  62. }
  63. _, err = w.Write(b)
  64. if err != nil {
  65. e.Log(err.Error())
  66. w.WriteHeader(http.StatusInternalServerError)
  67. return
  68. }
  69. return
  70. }
  71. /*
  72. Handler for updating the calling users configuration
  73. :param w: the http.ResponseWriter to write the response into
  74. :param req: a pointer to the http.Request to parse
  75. */
  76. func (e *ExecutionHandler) UpdateUserConfiguration(w http.ResponseWriter, req *http.Request) {
  77. e.Log("Recieved request: ", req.URL.Path)
  78. if req.Method != http.MethodPost || req.Method != http.MethodPatch {
  79. e.Log("Unsupported method: ", req.Method, "to endpoint: ", req.URL.Path)
  80. w.WriteHeader(http.StatusMethodNotAllowed)
  81. return
  82. }
  83. user := req.PathValue(UserQueryParam)
  84. if user == "" {
  85. w.WriteHeader(http.StatusBadRequest)
  86. return
  87. }
  88. e.Log("Called from: ", user)
  89. body, err := io.ReadAll(req.Body)
  90. if err != nil {
  91. e.Log(err.Error())
  92. w.WriteHeader(http.StatusBadRequest)
  93. return
  94. }
  95. var config daemon.Configuration
  96. if err = json.Unmarshal(body, &config); err != nil {
  97. e.Log(err.Error())
  98. w.WriteHeader(http.StatusBadRequest)
  99. return
  100. }
  101. if err = e.DbHook.UpdateUser(ValidateUsername(user), config); err != nil {
  102. e.Log(err.Error())
  103. w.WriteHeader(http.StatusBadRequest)
  104. return
  105. }
  106. w.WriteHeader(http.StatusOK)
  107. return
  108. }