Git Product home page Git Product logo

go-router's Introduction

go-router

GoDoc CircleCI Go Report Card

Provide routing for HTTP request. It implemented http.handler interface, thereby easily introducing. Support URL path parameters and can be mapped to argument of handler method.

Installation

go get -u github.com/takashabe/go-router

Usage

Basic usage:

package main

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

  "github.com/takashabe/go-router"
)

func main() {
  r := router.NewRouter()
  r.Get("/", func(w http.ResponseWriter, req *http.Request) {
    fmt.Println("called get '/'")
  })
  // Path "/" can be registered each HTTP methods
  r.Post("/", func(w http.ResponseWriter, req *http.Request) {
    fmt.Println("called post '/'")
  })
  // ex. receive query "/10":
  // Mapped to id=10, and "10" already checked valid int type
  r.Get("/:id", func(w http.ResponseWriter, req *http.Request, id int) {
    fmt.Printf("called get '/:id' with %d\n", id)
  })

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

For static files:

func Routes() *router.Router {
  r := router.NewRouter()
  r.Get("/", getIndex)

  // Query "css/*" mapped to "./static/css/*"
  r.ServeDir("/css/*filepath", http.Dir("static/css"))

  return r
}

For SPA application:

func Routes() *router.Router {
  r := router.NewRouter()

  // Routing of the backend API
  r.Get("/api/login", ...)
  r.Post("/api/login", ...)

  // Routing of the frontend
  r.ServeFile("/", "./public/index.html")
  r.ServeFile("/bundle.js", "./public/bundle.js")
  r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    http.ServeFile(w, req, "./public/index.html")
  })

  return r
}

For customizable validation parameters:

package main

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

  "github.com/takashabe/go-router"
)

// Allow3DigitNumber expect allow 3 digit number
// Require implements router.ValidateParam
type Allow3DigitNumber struct {
  raw string
}

// Validate is validate URL path parameters
func (v *Allow3DigitNumber) Validate(raw string) bool {
  v.raw = raw
  return regexp.MustCompile(`\A[0-9]{3}\z`).MatchString(raw)
}

func (v *Allow3DigitNumber) convert() (int, error) {
  return strconv.Atoi(v.raw)
}

func main() {
  r := router.NewRouter()
  r.Get("/:id", func(w http.ResponseWriter, req *http.Request, v *Allow3DigitNumber) {
    id, _ := v.convert()
    fmt.Printf("called get '/:id' with %d\n", id)
  })

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

go-router's People

Contributors

takashabe avatar

Watchers

James Cloos avatar  avatar

go-router's Issues

CI

used ci-as-a-service

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.