Giter Club home page Giter Club logo

phi's Introduction

phi

GoDoc Widget Travis Widget License Widget GoReport Widget

phi is a package which ports chi to fasthttp.

Install

go get -u github.com/fate-lovely/phi

Example

package main

import (
  "log"
  "time"

  "github.com/fate-lovely/phi"
  "github.com/valyala/fasthttp"
)

func main() {
  r := phi.NewRouter()

  reqIDMW := func(next phi.HandlerFunc) phi.HandlerFunc {
    return func(ctx *fasthttp.RequestCtx) {
      next(ctx)
      ctx.WriteString("+reqid=1")
    }
  }
  r.Use(reqIDMW)

  r.Get("/", func(ctx *fasthttp.RequestCtx) {
    ctx.WriteString("index")
  })
  r.NotFound(func(ctx *fasthttp.RequestCtx) {
    ctx.WriteString("whoops, not found")
    ctx.SetStatusCode(404)
  })
  r.MethodNotAllowed(func(ctx *fasthttp.RequestCtx) {
    ctx.WriteString("whoops, bad method")
    ctx.SetStatusCode(405)
  })

  // tasks
  r.Group(func(r phi.Router) {
    mw := func(next phi.HandlerFunc) phi.HandlerFunc {
      return func(ctx *fasthttp.RequestCtx) {
        next(ctx)
        ctx.WriteString("+task")
      }
    }
    r.Use(mw)

    r.Get("/task", func(ctx *fasthttp.RequestCtx) {
      ctx.WriteString("task")
    })
    r.Post("/task", func(ctx *fasthttp.RequestCtx) {
      ctx.WriteString("new task")
    })

    caution := func(next phi.HandlerFunc) phi.HandlerFunc {
      return func(ctx *fasthttp.RequestCtx) {
        next(ctx)
        ctx.WriteString("+caution")
      }
    }
    r.With(caution).Delete("/task", func(ctx *fasthttp.RequestCtx) {
      ctx.WriteString("delete task")
    })
  })

  // cat
  r.Route("/cat", func(r phi.Router) {
    r.NotFound(func(ctx *fasthttp.RequestCtx) {
      ctx.WriteString("no such cat")
      ctx.SetStatusCode(404)
    })
    r.Use(func(next phi.HandlerFunc) phi.HandlerFunc {
      return func(ctx *fasthttp.RequestCtx) {
        next(ctx)
        ctx.WriteString("+cat")
      }
    })
    r.Get("/", func(ctx *fasthttp.RequestCtx) {
      ctx.WriteString("cat")
    })
    r.Patch("/", func(ctx *fasthttp.RequestCtx) {
      ctx.WriteString("patch cat")
    })
  })

  // user
  userRouter := phi.NewRouter()
  userRouter.NotFound(func(ctx *fasthttp.RequestCtx) {
    ctx.WriteString("no such user")
    ctx.SetStatusCode(404)
  })
  userRouter.Use(func(next phi.HandlerFunc) phi.HandlerFunc {
    return func(ctx *fasthttp.RequestCtx) {
      next(ctx)
      ctx.WriteString("+user")
    }
  })
  userRouter.Get("/", func(ctx *fasthttp.RequestCtx) {
    ctx.WriteString("user")
  })
  userRouter.Post("/", func(ctx *fasthttp.RequestCtx) {
    ctx.WriteString("new user")
  })
  r.Mount("/user", userRouter)

  server := &fasthttp.Server{
    Handler:     r.ServeFastHTTP,
    ReadTimeout: 10 * time.Second,
  }

  log.Fatal(server.ListenAndServe(":7789"))
}

output:

Path Status Code Body
GET / 200 index+reqid=1
POST / 405 whoops, bad method+reqid=1
GET /nothing 404 whoops, not found+reqid=1
GET /task 200 task+task+reqid=1
POST /task 200 new task+task+reqid=1
DELETE /task 200 delete task+caution+task+reqid=1
GET /cat 200 cat+cat+reqid=1
PATCH /cat 200 patch cat+cat+reqid=1
GET /cat/nothing 404 no such cat+cat+reqid=1
GET /user 200 user+user+reqid=1
POST /user 200 new user+user+reqid=1
GET /user/nothing 404 no such user+user+reqid=1

License

Licensed under MIT License

phi's People

Contributors

cj1128 avatar

Watchers

James Cloos 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.