Skip to content

Commit 6f4831a

Browse files
committed
add limiter
1 parent 54d6ee1 commit 6f4831a

10 files changed

Lines changed: 105 additions & 17 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.DS_Store
22
tmp
3-
./dist
43
repo.png
54
github_link_creator
5+
dist

docker-entrypoint.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ set -e
88

99
export GITHUB_CLIENT_ID=$(aws ssm get-parameters --name /ghlinkcard/github/clientid --query "Parameters[0].Value" --region ap-northeast-1 --output text)
1010
export GITHUB_SECRET=$(aws ssm get-parameters --name /ghlinkcard/github/clientsecret --with-decryption --query "Parameters[0].Value" --region ap-northeast-1 --output text)
11+
export SENTRY_DSN=$(aws ssm get-parameters --name /ghlinkcard/sentry/dsn --with-decryption --query "Parameters[0].Value" --region ap-northeast-1 --output text)
1112

12-
exec "$@"
13+
exec "$@"

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module github.com/po3rin/github_link_creator
22

33
require (
44
github.com/aws/aws-sdk-go v1.16.26
5+
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 // indirect
6+
github.com/evalphobia/logrus_sentry v0.8.0
7+
github.com/getsentry/raven-go v0.2.0
58
github.com/gin-contrib/cors v0.0.0-20190101123304-5e7acb10687f
69
github.com/gin-gonic/contrib v0.0.0-20181101072842-54170a7b0b4b
710
github.com/gin-gonic/gin v1.3.0
@@ -15,4 +18,5 @@ require (
1518
github.com/sirupsen/logrus v1.3.0
1619
github.com/stretchr/testify v1.3.0
1720
golang.org/x/image v0.0.0-20190118043309-183bebdce1b2
21+
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c
1822
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
github.com/aws/aws-sdk-go v1.16.26 h1:GWkl3rkRO/JGRTWoLLIqwf7AWC4/W/1hMOUZqmX0js4=
22
github.com/aws/aws-sdk-go v1.16.26/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
3+
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448 h1:8tNk6SPXzLDnATTrWoI5Bgw9s/x4uf0kmBpk21NZgI4=
4+
github.com/certifi/gocertifi v0.0.0-20190105021004-abcd57078448/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
35
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
46
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
57
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/evalphobia/logrus_sentry v0.8.0 h1:BcXJRqUsqJwmFBGflmPzFSdrQhRzfQkyb+42XLMyTuM=
9+
github.com/evalphobia/logrus_sentry v0.8.0/go.mod h1:pKcp+vriitUqu9KiWj/VRFbRfFNUwz95/UkgG8a6MNc=
10+
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
11+
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
612
github.com/gin-contrib/cors v0.0.0-20190101123304-5e7acb10687f h1:iYwRrkSI4/6UeKFeqshIoreaNjzj6pm08J0cc1M/ZZc=
713
github.com/gin-contrib/cors v0.0.0-20190101123304-5e7acb10687f/go.mod h1:pL2kNE+DgDU+eQ+dary5bX0Z6LPP8nR6Mqs1iejILw4=
814
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
@@ -63,6 +69,8 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
6369
golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
6470
golang.org/x/sys v0.0.0-20190124100055-b90733256f2e h1:3GIlrlVLfkoipSReOMNAgApI0ajnalyLa/EZHHca/XI=
6571
golang.org/x/sys v0.0.0-20190124100055-b90733256f2e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
72+
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c h1:fqgJT0MGcGpPgpWU7VRdRjuArfcOvC4AoJmILihzhDg=
73+
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
6674
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6775
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6876
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=

handler/card.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func (h *Handler) GetCode(c *gin.Context) {
2929
Message: err.Error(),
3030
DocumentationURL: documentationURL,
3131
})
32-
l.Error(err)
3332
doneCh <- struct{}{}
3433
return
3534
}

infrastructure/limiter.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package infrastructure
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
l "github.com/po3rin/github_link_creator/lib/logger"
8+
"golang.org/x/time/rate"
9+
)
10+
11+
func limiter(r *rate.Limiter) gin.HandlerFunc {
12+
return func(c *gin.Context) {
13+
if err := r.Wait(c); err != nil {
14+
l.Warnf("To many requests, %v", c.Request)
15+
c.JSON(http.StatusTooManyRequests, "To many requests, Please wait for a while")
16+
c.Abort()
17+
}
18+
c.Next()
19+
}
20+
}

infrastructure/router.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/gin-gonic/contrib/static"
55
"github.com/gin-gonic/gin"
66
"github.com/po3rin/github_link_creator/handler"
7+
"golang.org/x/time/rate"
78
)
89

