GithubHelp home page GithubHelp logo

design a middleware system for web.go about web HOT 6 OPEN

hoisie avatar hoisie commented on June 17, 2024
design a middleware system for web.go

from web.

Comments (6)

nileflowers avatar nileflowers commented on June 17, 2024

Also, you can look at java servlets filters as another design choice with similar features. I can't really say if WSGI is simpler since I don't know it in great detail.

from web.

tredoe avatar tredoe commented on June 17, 2024

It would be great to have a WSGI clone (or anything similar).

Ruby got a WSGI clone too some time ago called Rack. And it's also possible to use WSGI middleware for App Engine applications.

http://wsgi.org/wsgi/

from web.

flavius avatar flavius commented on June 17, 2024

+1 for functionality similar to WSGI middlewares

from web.

yvsong avatar yvsong commented on June 17, 2024

If someone is working on this, may I suggest the design draft be published as RFC before implementation?

from web.

ysimonson avatar ysimonson commented on June 17, 2024

What about an express-like middleware system? IMO it's cleaner than WSGI-like middleware, because you use the same interface regardless of whether you're writing a handler or middleware. The change might be simpler too. It would consist of two additions; first, a way of adding global middleware that's activated for all requests. e.g. extending the hello world example:

package main

import (
    "github.com/hoisie/web"
    "fmt"
)

func someMiddleware(ctx *web.Context, urlArgs ...string) {
    fmt.Println("Middleware hit!")
}

func hello(ctx *web.Context, val string) { 
    for k,v := range ctx.Params {
        println(k, v)
    }
}   

func main() {
    web.Middleware(someMiddleware)
    web.Get("/(.*)", hello)
    web.Run("0.0.0.0:9999")
}

Then there'd also be a way to setup middleware on a per-endpoint basis, probably by changing methods like web.Get to accept multiple functions, e.g.

package main

import (
    "github.com/hoisie/web"
    "fmt"
)

func someMiddleware(ctx *web.Context, urlArgs ...string) {
    fmt.Println("Middleware hit!")
}

func hello(ctx *web.Context, val string) { 
    for k,v := range ctx.Params {
        println(k, v)
    }
}   

func main() {
    web.Get("/(.*)", someMiddleware, hello)
    web.Run("0.0.0.0:9999")
}

from web.

ysimonson avatar ysimonson commented on June 17, 2024

Made a PR for the above proposal in #152 .

from web.

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.