Giter VIP home page Giter VIP logo

restgo's Introduction

Router for api server of golang, on the top of fasthttp, inspired by expressjs, use it like expressjs.

GoDoc

Install

go get github.com/restgo/restgo

Url Pattern

/users
/users/:id
/categories/:category_id/posts/:id

URL Params will be encoded in querystring, you can get values from querystring or use ctx.ParamXXX methods to get it.

for url: /users/12345?limit=10&page=2&sort=

ctx.ParamString("id", "")   // get "12345" 
ctx.ParamInt("limit", 15)   // get 15
ctx.ParamInt("page", 1)     // get 2
ctx.ParamString("sort", "id")  // get "id", default value

Middleware

It's easy to develop a middleware for it

Such as log middleware

app.Use(func (ctx *Context, next restgo.Next) {
    fmt.Println("This is log middleware, I will log everything!")
    next(nil)
})

Session

Please check restgo/session

Use with Controller

type UserController struct {}

// implement ControllerRouter Interface, then you can set route for this controller
func (this *UserController)Route(router *restgo.Router) {
    router.GET("/", this.Get) // GET /users/
}

func (this *UserController) Get(ctx *Context, next restgo.Next) {
    ctx.ServeText(200, "GET User")
}

// Add it to router
app.Use("/users", &UserController{});

//now, you can access it `GET /users/`, SIMPLE!!! 

Demo

check example exmaple/app.go

app := restgo.App()

// filter all request
app.Use(func(ctx *Context, next restgo.Next) {
    fmt.Println("Filter all")
    next(nil)
})

// load controller route
app.Use("/users", &UserController{})

// all /test requests(GET, DELETE, PUT...) go into this handler
app.All("/test", func(ctx *Context, next restgo.Next) {
    fmt.Println("All test: " + string(ctx.Method()))
    ctx.ServeText(200, "All test: "+string(ctx.Method()))
})

// set /about path handler
app.GET("/about", func(ctx *Context, next restgo.Next) {
    fmt.Println("GET about")
    ctx.ServeText(200, "GET about")
})

// default :8080, you can specify it too. app.Run(":8080")
app.Run()

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.