The backend for the project formerly known as signet, now known as beignet.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

61 rader
1.2 KiB

  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/imosed/signet/auth"
  6. . "github.com/imosed/signet/data"
  7. "github.com/rs/zerolog/log"
  8. )
  9. type CreateQueueRequest struct {
  10. Name string `json:"name"`
  11. }
  12. type CreateQueueResponse struct {
  13. ID uint `json:"id"`
  14. }
  15. func CreateQueue(w http.ResponseWriter, r *http.Request) {
  16. var req CreateQueueRequest
  17. err := json.NewDecoder(r.Body).Decode(&req)
  18. if err != nil {
  19. log.Error().Err(err).Msg("Could not decode body in CreateQueue call")
  20. return
  21. }
  22. var claims *auth.Claims
  23. claims, err = auth.GetUserClaims(r)
  24. if err != nil {
  25. log.Error().Err(err).Msg("Could not determine if user is authenticated")
  26. return
  27. }
  28. var resp CreateQueueResponse
  29. if claims != nil && claims.Privileges <= Admin {
  30. var specificQueue Queue
  31. Db.Table("queues").First(&specificQueue, "name = ?", req.Name)
  32. if specificQueue.ID != 0 {
  33. resp.ID = specificQueue.ID
  34. } else {
  35. queue := Queue{
  36. Name: req.Name,
  37. }
  38. Db.Create(&queue)
  39. resp.ID = queue.ID
  40. }
  41. } else {
  42. w.WriteHeader(403)
  43. return
  44. }
  45. err = json.NewEncoder(w).Encode(resp)
  46. if err != nil {
  47. log.Error().Err(err).Msg("Could not deliver response in CreateQueue call")
  48. }
  49. }