Coder Social home page Coder Social logo

flow's Introduction

Flow

Go Reference Go Report Card MIT Code size

A delightfully tiny but powerful HTTP router for Go web applications


Flow packs in a bunch of features that you'll probably like:

  • Use named parameters, wildcards and (optionally) regexp patterns in your routes.
  • Create route groups which use different middleware (a bit like chi).
  • Customizable handlers for 404 Not Found and 405 Method Not Allowed responses.
  • Automatic handling of OPTIONS and HEAD requests.
  • Works with http.Handler, http.HandlerFunc, and standard Go middleware.
  • Zero dependencies.
  • Tiny, readable, codebase (~160 lines of code).

Installation

$ go get github.com/alexedwards/flow@latest

Basic example

package main

import (
    "fmt"
    "log"
    "net/http"

    "github.com/alexedwards/flow"
)

func main() {
    // Initialize a new router.
    mux := flow.New()

    // Add a `GET /greet/:name` route. The : character is used to denote a 
    // named parameter in the URL path, which acts like a 'wildcard'.
    mux.HandleFunc("/greet/:name", greet, "GET")

    err := http.ListenAndServe(":2323", mux)
    log.Fatal(err)
}

func greet(w http.ResponseWriter, r *http.Request) {
    // Use flow.Param() to retrieve the value of the named parameter from the
    // request context.
    name := flow.Param(r.Context(), "name")

    fmt.Fprintf(w, "Hello %s", name)
}

Kitchen-sink example

mux := flow.New()

// The Use() method can be used to register middleware. Middleware declared at
// the top level will used on all routes (including error handlers and OPTIONS
// responses).
mux.Use(exampleMiddleware1)

// Routes can use multiple HTTP methods.
mux.HandleFunc("/profile/:name", exampleHandlerFunc1, "GET", "POST")

// Optionally, regular expressions can be used to enforce a specific pattern
// for a named parameter.
mux.HandleFunc("/profile/:name/:age|^[0-9]{1,3}$", exampleHandlerFunc2, "GET")

// The wildcard ... can be used to match the remainder of a request path.
// Notice that HTTP methods are also optional (if not provided, all HTTP
// methods will match the route). The value of the wildcard can be retrieved 
// by calling flow.Param("...").
mux.Handle("/static/...", exampleHandler)

// You can create route 'groups'.
mux.Group(func(mux *flow.Mux) {
    // Middleware declared within in the group will only be used on the routes
    // in the group.
    mux.Use(exampleMiddleware2)

    mux.HandleFunc("/admin", exampleHandlerFunc3, "GET")

    // Groups can be nested.
    mux.Group(func(mux *flow.Mux) {
        mux.Use(exampleMiddleware3)

        mux.HandleFunc("/admin/passwords", exampleHandlerFunc4, "GET")
    })
})

Notes

  • Conflicting routes are permitted (e.g. /posts/:id and posts/new). Routes are matched in the order that they are declared.
  • Trailing slashes are significant (/profile/:id and /profile/:id/ are not the same).
  • An Allow header is automatically set for all OPTIONS and 405 Method Not Allowed responses (including when using custom handlers).
  • Once the flow.Mux type is being used by your server, it is not safe to add more middleware or routes concurrently.
  • Middleware must be declared before a route in order to be used by that route. Any middleware declared after a route won't act on that route. For example:
mux := flow.New()
mux.Use(middleware1)
mux.HandleFunc("/foo", ...) // This route will use middleware1 only.
mux.Use(middleware2)
mux.HandleFunc("/bar", ...) // This route will use both middleware1 and middleware2.

Contributing

Bug fixes and documentation improvements are very welcome! For feature additions or behavioral changes, please open an issue to discuss the change before submitting a PR.

Thanks

The pattern matching logic for Flow was heavily inspired by matryer/way.

flow's People

Contributors

alexedwards avatar chilic avatar yurivish avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

flow's Issues

Mounting suppot

Very nice router! I like it very much.
Have a question. How can I mount multiple sets of routes like chi.Mount() does. For example I have some subdomens/components, each with their own routes set. How can I wire them together for passing to main http.Server?

Question: why making a copy rather than passing the derefrenced value directly?

https://github.com/alexedwards/flow/blob/1828f587dbe5c8a465ca74250f696e1d51f89f79/flow.go#L161C1-L164C2

Hello Mr. Edwards,
hope you are doing well!

func (m *Mux) Group(fn func(*Mux)) {
	mm := *m
	fn(&mm)
}

I wonder why making a copy of m and then passing the address of the newly created copy to the function, isn't better to update the function declaration to accept Mux instead of *Mux and passing the dereferenced value of m?

func (m *Mux) Group(fn func(Mux)) {
	fn(*m)
}

Comparison

The functionality will remain the same.
The differences between them are:

  • in the current implementation, a copy of m is created, then a copy of the address will be created, and the (*Mux).Group function contains 2 lines of code.
  • in the proposed implementation, a copy of m will be created, and the (*Mux).Group contains only 1 line of code.

Unexpected % Appended To Param Value.

I'm working through Let's Go Further for the second time. I'm using the Flow router this time. I'm on chapter 2 of the book and I've built out 3 basic routes.

package main

import (
	"net/http"

	"github.com/alexedwards/flow"
)

