Skip to content

Commit

Permalink
Move to Chi
Browse files Browse the repository at this point in the history
  • Loading branch information
sudermanjr committed Feb 18, 2023
1 parent c14247b commit 84e1807
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 // indirect
github.com/brutella/dnssd v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/gosexy/to v0.0.0-20141221203644-c20e083e3123 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/miekg/dns v1.1.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
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/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gosexy/to v0.0.0-20141221203644-c20e083e3123 h1:6Q7VB4v0aEgIE6BtsbJhEH0KgFE0f+FHAxXePQp9Klc=
Expand Down
24 changes: 7 additions & 17 deletions pkg/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"text/template"

"github.com/gorilla/mux"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"

"github.com/sudermanjr/led-controller/pkg/color"
Expand All @@ -27,7 +27,7 @@ var (

// App encapsulates all the config for the server
type App struct {
Router *mux.Router
Router *chi.Mux
Port int
Array *neopixel.LEDArray
Screen *screen.Display
Expand Down Expand Up @@ -82,17 +82,16 @@ func (a *App) writeTemplate(tmpl *template.Template, data string, w http.Respons

// Initialize sets up an instance of App
func (a *App) Initialize() {
router := mux.NewRouter()
router.NotFoundHandler = a.handle404()
router := chi.NewRouter()

//API
router.HandleFunc("/health", a.health).Methods("GET")
router.HandleFunc("/control", a.control).Methods("POST")
router.HandleFunc("/demo", a.demo).Methods("POST")
router.Get("/health", a.health)
router.Post("/control", a.control)
router.Post("/demo", a.demo)

// HTML Dashboard
fileServer := http.FileServer(http.FS(assets))
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fileServer))
router.Handle("/static/*", fileServer)
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
Expand All @@ -116,7 +115,6 @@ func (a *App) Initialize() {

// Run starts the http server
func (a *App) Run() {
http.Handle("/", a.Router)
a.Logger.Infow("starting server", "port", a.Port)
go a.WatchButton()
defer a.Array.WS.Fini()
Expand All @@ -125,14 +123,6 @@ func (a *App) Run() {
}
}

// Handle404 handles the not found error and logs it
func (a *App) handle404() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
a.Logger.Warnw("serving 404", "request", r)
http.Error(w, "Not Found", http.StatusNotFound)
})
}

// rootHandler gets template data and renders the dashboard with it.
func (a *App) rootHandler(w http.ResponseWriter, r *http.Request) {

Expand Down

0 comments on commit 84e1807

Please sign in to comment.