-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
50 lines (39 loc) · 1 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package organization
import (
"context"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/superstackhq/common/api"
"github.com/superstackhq/identity/internal/app/identity/authentication"
)
type Handler struct {
router *gin.Engine
authenticator *authentication.Authenticator
manager *Manager
}
func NewHandler(router *gin.Engine, authenticator *authentication.Authenticator, manager *Manager) *Handler {
return &Handler{
router: router,
authenticator: authenticator,
manager: manager,
}
}
func (h *Handler) Register() {
h.router.GET("/api/v1/organization", h.get)
}
func (h *Handler) get(c *gin.Context) {
ctx, cancel := context.WithTimeout(c, 1*time.Second)
defer cancel()
au, err := h.authenticator.ValidateContext(c, ctx)
if err != nil {
api.Error(c, http.StatusUnauthorized, err)
return
}
org, err := h.manager.Get(ctx, au.OrganizationID)
if err != nil {
api.Error(c, http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, org)
}