forked from amborle/featmap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
link-api.go
63 lines (49 loc) · 1.29 KB
/
link-api.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
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/pkg/errors"
)
func linkAPI(r chi.Router) {
r.Group(func(r chi.Router) {
r.Route("/{LINK}",
func(r chi.Router) {
r.Get("/", getLink)
})
})
}
// getLink ...
func getLink(w http.ResponseWriter, r *http.Request) {
link := chi.URLParam(r, "LINK")
s := GetEnv(r).Service
project, err := s.GetProjectByExternalLink(link)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(errors.New("not found")))
return
}
ws, _ := s.GetWorkspace(project.WorkspaceID)
sub := s.GetSubscriptionByWorkspace(ws.ID)
if sub == nil {
_ = render.Render(w, r, ErrInvalidRequest(errors.New("not allowed")))
return
}
if !subscriptionIsActive(sub) {
_ = render.Render(w, r, ErrInvalidRequest(errors.New("not allowed")))
return
}
if !(sub.Level == "PRO" || sub.Level == "TRIAL" || sub.Level == "BASIC") {
_ = render.Render(w, r, ErrInvalidRequest(errors.New("not allowed")))
return
}
if !ws.AllowExternalSharing {
_ = render.Render(w, r, ErrInvalidRequest(errors.New("not allowed")))
return
}
extended, err := s.GetProjectExtendedByExternalLink(link)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(errors.New("not found")))
return
}
render.JSON(w, r, extended)
}