Coder Social home page Coder Social logo

router's Introduction

Simple router

This is a simple web router for golang supporting lazy (optional) parameters You may check for samples inside the router_test.go

Quick Guide

Domain Filter

Domain filter is used to filter domains. it can parse wildcards or even regex.

    handler := router.GetInstance()
    
	handler.Domain("*.test.com,test.com,domain2.com", func(req router.Request) {
		//Any thing you want to do if domain matches
		fmt.Println(req.Req())
	}, func(handle *router.Route) {
		//Sub routers goes here
		handle.Match("index.html","GET", func(req router.Request){
			fmt.Println("Matched")
		})
	})
	

Group And Match Filters

Group filter is used to filter first part of url. Group filter router does not care about anything but the first part of url and ignore the rest.

Match filter is used to match whole url. It matches whole url as same as filter. match can be combined with group filter for easier routing decision and cleaner code.

    handler := router.GetInstance()
    
	handler.Group("articles", func(req router.Request) {
	    //You will reach to here if domain.tld/articles/* matched 
		//If you dont need this just pass nil!
	}, func(handle *router.Route) {
		//Sub routers goes here
		handle.Match("article/[i:id]","GET", func(req router.Request){
			fmt.Println("Matched article id"+req.Parameters["id"])
		})
	})
	

Parameters

You might want to read url parameters it can simply done by few simple switches

	//a => alpha num
	//i or id => integer
	//s => string
	handle.Match("article/[i:id]/[a:title]/[s:str]","GET", func(req router.Request){
			fmt.Println("Matched article id:"+req.Parameters["id"])
			fmt.Println("Matched article title only alpha num :"+req.Parameters["title"])
			fmt.Println("Matched some strings:"+req.Parameters["str"])
	})
		

Lazy parameter

Lazy or optional parameter is kind of parameters that you might want to pass it to url or simply ignore it. the router will match and extract the parameter if exist but still will match if the corresponding lazy parameter is not exist.

The syntax is same as common parameters with and additional ~ appended to first part of parameter. the syntax for url of lazy parameters should be like domain.tld/lazy:123

    //Following urls should match the router
    //domain.tld/article/1/optional_id:18/title:xyz123
    //domain.tld/article/1/optional_id:18/
    //domain.tld/article/1
   	handle.Match("article/[i:id]/[i:optional_id]/~[a:title]/","GET", func(req router.Request){
   			fmt.Println(req.Parameters)
   	})

MiddleWares

You may use middlewares to intervene in url matching procedure. middlewares can override matching by return true or false.

        handle.Match("article/[i:id]","GET", func(req router.Request){
			fmt.Println("Matched article id"+req.Parameters["id"])
		}).Middleware(func(req router.Request) bool {
          				if req.Req().Host == "test.com"{
          					return false
          				}
          
          				return true
          			})
		

Static files

You may want to serve static files as assets or simple html files and etc. you may simply create a customized router for this reason. Notice: Static file router support resume

	handler.Group("static",nil, func(handle *router.Route) {
		handle.Static("subdir","./assets/",nil)
	})
	//or
	handler.Static("subdir","./assets/", func(req router.Request) {
    	fmt.Println("User request for static file:"+req.Req().RequestURI)
    })

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.