-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
127 lines (102 loc) · 2.84 KB
/
router.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"errors"
"fmt"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func CheckServerIsHealthy(provider ProvidesDecentralizedIDs) gin.HandlerFunc {
return func(c *gin.Context) {
healthy, explanation := provider.IsHealthy(c)
if !healthy {
_ = c.AbortWithError(http.StatusInternalServerError, errors.New(explanation))
}
c.String(http.StatusOK, explanation)
}
}
type Result struct {
HasDecentralizedID bool
DecentralizedID DecentralizedID
}
func ParseHandleFromHostname(c *gin.Context) {
handle, err := HostnameToHandle(c.Request.Host)
if err != nil {
_ = c.AbortWithError(http.StatusBadRequest, err)
return
}
c.Set("handle", handle)
c.Next()
}
func CheckServerProvidesForDomain(provider ProvidesDecentralizedIDs, handleParameter string) gin.HandlerFunc {
return func(c *gin.Context) {
handle, err := HostnameToHandle(strings.ToLower(c.Query(handleParameter)))
if err != nil {
_ = c.AbortWithError(http.StatusBadRequest, err)
return
}
canProvide, err := provider.CanProvideForDomain(c, handle.Domain)
if err != nil {
_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}
if !canProvide {
c.String(http.StatusNotFound, "Decentralized IDs are not provided for %s by this server.", handle.Domain)
return
}
c.String(http.StatusOK, "Decentralized IDs are provided for %s by this server.", handle.Domain)
}
}
func WithHandleResult(provider ProvidesDecentralizedIDs) gin.HandlerFunc {
return func(c *gin.Context) {
handle := c.MustGet("handle").(Handle)
did, err := provider.GetDecentralizedIDForHandle(c, handle)
if errors.Is(err, (*CannotGetHandelsFromDomainError)(nil)) {
c.String(
http.StatusBadRequest,
fmt.Sprintf("Decentralized IDs for the domain %s are not provided by this server.", handle.Domain),
)
c.Abort()
return
}
if err != nil {
_ = c.AbortWithError(http.StatusBadGateway, err)
return
}
c.Set("result", Result{
HasDecentralizedID: did != "",
DecentralizedID: did,
})
}
}
func VerifyHandle(c *gin.Context) {
result := c.MustGet("result").(Result)
if !result.HasDecentralizedID {
c.String(
http.StatusNotFound,
fmt.Sprintf("Decentralized ID not found for %s", c.MustGet("handle").(Handle).String()),
)
return
}
c.String(http.StatusOK, string(result.DecentralizedID))
}
func RedirectUnmatchedRoute(redirectDid URLTemplate, redirectHandle URLTemplate) gin.HandlerFunc {
return func(c *gin.Context) {
result := c.MustGet("result").(Result)
if result.HasDecentralizedID {
c.Redirect(http.StatusTemporaryRedirect, URLFromTemplate(
redirectDid,
c.Request,
c.MustGet("handle").(Handle),
result.DecentralizedID,
))
return
}
c.Redirect(http.StatusTemporaryRedirect, URLFromTemplate(
redirectHandle,
c.Request,
c.MustGet("handle").(Handle),
DecentralizedID(""),
))
}
}