Code Monkey home page Code Monkey logo

go-router-simple's Introduction

go-router-simple

test PkgGoDev Go Report Card

go-router-simple is a simple HTTP request router for Go. This package is ported from Router::Simple which is a great module in Perl.

Motivation: Most request routing third-party modules in Go are implemented using the trie (radix) tree algorithm. Hence, they are fast but it hard to provide flexible URL parameters. (e.g. URL paths containing /)

โš ๏ธ Supports Go1.12 and above.

Synopsis

package main

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

	"github.com/Code-Hex/go-router-simple"
)

func main() {
	r := router.New()

	r.GET("/", http.HandlerFunc(Index))
	r.GET("/hi/:name", http.HandlerFunc(Hi))
	r.GET("/hi/{name}", http.HandlerFunc(Hi))
	r.GET("/download/*.*", http.HandlerFunc(GetFilename))
	r.GET(`/blog/{year:\d{4}}/{month:(?:\d{2})}`, Blog())

	log.Fatal(http.ListenAndServe(":8080", r))
}

func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Index!\n")
}

func Hi(w http.ResponseWriter, r *http.Request) {
	name := router.ParamFromContext(r.Context(), "name")
	fmt.Fprintf(w, "Welcome! %q\n", name)
}

func GetFilename(w http.ResponseWriter, r *http.Request) {
	wildcards := router.WildcardsFromContext(r.Context())
	fmt.Fprintf(w, "File: %q\n", strings.Join(wildcards, "."))
}

func Blog() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		year := router.ParamFromContext(r.Context(), "year")
		month := router.ParamFromContext(r.Context(), "month")
		fmt.Fprintf(w, "Render: %q\n", year+"/"+month)
	})
}

Advanced usage

go-router-simple is used regular expressions for traversal but it's a little slow. So you would better to use other third-party routing packages for traversing most request paths whenever possible.

Here's example. Basically, use httprouter, but use go-router-simple for complex URL parameters.

package main

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

	"github.com/Code-Hex/go-router-simple"
	"github.com/julienschmidt/httprouter"
)

func main() {
    // Setup go-router-simple.
	r := router.New()
	r.GET(`/blog/{year:(?:199\d|20\d{2})}/{month:(?:0?[1-9]|1[0-2])}`, Blog())

	// Setup httprouter.
	hr := httprouter.New()
	hr.GET("/hi/:name", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		name := ps.ByName("name")
		fmt.Fprintf(w, "Welcome! %q\n", name)
	})
	hr.GET("/bye/:name", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		name := ps.ByName("name")
		fmt.Fprintf(w, "Bye! %q\n", name)
	})

	// If not found on httprouter, it will traverse on go-router-simple.
	hr.NotFound = r

	log.Fatal(http.ListenAndServe(":8080", hr))
}

func Blog() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		year := router.ParamFromContext(r.Context(), "year")
		month := router.ParamFromContext(r.Context(), "month")
		fmt.Fprintf(w, "Render: %q\n", year+"/"+month)
	})
}
Result
$ curl localhost:8080/hi/codehex
Welcome! "codehex"
$ curl localhost:8080/bye/codehex
Bye! "codehex"
$ curl localhost:8080/blog/2020/11
Render: "2020/11"

go-router-simple's People

Watchers

 avatar  avatar  avatar

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.