Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit ff50d6e

Browse files
author
Noah Hanjun Lee
authored
Refactorying middleware (#161)
1 parent a212b8a commit ff50d6e

File tree

9 files changed

+110
-112
lines changed

9 files changed

+110
-112
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
//go:generate mockgen -source ./interface.go -destination ./mock/interactor.go -package mock
22

3-
package middlewares
3+
package shared
44

55
import (
66
"context"
77

8-
"github.com/gitploy-io/gitploy/ent"
98
"github.com/gitploy-io/gitploy/vo"
109
)
1110

1211
type (
1312
Interactor interface {
14-
FindUserByHash(ctx context.Context, hash string) (*ent.User, error)
1513
GetLicense(ctx context.Context) (*vo.License, error)
1614
}
1715
)

internal/server/middlewares/license.go renamed to internal/server/api/shared/middleware.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package middlewares
1+
package shared
22

33
import (
44
"net/http"
@@ -13,22 +13,22 @@ const (
1313
)
1414

1515
type (
16-
LicenseMiddleware struct {
16+
Middleware struct {
1717
i Interactor
1818
}
1919
)
2020

21-
func NewLicenseMiddleware(intr Interactor) *LicenseMiddleware {
22-
return &LicenseMiddleware{
21+
func NewMiddleware(intr Interactor) *Middleware {
22+
return &Middleware{
2323
i: intr,
2424
}
2525
}
2626

27-
func (lm *LicenseMiddleware) IsExpired() gin.HandlerFunc {
27+
func (m *Middleware) IsLicenseExpired() gin.HandlerFunc {
2828
return func(c *gin.Context) {
2929
ctx := c.Request.Context()
3030

31-
lic, err := lm.i.GetLicense(ctx)
31+
lic, err := m.i.GetLicense(ctx)
3232
if err != nil {
3333
gb.AbortWithErrorResponse(c, http.StatusInternalServerError, "It has failed to get the license.")
3434
return
@@ -48,3 +48,14 @@ func (lm *LicenseMiddleware) IsExpired() gin.HandlerFunc {
4848
}
4949
}
5050
}
51+
52+
func (m *Middleware) OnlyAuthorized() gin.HandlerFunc {
53+
return func(c *gin.Context) {
54+
_, ok := c.Get(gb.KeyUser)
55+
if !ok {
56+
c.AbortWithStatusJSON(http.StatusUnauthorized, map[string]string{
57+
"message": "Unauthorized user",
58+
})
59+
}
60+
}
61+
}

internal/server/middlewares/license_test.go renamed to internal/server/api/shared/middleware_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package middlewares
1+
package shared
22

33
import (
44
"net/http"
@@ -7,12 +7,12 @@ import (
77
"time"
88

99
"github.com/gin-gonic/gin"
10-
"github.com/gitploy-io/gitploy/internal/server/middlewares/mock"
10+
"github.com/gitploy-io/gitploy/internal/server/api/shared/mock"
1111
"github.com/gitploy-io/gitploy/vo"
1212
"github.com/golang/mock/gomock"
1313
)
1414

15-
func TestLicenseMiddleware_IsExpired(t *testing.T) {
15+
func TestMiddleware_IsLicenseExpired(t *testing.T) {
1616
month := 30 * 24 * time.Hour
1717

1818
t.Run("Return 402 error when the count of member is over the limit.", func(t *testing.T) {
@@ -27,8 +27,8 @@ func TestLicenseMiddleware_IsExpired(t *testing.T) {
2727
gin.SetMode(gin.ReleaseMode)
2828
router := gin.New()
2929

30-
lm := NewLicenseMiddleware(m)
31-
router.GET("/repos", lm.IsExpired(), func(c *gin.Context) {
30+
lm := NewMiddleware(m)
31+
router.GET("/repos", lm.IsLicenseExpired(), func(c *gin.Context) {
3232
c.Status(http.StatusOK)
3333
})
3434

@@ -38,7 +38,7 @@ func TestLicenseMiddleware_IsExpired(t *testing.T) {
3838
router.ServeHTTP(w, req)
3939

4040
if w.Code != http.StatusPaymentRequired {
41-
t.Fatalf("IsExpired = %v, wanted %v", w.Code, http.StatusPaymentRequired)
41+
t.Fatalf("IsLicenseExpired = %v, wanted %v", w.Code, http.StatusPaymentRequired)
4242
}
4343
})
4444

@@ -54,8 +54,8 @@ func TestLicenseMiddleware_IsExpired(t *testing.T) {
5454
gin.SetMode(gin.ReleaseMode)
5555
router := gin.New()
5656

57-
lm := NewLicenseMiddleware(m)
58-
router.GET("/repos", lm.IsExpired(), func(c *gin.Context) {
57+
lm := NewMiddleware(m)
58+
router.GET("/repos", lm.IsLicenseExpired(), func(c *gin.Context) {
5959
c.Status(http.StatusOK)
6060
})
6161

@@ -65,7 +65,7 @@ func TestLicenseMiddleware_IsExpired(t *testing.T) {
6565
router.ServeHTTP(w, req)
6666

6767
if w.Code != http.StatusOK {
68-
t.Fatalf("IsExpired = %v, wanted %v", w.Code, http.StatusOK)
68+
t.Fatalf("IsLicenseExpired = %v, wanted %v", w.Code, http.StatusOK)
6969
}
7070
})
7171

@@ -84,8 +84,8 @@ func TestLicenseMiddleware_IsExpired(t *testing.T) {
8484
gin.SetMode(gin.ReleaseMode)
8585
router := gin.New()
8686

87-
lm := NewLicenseMiddleware(m)
88-
router.GET("/repos", lm.IsExpired(), func(c *gin.Context) {
87+
lm := NewMiddleware(m)
88+
router.GET("/repos", lm.IsLicenseExpired(), func(c *gin.Context) {
8989
c.Status(http.StatusOK)
9090
})
9191

@@ -95,7 +95,7 @@ func TestLicenseMiddleware_IsExpired(t *testing.T) {
9595
router.ServeHTTP(w, req)
9696

9797
if w.Code != http.StatusPaymentRequired {
98-
t.Fatalf("IsExpired = %v, wanted %v", w.Code, http.StatusPaymentRequired)
98+
t.Fatalf("IsLicenseExpired = %v, wanted %v", w.Code, http.StatusPaymentRequired)
9999
}
100100
})
101101

@@ -115,8 +115,8 @@ func TestLicenseMiddleware_IsExpired(t *testing.T) {
115115
gin.SetMode(gin.ReleaseMode)
116116
router := gin.New()
117117

118-
lm := NewLicenseMiddleware(m)
119-
router.GET("/repos", lm.IsExpired(), func(c *gin.Context) {
118+
lm := NewMiddleware(m)
119+
router.GET("/repos", lm.IsLicenseExpired(), func(c *gin.Context) {
120120
c.Status(http.StatusOK)
121121
})
122122

@@ -126,7 +126,7 @@ func TestLicenseMiddleware_IsExpired(t *testing.T) {
126126
router.ServeHTTP(w, req)
127127

128128
if w.Code != http.StatusOK {
129-
t.Fatalf("IsExpired = %v, wanted %v", w.Code, http.StatusOK)
129+
t.Fatalf("IsLicenseExpired = %v, wanted %v", w.Code, http.StatusOK)
130130
}
131131
})
132132
}

internal/server/middlewares/mock/interactor.go renamed to internal/server/api/shared/mock/interactor.go

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/server/global/interface.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package global
2+
3+
import (
4+
"context"
5+
6+
"github.com/gitploy-io/gitploy/ent"
7+
)
8+
9+
type (
10+
Interactor interface {
11+
FindUserByHash(ctx context.Context, hash string) (*ent.User, error)
12+
}
13+
)

internal/server/global/middleware.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package global
2+
3+
import (
4+
"strings"
5+
6+
"github.com/gin-gonic/gin"
7+
"github.com/gitploy-io/gitploy/ent"
8+
)
9+
10+
type (
11+
Middleware struct {
12+
i Interactor
13+
}
14+
)
15+
16+
func NewMiddleware(i Interactor) *Middleware {
17+
return &Middleware{
18+
i: i,
19+
}
20+
}
21+
22+
func (s *Middleware) SetUser() gin.HandlerFunc {
23+
return func(c *gin.Context) {
24+
ctx := c.Request.Context()
25+
26+
u, err := s.i.FindUserByHash(ctx, FindHash(c))
27+
if ent.IsNotFound(err) {
28+
return
29+
}
30+
31+
c.Set(KeyUser, u)
32+
}
33+
}
34+
35+
func FindHash(c *gin.Context) string {
36+
s, _ := c.Cookie(CookieSession)
37+
if s != "" {
38+
return s
39+
}
40+
41+
header := c.GetHeader("Authorization")
42+
s = strings.TrimPrefix(header, "Bearer ")
43+
if s != "" {
44+
return s
45+
}
46+
47+
return ""
48+
}

internal/server/metrics/middleware.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func init() {
3333
prometheus.MustRegister(RequestDuration)
3434
}
3535

36-
// ReponseMetrics is the middleware to collect metrics about the response.
37-
func ReponseMetrics() gin.HandlerFunc {
36+
// CollectRequestMetrics is the middleware to collect metrics for the request.
37+
func CollectRequestMetrics() gin.HandlerFunc {
3838
return func(c *gin.Context) {
3939
start := time.Now()
4040

internal/server/middlewares/sess.go

Lines changed: 0 additions & 61 deletions
This file was deleted.

internal/server/router.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ import (
99
"github.com/gin-gonic/gin"
1010
"golang.org/x/oauth2"
1111

12+
"github.com/gitploy-io/gitploy/internal/server/api/shared"
1213
"github.com/gitploy-io/gitploy/internal/server/api/v1/license"
1314
"github.com/gitploy-io/gitploy/internal/server/api/v1/repos"
1415
"github.com/gitploy-io/gitploy/internal/server/api/v1/search"
1516
"github.com/gitploy-io/gitploy/internal/server/api/v1/stream"
1617
"github.com/gitploy-io/gitploy/internal/server/api/v1/sync"
1718
"github.com/gitploy-io/gitploy/internal/server/api/v1/users"
19+
gb "github.com/gitploy-io/gitploy/internal/server/global"
1820
"github.com/gitploy-io/gitploy/internal/server/hooks"
1921
"github.com/gitploy-io/gitploy/internal/server/metrics"
20-
mw "github.com/gitploy-io/gitploy/internal/server/middlewares"
2122
s "github.com/gitploy-io/gitploy/internal/server/slack"
2223
"github.com/gitploy-io/gitploy/internal/server/web"
2324
)
@@ -66,9 +67,10 @@ type (
6667
}
6768

6869
Interactor interface {
70+
gb.Interactor
6971
s.Interactor
7072
sync.Interactor
71-
mw.Interactor
73+
shared.Interactor
7274
web.Interactor
7375
repos.Interactor
7476
users.Interactor
@@ -95,17 +97,20 @@ func NewRouter(c *RouterConfig) *gin.Engine {
9597
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
9698
AllowHeaders: []string{"Origin", "Authorization", "Accept", "Content-Length", "Content-Type"},
9799
}))
98-
sm := mw.NewSessMiddleware(c.Interactor)
99-
lm := mw.NewLicenseMiddleware(c.Interactor)
100100

101-
r.Use(sm.User(), metrics.ReponseMetrics())
101+
gm := gb.NewMiddleware(c.Interactor)
102+
r.Use(
103+
gm.SetUser(),
104+
metrics.CollectRequestMetrics(),
105+
)
102106

103107
v1 := r.Group("/api/v1")
104108
{
105-
// Only authorized user can access to API.
106-
v1.Use(mw.OnlyAuthorized())
107-
// Check license expired.
108-
v1.Use(lm.IsExpired())
109+
m := shared.NewMiddleware(c.Interactor)
110+
v1.Use(
111+
m.OnlyAuthorized(),
112+
m.IsLicenseExpired(),
113+
)
109114
}
110115

111116
syncv1 := v1.Group("/sync")

0 commit comments

Comments
 (0)