910
type Infrastructure interface {
@@ -21,6 +22,10 @@ func NewRouter() *Router {
2122
// InitRouter provide initialized router
2223
func (r *Router) InitRouter() *gin.Engine {
2324
router := gin.Default()
25+
26+
l := rate.NewLimiter(rate.Limit(1), 50)
27+
router.Use(limiter(l))
28+
2429
router.Use(static.Serve("/", static.LocalFile("client/dist", true)))
2530
router.GET("/api/v1/health", r.Handler.HealthCheck)
2631
router.GET("/api/v1/images/:user/:repo", r.Handler.GetCode)

lib/logger/init.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package logger
33
import (
44
"os"
55

6+
"github.com/evalphobia/logrus_sentry"
7+
"github.com/getsentry/raven-go"
68
"github.com/sirupsen/logrus"
79
)
810

@@ -21,8 +23,26 @@ func init() {
2123
case "ERROR":
2224
Log.Level = logrus.ErrorLevel
2325
default:
24-
Log.Level = logrus.ErrorLevel
26+
Log.Level = logrus.WarnLevel
2527
}
2628

2729
Log.Formatter = &logrus.TextFormatter{FullTimestamp: true, DisableColors: true}
28-
}
30+
31+
sentryDSN := os.Getenv("SENTRY_DSN")
32+
if sentryDSN == "" {
33+
return
34+
}
35+
client, err := raven.New(sentryDSN)
36+
if err != nil {
37+
Fatal(err)
38+
}
39+
hook, err := logrus_sentry.NewWithClientSentryHook(client, []logrus.Level{
40+
logrus.WarnLevel,
41+
logrus.ErrorLevel,
42+
logrus.FatalLevel,
43+
logrus.PanicLevel,
44+
})
45+
if err == nil {
46+
Log.Hooks.Add(hook)
47+
}
48+
}

pipeline/pipeline.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/po3rin/github_link_creator/config"
1010
"github.com/po3rin/github_link_creator/entity"
11+
l "github.com/po3rin/github_link_creator/lib/logger"
1112
"github.com/po3rin/img2circle"
1213
)
1314

@@ -30,17 +31,26 @@ func ProcessingImg(ctx context.Context, r Repoitory, userName string, repoName s
3031
}
3132
cropper, err := img2circle.NewCropper(img2circle.Params{Src: ResizeImg(img)})
3233
if err != nil {
34+
l.Error(err)
3335
return nil, err
3436
}
3537
synthesizedImg, err := SynthesizeToBase(cropper.CropCircle())
3638
if err != nil {
39+
l.Error(err)
3740
return nil, err
3841
}
3942

40-
img = DrawText(synthesizedImg, config.Title, repo.Name)
41-
43+
img, err = DrawText(synthesizedImg, config.Title, repo.Name)
44+
if err != nil {
45+
l.Error(err)
46+
return nil, err
47+
}
4248
if len(repo.Description) < 45 {
43-
img = DrawText(img, config.FirstDescription, repo.Description)
49+
img, err = DrawText(img, config.FirstDescription, repo.Description)
50+
if err != nil {
51+
l.Error(err)
52+
return nil, err
53+
}
4454
} else {
4555
desc := repo.Description
4656
if len(repo.Description) > 86 {
@@ -51,16 +61,31 @@ func ProcessingImg(ctx context.Context, r Repoitory, userName string, repoName s
5161
for _, w := range words {
5262
firstline += w + " "
5363
if len(firstline) >= 40 {
54-
img = DrawText(img, config.FirstDescription, firstline)
64+
img, err = DrawText(img, config.FirstDescription, firstline)
65+
if err != nil {
66+
l.Error(err)
67+
return nil, err
68+
}
5569
secondline = strings.TrimPrefix(desc, firstline)
56-
img = DrawText(img, config.SecondDescription, secondline)
70+
img, err = DrawText(img, config.SecondDescription, secondline)
71+
if err != nil {
72+
l.Error(err)
73+
return nil, err
74+
}
5775
break
5876
}
5977
}
6078
}
61-
62-
img = DrawText(img, config.Star, strconv.Itoa(repo.Stars))
63-
img = DrawText(img, config.Fork, strconv.Itoa(repo.Forks))
79+
img, err = DrawText(img, config.Star, strconv.Itoa(repo.Stars))
80+
if err != nil {
81+
l.Error(err)
82+
return nil, err
83+
}
84+
img, err = DrawText(img, config.Fork, strconv.Itoa(repo.Forks))
85+
if err != nil {
86+
l.Error(err)
87+
return nil, err
88+
}
6489

6590
return img, nil
6691
}

pipeline/draw.go renamed to pipeline/text.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
)
99

1010
// DrawText shorthand to draw text.
11-
func DrawText(img image.Image, c config.Text, text string) image.Image {
12-
d, _ := txt2img.NewDrawer(
11+
func DrawText(img image.Image, c config.Text, text string) (image.Image, error) {
12+
d, err := txt2img.NewDrawer(
1313
txt2img.Params{
1414
Img: img,
1515
FontSize: c.Size,
@@ -19,6 +19,12 @@ func DrawText(img image.Image, c config.Text, text string) image.Image {
1919
TextPosVertical: c.PosVertical,
2020
},
2121
)
22-
result, _ := d.Draw(text)
23-
return result
22+
if err != nil {
23+
return nil, err
24+
}
25+
result, err := d.Draw(text)
26+
if err != nil {
27+
return nil, err
28+
}
29+
return result, nil
2430
}

0 commit comments

Comments
 (0)