Skip to content

Commit 993db7b

Browse files
authored
Merge pull request #126 from jhrozek/chi
Use go-chi instead of gorilla as a router
2 parents eba3b13 + f1b04c4 commit 993db7b

File tree

24 files changed

+2609
-3001
lines changed

24 files changed

+2609
-3001
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ go 1.19
55
require (
66
github.com/cenkalti/backoff/v4 v4.2.1
77
github.com/fsnotify/fsnotify v1.6.0
8+
github.com/go-chi/chi/v5 v5.0.8
89
github.com/go-logr/logr v1.2.4
910
github.com/go-logr/zapr v1.2.4
10-
github.com/gorilla/mux v1.8.0
1111
github.com/mitchellh/go-homedir v1.1.0
1212
github.com/olekukonko/tablewriter v0.0.5
1313
github.com/onsi/ginkgo/v2 v2.11.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
6363
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
6464
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
6565
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
66+
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
67+
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
6668
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
6769
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
6870
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -131,8 +133,6 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
131133
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
132134
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
133135
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
134-
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
135-
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
136136
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
137137
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
138138
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=

pkg/daemon/status_server.go

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"time"
1212

1313
"github.com/containers/selinuxd/pkg/datastore"
14+
"github.com/go-chi/chi/v5"
1415
"github.com/go-logr/logr"
15-
"github.com/gorilla/mux"
1616
)
1717

1818
const (
@@ -53,7 +53,7 @@ func initStatusServer(cfg StatusServerConfig, ds datastore.ReadOnlyDataStore, l
5353
}
5454

5555
func (ss *statusServer) Serve(readychan <-chan bool) error {
56-
r := mux.NewRouter()
56+
r := chi.NewRouter()
5757
ss.initializeRoutes(r)
5858

5959
server := &http.Server{
@@ -75,25 +75,16 @@ func (ss *statusServer) waitForReady(readychan <-chan bool) {
7575
ss.l.Info("Status Server got READY signal")
7676
}
7777

78-
func (ss *statusServer) initializeRoutes(r *mux.Router) {
78+
func (ss *statusServer) initializeRoutes(r chi.Router) {
7979
// /policies/
80-
s := r.PathPrefix("/policies").Subrouter()
81-
s.HandleFunc("/", ss.listPoliciesHandler).
82-
Methods("GET")
83-
s.HandleFunc("/", ss.catchAllNotGetHandler)
84-
// IMPORTANT(jaosorior): We should better restrict what characters
85-
// does this handler accept
86-
s.HandleFunc("/{policy}", ss.getPolicyStatusHandler).
87-
Methods("GET")
88-
s.HandleFunc("/{policy}", ss.catchAllNotGetHandler)
89-
90-
// /policies -- without the trailing /
91-
r.HandleFunc("/policies", ss.listPoliciesHandler).
92-
Methods("GET")
93-
r.HandleFunc("/policies", ss.catchAllNotGetHandler)
94-
r.HandleFunc("/ready", ss.readyStatusHandler)
95-
r.HandleFunc("/ready/", ss.readyStatusHandler)
96-
r.HandleFunc("/", ss.catchAllHandler)
80+
r.Route("/policies", func(r chi.Router) {
81+
r.Get("/", ss.listPoliciesHandler)
82+
r.Get("/{policy}", ss.getPolicyStatusHandler)
83+
})
84+
85+
r.Get("/ready", ss.readyStatusHandler)
86+
r.Get("/ready/", ss.readyStatusHandler)
87+
r.Get("/", ss.catchAllHandler)
9788

9889
if ss.cfg.EnableProfiling {
9990
r.HandleFunc("/debug/pprof/", pprof.Index)
@@ -125,8 +116,7 @@ func (ss *statusServer) listPoliciesHandler(w http.ResponseWriter, r *http.Reque
125116
}
126117

127118
func (ss *statusServer) getPolicyStatusHandler(w http.ResponseWriter, r *http.Request) {
128-
vars := mux.Vars(r)
129-
policy := vars["policy"]
119+
policy := chi.URLParam(r, "policy")
130120
status, err := ss.ds.Get(policy)
131121
if errors.Is(err, datastore.ErrPolicyNotFound) {
132122
http.Error(w, "couldn't find requested policy", http.StatusNotFound)
@@ -159,10 +149,6 @@ func (ss *statusServer) catchAllHandler(w http.ResponseWriter, r *http.Request)
159149
http.Error(w, "Invalid path", http.StatusBadRequest)
160150
}
161151

162-
func (ss *statusServer) catchAllNotGetHandler(w http.ResponseWriter, r *http.Request) {
163-
http.Error(w, "Only GET is allowed", http.StatusBadRequest)
164-
}
165-
166152
func createSocket(path string, uid, gid int) (net.Listener, error) {
167153
if err := os.RemoveAll(path); err != nil {
168154
return nil, fmt.Errorf("cannot remove old socket: %w", err)

vendor/github.com/go-chi/chi/v5/.gitignore

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

0 commit comments

Comments
 (0)