Skip to content

Commit

Permalink
Merge pull request #126 from jhrozek/chi
Browse files Browse the repository at this point in the history
Use go-chi instead of gorilla as a router
  • Loading branch information
jhrozek authored Jun 19, 2023
2 parents eba3b13 + f1b04c4 commit 993db7b
Show file tree
Hide file tree
Showing 24 changed files with 2,609 additions and 3,001 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ go 1.19
require (
github.com/cenkalti/backoff/v4 v4.2.1
github.com/fsnotify/fsnotify v1.6.0
github.com/go-chi/chi/v5 v5.0.8
github.com/go-logr/logr v1.2.4
github.com/go-logr/zapr v1.2.4
github.com/gorilla/mux v1.8.0
github.com/mitchellh/go-homedir v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/onsi/ginkgo/v2 v2.11.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -131,8 +133,6 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down
38 changes: 12 additions & 26 deletions pkg/daemon/status_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"time"

"github.com/containers/selinuxd/pkg/datastore"
"github.com/go-chi/chi/v5"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
)

const (
Expand Down Expand Up @@ -53,7 +53,7 @@ func initStatusServer(cfg StatusServerConfig, ds datastore.ReadOnlyDataStore, l
}

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

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

func (ss *statusServer) initializeRoutes(r *mux.Router) {
func (ss *statusServer) initializeRoutes(r chi.Router) {
// /policies/
s := r.PathPrefix("/policies").Subrouter()
s.HandleFunc("/", ss.listPoliciesHandler).
Methods("GET")
s.HandleFunc("/", ss.catchAllNotGetHandler)
// IMPORTANT(jaosorior): We should better restrict what characters
// does this handler accept
s.HandleFunc("/{policy}", ss.getPolicyStatusHandler).
Methods("GET")
s.HandleFunc("/{policy}", ss.catchAllNotGetHandler)

// /policies -- without the trailing /
r.HandleFunc("/policies", ss.listPoliciesHandler).
Methods("GET")
r.HandleFunc("/policies", ss.catchAllNotGetHandler)
r.HandleFunc("/ready", ss.readyStatusHandler)
r.HandleFunc("/ready/", ss.readyStatusHandler)
r.HandleFunc("/", ss.catchAllHandler)
r.Route("/policies", func(r chi.Router) {
r.Get("/", ss.listPoliciesHandler)
r.Get("/{policy}", ss.getPolicyStatusHandler)
})

r.Get("/ready", ss.readyStatusHandler)
r.Get("/ready/", ss.readyStatusHandler)
r.Get("/", ss.catchAllHandler)

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

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

func (ss *statusServer) catchAllNotGetHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Only GET is allowed", http.StatusBadRequest)
}

func createSocket(path string, uid, gid int) (net.Listener, error) {
if err := os.RemoveAll(path); err != nil {
return nil, fmt.Errorf("cannot remove old socket: %w", err)
Expand Down
3 changes: 3 additions & 0 deletions vendor/github.com/go-chi/chi/v5/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 993db7b

Please sign in to comment.