Skip to content

Commit

Permalink
Removed chi router
Browse files Browse the repository at this point in the history
  • Loading branch information
joeychilson committed Dec 21, 2023
1 parent a875289 commit 4e86162
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/joeychilson/hackernews

go 1.21.5

require (
github.com/a-h/templ v0.2.476 // indirect
github.com/go-chi/chi/v5 v5.0.11 // indirect
)
require github.com/a-h/templ v0.2.476
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/a-h/templ v0.2.476 h1:+H4hP4CwK4kfJwXsE6kHeFWMGtcVOVoOm/I64uzARBk=
github.com/a-h/templ v0.2.476/go.mod h1:zQ95mSyadNTGHv6k5Fm+wQU8zkBMMbHCHg7eAvUZKNM=
github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
42 changes: 17 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/joeychilson/hackernews/client"
"github.com/joeychilson/hackernews/handlers"
"github.com/joeychilson/hackernews/pages"
Expand All @@ -18,37 +16,31 @@ var assets embed.FS
func main() {
client := client.New()

router := chi.NewRouter()

router.Use(middleware.RequestID)
router.Use(middleware.RealIP)
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
mux := http.NewServeMux()

// Redirects to GitHub repo
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
pages.NotFound().Render(r.Context(), w)
return
}
http.Redirect(w, r, "/news", http.StatusFound)
})

// Assets
router.Handle("/assets/*", http.FileServer(http.FS(assets)))
mux.Handle("/assets/", http.FileServer(http.FS(assets)))

// Pages
router.HandleFunc("/ask", handlers.HandleAsk(client))
router.HandleFunc("/item", handlers.HandleItem(client))
router.HandleFunc("/jobs", handlers.HandleJobs(client))
router.HandleFunc("/newest", handlers.HandleNewest(client))
router.HandleFunc("/news", handlers.HandleNews(client))
router.HandleFunc("/show", handlers.HandleShow(client))
router.HandleFunc("/submitted", handlers.HandleSubmitted(client))
router.HandleFunc("/threads", handlers.HandleThreads(client))
router.HandleFunc("/user", handlers.HandleUser(client))

// Not Found
router.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
pages.NotFound().Render(r.Context(), w)
})
mux.HandleFunc("/ask", handlers.HandleAsk(client))
mux.HandleFunc("/item", handlers.HandleItem(client))
mux.HandleFunc("/jobs", handlers.HandleJobs(client))
mux.HandleFunc("/newest", handlers.HandleNewest(client))
mux.HandleFunc("/news", handlers.HandleNews(client))
mux.HandleFunc("/show", handlers.HandleShow(client))
mux.HandleFunc("/submitted", handlers.HandleSubmitted(client))
mux.HandleFunc("/threads", handlers.HandleThreads(client))
mux.HandleFunc("/user", handlers.HandleUser(client))

fmt.Println("Listening on http://localhost:8080")
http.ListenAndServe(":8080", router)
http.ListenAndServe(":8080", mux)
}

0 comments on commit 4e86162

Please sign in to comment.