Skip to content

Commit 8d8d456

Browse files
authored
Merge pull request #1 from steemax/paa.init
v0.1
2 parents e65726b + 7d49d6a commit 8d8d456

6 files changed

Lines changed: 79 additions & 124 deletions

File tree

.traefik.yml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
displayName: Demo Plugin
1+
# The name of your plugin as displayed in the Plugins Catalog web UI.
2+
displayName: Rate Limit
3+
4+
# For now, `middleware` is the only type available.
25
type: middleware
3-
iconPath: .assets/icon.png
46

5-
import: github.com/traefik/plugindemo
7+
# The import path of your plugin.
8+
import: github.com/kav789/traefik-ratelimit
9+
10+
# A brief description of what your plugin is doing.
11+
summary: Rate Limit
612

7-
summary: '[Demo] Add Request Header'
13+
# Medias associated to the plugin (optional)
14+
iconPath: .assets/icon.png
815

16+
# Configuration data for your plugin.
17+
# This is mandatory,
18+
# and Plugins Catalog will try to execute the plugin with the data you provide as part of its startup validity tests.
919
testData:
10-
Headers:
11-
X-Demo: test
12-
X-URL: '{{URL}}'
20+
rate: 100

demo.go

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

demo_test.go

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

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module github.com/traefik/plugindemo
1+
module github.com/kav789/traefik-ratelimit
22

3-
go 1.19
3+
go 1.18

main.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package traefik_ratelimit
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"net/http"
8+
"os"
9+
)
10+
11+
func CreateConfig() *Config {
12+
return &Config{}
13+
}
14+
15+
type Config struct {
16+
Rate int `json:"rate,omitempty"`
17+
}
18+
19+
type RateLimit struct {
20+
name string
21+
next http.Handler
22+
rate int
23+
config *Config
24+
}
25+
26+
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
27+
mlog(fmt.Sprintf("config %v", config))
28+
return &RateLimit{
29+
name: name,
30+
next: next,
31+
config: config,
32+
}, nil
33+
}
34+
35+
func (r *RateLimit) Allow(ctx context.Context, req *http.Request, rw http.ResponseWriter) bool {
36+
return true
37+
}
38+
39+
func (r *RateLimit) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
40+
encoder := json.NewEncoder(rw)
41+
reqCtx := req.Context()
42+
if r.Allow(reqCtx, req, rw) {
43+
r.next.ServeHTTP(rw, req)
44+
return
45+
}
46+
rw.Header().Set("Content-Type", "application/json")
47+
rw.WriteHeader(http.StatusTooManyRequests)
48+
encoder.Encode(map[string]any{"status_code": http.StatusTooManyRequests, "message": "rate limit exceeded, try again later"})
49+
return
50+
}
51+
52+
func mlog(args ...any) {
53+
os.Stdout.WriteString(fmt.Sprintf("[rate-limit-middleware-plugin] %s\n", fmt.Sprint(args...)))
54+
}

main_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package traefik_ratelimit
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMain(t *testing.T) {
8+
}

0 commit comments

Comments
 (0)