-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_admin.go
67 lines (56 loc) · 1.49 KB
/
server_admin.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
64
65
66
67
package api
import (
"errors"
"fmt"
"net/http"
"github.com/fabiante/persurl/api/res"
"github.com/fabiante/persurl/app"
"github.com/gin-gonic/gin"
)
func (s *Server) SavePURL(ctx *gin.Context) {
domainName := ctx.Param("domain")
name := ctx.Param("name")
var req res.SavePURL
if err := ctx.BindJSON(&req); err != nil {
ctx.Abort()
return
}
domain, err := s.admin.GetDomain(domainName)
switch {
case errors.Is(err, app.ErrNotFound):
respondWithError(ctx, http.StatusBadRequest, err)
return
case err != nil:
respondWithError(ctx, http.StatusInternalServerError, err)
return
}
// check authorization
user := getAuthenticatedUser(ctx)
if domain.OwnerID != user.ID {
respondWithError(ctx, http.StatusForbidden, ErrForbidden)
return
}
err = s.admin.SavePURL(domain, name, req.Target)
switch {
case errors.Is(err, app.ErrBadRequest):
respondWithError(ctx, http.StatusBadRequest, err)
return
case err != nil:
respondWithError(ctx, http.StatusInternalServerError, err)
return
}
ctx.JSON(http.StatusOK, res.NewSavePURLResponse(fmt.Sprintf("/r/%s/%s", domainName, name)))
}
func (s *Server) CreateDomain(ctx *gin.Context) {
domain := ctx.Param("domain")
user := getAuthenticatedUser(ctx)
_, err := s.admin.CreateDomain(user, domain)
switch true {
case err == nil:
ctx.Status(http.StatusNoContent)
case errors.Is(err, app.ErrBadRequest):
respondWithError(ctx, http.StatusBadRequest, err)
default:
respondWithError(ctx, http.StatusInternalServerError, err)
}
}