Skip to content

Commit 9cd64c2

Browse files
committed
refact: better api mux
1 parent fe14c1c commit 9cd64c2

File tree

20 files changed

+1105
-1041
lines changed

20 files changed

+1105
-1041
lines changed

go.mod

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,65 @@ module kubegems.io/library
33
go 1.20
44

55
require (
6-
github.com/casbin/casbin/v2 v2.73.0
7-
github.com/containers/image/v5 v5.26.1
8-
github.com/coreos/go-oidc/v3 v3.6.0
9-
github.com/go-logr/logr v1.2.4
10-
github.com/go-logr/zapr v1.2.4
6+
github.com/containers/image/v5 v5.29.0
7+
github.com/coreos/go-oidc/v3 v3.7.0
8+
github.com/go-logr/logr v1.3.0
9+
github.com/go-logr/zapr v1.3.0
1110
github.com/go-openapi/spec v0.20.9
11+
github.com/go-playground/validator/v10 v10.16.0
1212
github.com/jinzhu/inflection v1.0.0
13-
github.com/opencontainers/distribution-spec/specs-go v0.0.0-20230727214836-6bc87156eacf
13+
github.com/opencontainers/distribution-spec/specs-go v0.0.0-20231117024018-3ec8a56d897b
1414
github.com/spf13/pflag v1.0.5
15-
github.com/spf13/viper v1.16.0
16-
go.uber.org/zap v1.24.0
17-
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691
18-
golang.org/x/net v0.12.0
15+
github.com/spf13/viper v1.17.0
16+
go.uber.org/zap v1.26.0
17+
golang.org/x/crypto v0.15.0
18+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
19+
golang.org/x/net v0.18.0
1920
k8s.io/apimachinery v0.27.4
2021
)
2122

2223
require (
23-
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
24-
github.com/containers/storage v1.48.0 // indirect
25-
github.com/fsnotify/fsnotify v1.6.0 // indirect
26-
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
27-
github.com/go-openapi/jsonpointer v0.19.6 // indirect
28-
github.com/go-openapi/jsonreference v0.20.1 // indirect
24+
github.com/containers/storage v1.51.0 // indirect
25+
github.com/fsnotify/fsnotify v1.7.0 // indirect
26+
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
27+
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
28+
github.com/go-openapi/jsonpointer v0.20.0 // indirect
29+
github.com/go-openapi/jsonreference v0.20.2 // indirect
2930
github.com/go-openapi/swag v0.22.4 // indirect
31+
github.com/go-playground/locales v0.14.1 // indirect
32+
github.com/go-playground/universal-translator v0.18.1 // indirect
3033
github.com/gogo/protobuf v1.3.2 // indirect
3134
github.com/golang/protobuf v1.5.3 // indirect
3235
github.com/google/gofuzz v1.2.0 // indirect
3336
github.com/hashicorp/hcl v1.0.0 // indirect
3437
github.com/josharian/intern v1.0.0 // indirect
3538
github.com/json-iterator/go v1.1.12 // indirect
39+
github.com/leodido/go-urn v1.2.4 // indirect
3640
github.com/magiconair/properties v1.8.7 // indirect
3741
github.com/mailru/easyjson v0.7.7 // indirect
3842
github.com/mitchellh/mapstructure v1.5.0 // indirect
3943
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4044
github.com/modern-go/reflect2 v1.0.2 // indirect
4145
github.com/opencontainers/go-digest v1.0.0 // indirect
42-
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
43-
github.com/spf13/afero v1.9.5 // indirect
46+
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
47+
github.com/sagikazarmark/locafero v0.3.0 // indirect
48+
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
49+
github.com/sourcegraph/conc v0.3.0 // indirect
50+
github.com/spf13/afero v1.10.0 // indirect
4451
github.com/spf13/cast v1.5.1 // indirect
45-
github.com/spf13/jwalterweatherman v1.1.0 // indirect
46-
github.com/subosito/gotenv v1.4.2 // indirect
47-
github.com/tidwall/gjson v1.14.4 // indirect
48-
github.com/tidwall/match v1.1.1 // indirect
49-
github.com/tidwall/pretty v1.2.0 // indirect
50-
go.uber.org/atomic v1.9.0 // indirect
51-
go.uber.org/goleak v1.2.0 // indirect
52-
go.uber.org/multierr v1.8.0 // indirect
53-
golang.org/x/crypto v0.11.0 // indirect
54-
golang.org/x/oauth2 v0.9.0 // indirect
55-
golang.org/x/sys v0.10.0 // indirect
56-
golang.org/x/text v0.11.0 // indirect
57-
google.golang.org/appengine v1.6.7 // indirect
58-
google.golang.org/protobuf v1.30.0 // indirect
52+
github.com/subosito/gotenv v1.6.0 // indirect
53+
go.uber.org/multierr v1.11.0 // indirect
54+
golang.org/x/oauth2 v0.14.0 // indirect
55+
golang.org/x/sys v0.14.0 // indirect
56+
golang.org/x/text v0.14.0 // indirect
57+
google.golang.org/appengine v1.6.8 // indirect
58+
google.golang.org/protobuf v1.31.0 // indirect
5959
gopkg.in/inf.v0 v0.9.1 // indirect
6060
gopkg.in/ini.v1 v1.67.0 // indirect
6161
gopkg.in/yaml.v2 v2.4.0 // indirect
6262
gopkg.in/yaml.v3 v3.0.1 // indirect
6363
k8s.io/klog/v2 v2.90.1 // indirect
6464
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 // indirect
6565
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
66-
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
66+
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
6767
)