func (app *application) routes() http.Handler {

	// initialize a new router (flow router from Alex Edwards)
	router := flow.New()

	// Routes --------------------------------------------- //
	router.HandleFunc("/v1/healthcheck", app.healthcheckHandler, "GET")
	router.HandleFunc("/v1/widgets", app.createWidgetHandler, "POST")
	router.HandleFunc("/v1/widgets/:id", app.getWidgetHandler, "GET")

	return router

}

Here's my getWidgetHandler function:

func (app *application) getWidgetHandler(w http.ResponseWriter, r *http.Request) {

	// get the id value as a string.
	stringId := flow.Param(r.Context(), "id")

	fmt.Fprintf(w, "id: %s", stringId)

}

I sent the following curl request:

curl localhost:4000/v1/widgets/123

I would expect to get the following output:

id: 123

However, I'm getting:

id: 123%

Oddly enough, if I use fmt.Println(stringId) the output is 123.

Am I doing something wrong? Or is this a bug?

Thanks,

Chris

Suggestions

Very cool! I like this a lot. I wrote my own router which is also a sort of stripped down take on Chi/Way/httprouter. It's interesting to see more versions of this idea.

Some thoughts:

  • .Use(mw) can be changed to .Use(mws...) to save some typing in some cases.
  • Integer params come up a lot, so func ParamInt[Int int|int32uint32|int64|uint64](ctx context.Context, param string, n *Int) (ok bool) would be nice to have.
  • I find that mounting static files also comes up a lot, so I wrote a Router.Mount convenience method. Something to think about.
  • It would be faster at route time but also better in terms of failing fast to parse the regexes up front. I suggest changing routes to
type route struct {
	method   string
	segments []string
	matchers []*regexp.Regexp 
	wildcard bool
	handler  http.Handler
}

and Handle to

	segments := strings.Split(pattern, "/")
	var matchers []*regexp.Regexp
	for _, seg := range segments {
		key, rxpattern, ok := strings.Cut(seg, "|")
		if !ok {
			continue
		}
		rx := regexp.MustCompile(rxpattern)
		matchers = append(matchers, rx)
	}
	for _, method := range methods {
		route := route{
			method:   strings.ToUpper(method),
			segments: segments,
			matchers: matchers,
			wildcard: strings.HasSuffix(pattern, "/..."),
			handler:  m.wrap(handler),
		}
		
		*m.routes = append(*m.routes, route)
	}

And in match you can just use the next matcher in the slice whenever you see a pipe.

Proposal: Optimize HTTP Router for Improved Performance and Code Efficiency

Problem:

The current implementation of the HTTP router suffers from redundancy and inefficiencies, particularly in route handling. Routes with the same path but different HTTP methods or handlers lead to duplicated route definitions, increased memory consumption, and slower lookup times.

Example:

package main

import (
	"fmt"
	"net/http"
	"test/pkg/flow"
)

func main() {
	mux := flow.New()
	mux.HandleFunc("/index", indexHandler, "GET", "POST")
	mux.HandleFunc("/index", indexPutHandler, "PUT")
	mux.HandleFunc("/index", indexDeleteHandler, "DELETE")

	fmt.Println(*mux.GetAllRoutes())
}

func indexHandler(w http.ResponseWriter, r *http.Request)       {}
func indexPutHandler(w http.ResponseWriter, r *http.Request)    {}
func indexDeleteHandler(w http.ResponseWriter, r *http.Request) {}
type Mux struct {
	NotFound         http.Handler
	MethodNotAllowed http.Handler
	Options          http.Handler
	routes           *[]route
	middlewares      []func(http.Handler) http.Handler
}

// for testing purposes
func (m *Mux) GetAllRoutes() *[]route {
	return m.routes
}

Output:

[{GET [ index] false 0x100a99160} {POST [ index] false 0x100a99160} {HEAD [ index] false 0x100a99160} {PUT [ index] false 0x100a99170} {DELETE [ index] false 0x100a99180}]

Proposed Solution:

To address these issues, I propose optimizing the HTTP router by consolidating routes with the same path into single route objects and internally mapping HTTP methods to their corresponding handlers. This approach reduces memory usage, streamlines route matching, and simplifies the codebase. By leveraging maps for method-handler associations, we achieve faster lookup times and improved runtime performance.

Implementation Details:

  1. Route Consolidation: Routes with the same path will be consolidated into single route objects.
  2. Method-Handler Mapping: Internally within each route object, HTTP methods will be mapped to their corresponding handlers using a map data structure.
  3. Route Matching Refinement: The route matching logic will be simplified to focus solely on comparing the request's path to the route's path. Once a matching route is found, the appropriate handler based on the request's HTTP method will be retrieved from the route's internal map (405 in case HTTP-Method is not supported).
  4. Code Refactoring: The codebase will be refactored to encapsulate method-handler associations within route objects, promoting better code organization and maintainability.

P.S This is my first-ever issue/proposal so feel free to correct and educate me in case I am wrong or missing something!
P.S.S @alexedwards In case this proposal seems reasonable I will be more than happy to create a PR :)

can't mount handler for fileserver

I have public folder and inside public folder I have test.html

	mux := flow.New()
	fs := http.FileServer(http.Dir("public"))
	mux.Handle("/public/", http.StripPrefix("/public/", fs))
	err := http.ListenAndServe(fmt.Sprintf(":%v", config.Get().ServerPort), mux)

http://localhost:5555/public/test.html does not serve the page

however if I change that with default mux I get my html page. I will try to debug it but I am openning issue just in case you spot the problem quickly

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.