handlers.go 3.1 KB

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