-
Notifications
You must be signed in to change notification settings - Fork 104
/
create_mount.go
62 lines (52 loc) · 1.54 KB
/
create_mount.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
package repo
import (
"net/http"
"strconv"
"github.com/asaskevich/govalidator"
"github.com/bleenco/abstruse/pkg/lib"
"github.com/bleenco/abstruse/server/api/middlewares"
"github.com/bleenco/abstruse/server/api/render"
"github.com/bleenco/abstruse/server/core"
"github.com/go-chi/chi"
)
// HandleCreateMount returns an http.HandlerFunc that writes json encoded
// result about creating env variable to the http response body.
func HandleCreateMount(mount core.MountsStore, repos core.RepositoryStore) http.HandlerFunc {
type form struct {
Host string `json:"host" valid:"required"`
Container string `json:"container" valid:"required"`
}
return func(w http.ResponseWriter, r *http.Request) {
claims := middlewares.ClaimsFromCtx(r.Context())
var f form
var err error
defer r.Body.Close()
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
render.InternalServerError(w, err.Error())
return
}
if err = lib.DecodeJSON(r.Body, &f); err != nil {
render.InternalServerError(w, err.Error())
return
}
if valid, err := govalidator.ValidateStruct(f); err != nil || !valid {
render.BadRequestError(w, err.Error())
return
}
if perm := repos.GetPermissions(uint(id), claims.ID); !perm.Write {
render.UnathorizedError(w, "permission denied")
return
}
mnt := &core.Mount{
Host: f.Host,
Container: f.Container,
RepositoryID: uint(id),
}
if err := mount.Create(mnt); err != nil {
render.InternalServerError(w, err.Error())
return
}
render.JSON(w, http.StatusOK, mnt)
}
}