go.sum

Lines changed: 88 additions & 82 deletions
Large diffs are not rendered by default.

rest/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# REST
2+
3+
help build a rest api easily

rest/api/attributes.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package api
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"strings"
7+
)
8+
9+
type AttrbuteResource struct {
10+
Resource string `json:"resource,omitempty"`
11+
Name string `json:"name,omitempty"`
12+
}
13+
14+
type Attributes struct {
15+
Action string `json:"action,omitempty"`
16+
Resources []AttrbuteResource `json:"resources,omitempty"`
17+
Path string `json:"path,omitempty"`
18+
}
19+
20+
type AttributeExtractor func(r *http.Request) (*Attributes, error)
21+
22+
func PrefixedAttributesExtractor(prefix string) AttributeExtractor {
23+
return func(r *http.Request) (*Attributes, error) {
24+
if !strings.HasPrefix(r.URL.Path, prefix) {
25+
return nil, nil
26+
}
27+
method, path := r.Method, strings.TrimPrefix(r.URL.Path, prefix)
28+
action, resources := DefaultRestAttributeExtractor(method, path)
29+
return &Attributes{Action: action, Resources: resources, Path: path}, nil
30+
}
31+
}
32+
33+
// plural
34+
var MethodActionMapPlural = map[string]string{
35+
"GET": "list",
36+
"POST": "create",
37+
"DELETE": "removeBatch",
38+
}
39+
40+
// singular plural
41+
var MethodActionMapSingular = map[string]string{
42+
"GET": "get",
43+
"PUT": "update",
44+
"DELETE": "remove",
45+
"PATCH": "patch",
46+
}
47+
48+
func DefaultRestAttributeExtractor(method string, path string) (string, []AttrbuteResource) {
49+
// example:
50+
// /api/v1/namespaces/default/pods/nginx-xxx -> ["namespaces", "default", "pods", "nginx-xxx"]
51+
// /api/v1/namespaces/default/pods -> ["namespaces", "default", "pods"]
52+
// /api/v1/namespaces/default -> ["namespaces", "default"]
53+
// /api/v1/namespaces -> ["namespaces"]
54+
// /api/v1 -> []
55+
resource, action := splitResourceAction(path)
56+
parts := removeEmpty(strings.Split(resource, "/"))
57+
if len(parts) == 0 {
58+
return action, nil
59+
}
60+
// if odd, it's a list request, e.g. GET /api/v1/namespaces/default/pods
61+
if len(parts)%2 != 0 {
62+
parts = append(parts, "")
63+
if action == "" {
64+
action = string(MethodActionMapPlural[method])
65+
}
66+
} else {
67+
if action == "" {
68+
action = string(MethodActionMapSingular[method])
69+
}
70+
}
71+
resources := []AttrbuteResource{}
72+
for i := 0; i < len(parts); i += 2 {
73+
resources = append(resources, AttrbuteResource{Resource: parts[i], Name: parts[i+1]})
74+
}
75+
return action, resources
76+
}
77+
78+
func removeEmpty(arr []string) []string {
79+
w := 0
80+
for _, v := range arr {
81+
if v != "" {
82+
arr[w] = v
83+
w++
84+
}
85+
}
86+
return arr[:w]
87+
}
88+
89+
// e.g. /zoos/{id}/animals/{name}:feed -> /zoos/{id}/animals/{name},feed
90+
func splitResourceAction(path string) (string, string) {
91+
if i := strings.LastIndex(path, ":"); i < 0 {
92+
return path, ""
93+
} else {
94+
return path[:i], path[i+1:]
95+
}
96+
}
97+
98+
func AttributeFilter(attributer AttributeExtractor) Filter {
99+
return FilterFunc(func(w http.ResponseWriter, r *http.Request, next http.Handler) {
100+
attributes, err := attributer(r)
101+
if err != nil {
102+
http.Error(w, err.Error(), http.StatusInternalServerError)
103+
return
104+
}
105+
ctx := WithAttributes(r.Context(), attributes)
106+
next.ServeHTTP(w, r.WithContext(ctx))
107+
})
108+
}
109+
110+
type attributesContext struct{}
111+
112+
var attributesContextKey = &attributesContext{}
113+
114+
func WithAttributes(ctx context.Context, attributes *Attributes) context.Context {
115+
return context.WithValue(ctx, attributesContextKey, attributes)
116+
}
117+
118+
func AttributesFromContext(ctx context.Context) *Attributes {
119+
if attributes, ok := ctx.Value(attributesContextKey).(*Attributes); ok {
120+
return attributes
121+
}
122+
return nil
123+
}

0 commit comments

Comments
 (0)