Giter Site home page Giter Site logo

Comments (4)

pkieltyka avatar pkieltyka commented on May 6, 2024 1

hey @buro9 - this can definitely be done, its just a matter of the approach. There isn't a way to opt out of a parent middleware stack, but that isn't quite necessary either.

I suggest something like..

func ListenAndServe() error {
    r := chi.NewRouter()

    r.Use(middleware.RequestID)
    r.Use(middleware.Logger)
    r.Use(middleware.Recoverer)
    r.Use(middleware.RedirectSlashes)

    // Protected routes.
    // This works by starting a new middleware stack for these routes.
    r.Group(func(r chi.Router) {
      r.Use(session)
      r.Get("/", homeGet)
    })

    r.Mount("/static", staticFiles())

    return http.ListenAndServeTLS(fmt.Sprintf(":%d", *listenPort), *certFile, *keyFile, r)
}

from chi.

pkieltyka avatar pkieltyka commented on May 6, 2024

or..

func ListenAndServe() error {
    r := chi.NewRouter()

    r.Use(middleware.RequestID)
    r.Use(middleware.Logger)
    r.Use(middleware.Recoverer)
    r.Use(middleware.RedirectSlashes)

    // or..
    r.Mount("/", appRoutes())

    // or.. which is effectively the same as r.Mount("/", anotherR),
    // Route() is just an inline definition
    r.Route("/", func(r chi.Router) {
      r.Use(session)
      r.Get("/", homeGet)
    })


    r.Mount("/static", staticFiles())

    return http.ListenAndServeTLS(fmt.Sprintf(":%d", *listenPort), *certFile, *keyFile, r)
}

func appRoutes() http.Handler {
  r := chi.NewRouter()
  r.Use(session)
  r.Get("/", h)
  r.Get("/etc", h2)
  r.Mount("/other", r2) // which will nest the paths
  return r
}

from chi.

buro9 avatar buro9 commented on May 6, 2024

Thanks for that, I'll go with the first r.Group method as it reads nicer.

Actually going to take it further and put no middleware at the top level and then define groups for all routes.

i.e.

package ui

import (
    "fmt"
    "net/http"

    "github.com/pressly/chi"
    "github.com/pressly/chi/middleware"
)

func ListenAndServe() error {
    r := chi.NewRouter()

    // Pages group, handles all routes for pages and defines the appropriate
    // middleware for web pages
    r.Group(func(r chi.Router) {
        r.Use(middleware.RequestID)
        r.Use(middleware.Logger)
        r.Use(middleware.RedirectSlashes)
        r.Use(middleware.Recoverer)
        r.Use(session)

        r.Get("/", homeGet)
    })

    // Static file group, defines minimal middleware
    r.Group(func(r chi.Router) {
        r.Use(middleware.Logger)

        r.Mount("/static", staticFiles())
        r.Get("/favicon.ico", favicon)
        r.Get("/robots.txt", robots)
    })

    return http.ListenAndServeTLS(
        fmt.Sprintf(":%d", *listenPort),
        *certFile,
        *keyFile,
        r,
    )
}

func staticFiles() http.Handler {
    r := chi.NewRouter()

    // Do nothing, but implement http.Handler
    r.Use(func(next http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            next.ServeHTTP(w, r)
        })
    })

    // Serve static files
    r.Mount("/",
        http.StripPrefix(
            "/static/",
            http.FileServer(http.Dir(*filesPath+"/static/")),
        ),
    )

    return r
}

from chi.

pkieltyka avatar pkieltyka commented on May 6, 2024

:)

from chi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.