GithubHelp home page GithubHelp logo

Match route in middleware about goji HOT 2 CLOSED

goji avatar goji commented on May 26, 2024
Match route in middleware

from goji.

Comments (2)

vidfamn avatar vidfamn commented on May 26, 2024

I solved my use case in a bit dubious manner with a type assert and string matching instead of using the pattern.Match(ctx, r) function.

Example below, if anyone has a similar issue while porting middleware from zenazn/goji to goji/goji.

package main

import (
    "fmt"
    "net/http"

    "goji.io"
    "goji.io/middleware"
    "goji.io/pat"
    "golang.org/x/net/context"
)

func hello(ctx context.Context, w http.ResponseWriter, r *http.Request) {
    name := pat.Param(ctx, "name")
    fmt.Fprintf(w, "Hello, %s!", name)
}

var routes = []string{
    "/", "/hello", "/hello/:name",
}

func myMiddleware(h goji.Handler) goji.Handler {
    fn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
        gojiPattern := middleware.Pattern(ctx)
        if gojiPattern == nil {
            h.ServeHTTPC(ctx, w, r)
            return
        }

        requestResource := gojiPattern.(*pat.Pattern).String()

        for _, resource := range routes {
            if requestResource == resource {
                fmt.Println("matched saved resource!")
                // do stuff
                break
            }
        }

        h.ServeHTTPC(ctx, w, r)
    }

    return goji.HandlerFunc(fn)
}

func main() {
    mux := goji.NewMux()
    mux.HandleFuncC(pat.Get("/hello/:name"), hello)
    mux.UseC(myMiddleware)

    http.ListenAndServe("localhost:8000", mux)
}

from goji.

zenazn avatar zenazn commented on May 26, 2024

This issue has been idle for a few months now, so I'm closing it to keep everything tidy. It looks like you were able to solve your problem using type assertions in middleware, but if you're still in the market for solutions this might be the sort of problem that is best solved using custom Pattern types.

from goji.

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.