modulepackage
2.0.0+incompatible
Repository: https://github.com/insionng/vodka.git
Documentation: pkg.go.dev
# README
Vodka
由于Echo V3不再支持fasthttp, 于是我将以Vodka的名义自行维护Echo V2的后续开发,如果你也在使用我的这个版本欢迎留言交流.
Fast and unfancy HTTP server framework for Go (Golang). Up to 10x faster than the rest.
Feature Overview
- Optimized HTTP router which smartly prioritize routes
- Build robust and scalable RESTful APIs
- Run with standard HTTP server or FastHTTP server
- Group APIs
- Extensible middleware framework
- Define middleware at root, group or route level
- Data binding for JSON, XML and form payload
- Handy functions to send variety of HTTP responses
- Centralized HTTP error handling
- Template rendering with any template engine
- Define your format for the logger
- Highly customizable
Performance
- Environment:
- Go 1.7.1
- wrk 4.0.0
- 16 GB, 8 Core
快速开始
安装
在安装之前确认你已经安装了Go语言. Go语言安装请访问 [install instructions](http://golang.org/doc/install.html).
Vodka is developed and tested using Go 1.7.x
+
$ go get -u github.com/insionng/vodka
Hello, World!
Create server.go
package main
import (
"net/http"
"github.com/insionng/vodka"
"github.com/insionng/vodka/engine/fasthttp"
)
func main() {
v := vodka.New()
v.GET("/", func(self vodka.Context) error {
return self.String(http.StatusOK, "Hello, World!")
})
v.Run(fasthttp.New(":1987"))
}
Start server
$ go run server.go
Browse to http://localhost:1987 and you should see Hello, World! on the page.
Routing
v.POST("/users", saveUser)
v.GET("/users/:id", getUser)
v.PUT("/users/:id", updateUser)
v.DELETE("/users/:id", deleteUser)
Path Parameters
func getUser(self vodka.Context) error {
// User ID from path `users/:id`
id := self.Param("id")
}
Query Parameters
/show?team=x-men&member=wolverine
func show(c vodka.Context) error {
// Get team and member from the query string
team := c.QueryParam("team")
member := c.QueryParam("member")
}
Form application/x-www-form-urlencoded
POST
/save
name | value |
---|---|
name | Joe Smith |
[email protected] |
func save(c vodka.Context) error {
// Get name and email
name := c.FormValue("name")
email := c.FormValue("email")
}
Form multipart/form-data
POST
/save
name | value |
---|---|
name | Joe Smith |
[email protected] | |
avatar | avatar |
func save(c vodka.Context) error {
// Get name and email
name := c.FormValue("name")
email := c.FormValue("email")
// Get avatar
avatar, err := c.FormFile("avatar")
if err != nil {
return err
}
// Source
src, err := avatar.Open()
if err != nil {
return err
}
defer src.Close()
// Destination
dst, err := os.Create(avatar.Filename)
if err != nil {
return err
}
defer dst.Close()
// Copy
if _, err = io.Copy(dst, src); err != nil {
return err
}
return c.HTML(http.StatusOK, "<b>Thank you!</b>")
}
Handling Request
- Bind
JSON
orXML
orform
payload into Go struct based onContent-Type
request header. - Render response as
JSON
orXML
with status code.
type User struct {
Name string `json:"name" xml:"name" form:"name"`
Email string `json:"email" xml:"email" form:"email"`
}
e.POST("/users", func(c vodka.Context) error {
u := new(User)
if err := c.Bind(u); err != nil {
return err
}
return c.JSON(http.StatusCreated, u)
// or
// return c.XML(http.StatusCreated, u)
})
Static Content
Server any file from static directory for path /static/*
.
e.Static("/static", "static")
Learn More
Template Rendering
Middleware
// Root level middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Group level middleware
g := e.Group("/admin")
g.Use(middleware.BasicAuth(func(username, password string) bool {
if username == "joe" && password == "secret" {
return true
}
return false
}))
// Route level middleware
track := func(next vodka.HandlerFunc) vodka.HandlerFunc {
return func(c vodka.Context) error {
println("request to /users")
return next(c)
}
}
e.GET("/users", func(c vodka.Context) error {
return c.String(http.StatusOK, "/users")
}, track)
Built-in Middleware
Middleware | Description |
---|---|
BodyLimit | Limit request body |
Logger | Log HTTP requests |
Recover | Recover from panics |
Gzip | Send gzip HTTP response |
BasicAuth | HTTP basic authentication |
JWTAuth | JWT authentication |
Secure | Protection against attacks |
CORS | Cross-Origin Resource Sharing |
CSRF | Cross-Site Request Forgery |
Static | Serve static files |
HTTPSRedirect | Redirect HTTP requests to HTTPS |
HTTPSWWWRedirect | Redirect HTTP requests to WWW HTTPS |
WWWRedirect | Redirect non WWW requests to WWW |
NonWWWRedirect | Redirect WWW requests to non WWW |
AddTrailingSlash | Add trailing slash to the request URI |
RemoveTrailingSlash | Remove trailing slash from the request URI |
MethodOverride | Override request method |
Learn More
Third-party Middleware
Middleware | Description |
---|---|
vodkaperm | Keeping track of users, login states and permissions. |
vodkapprof | Adapt net/http/pprof to labstack/vodka. |
Need help?
- [QQ Group] Vodka/Vodka Web 框架群号 242851426
- Open an issue
Support Us
- :star: the project
- :earth_americas: spread the word
- Contribute to the project
Contribute
Use issues for everything
- Report issues
- Discuss on chat before sending a pull request
- Suggest new features or enhancements
- Improve/fix documentation
Vodka System
Community created packages for Vodka
- [hello world](https://github.com/vodka-contrib/helloworld)
Vodka Case
- [ZenPress](https://github.com/insionng/zenpress) - Cms/Blog System(just start-up)
Credits
- Vishal Rana - Echo Author
- Nitin Rana - Consultant
- Contributors
- Insion Ng - Vodka Author
License
MIT License
QQ Group
Vodka/Vodka Web 框架群号 242851426
Golang编程(Go/Web/Nosql)群号 245386165
Go语言编程(Golang/Web/Orm)群号 231956113
Xorm & Golang群号 280360085
Golang & Tango & Web群号 369240307
Martini&Macaron 交流群 371440803
# Functions
ContentTypeByExtension returns the MIME type associated with the file based on its extension.
New creates an instance of Vodka.
NewHTTPError creates a new HTTPError instance.
NewRouter returns a new Router instance.
WrapMiddleware wrap `vodka.HandlerFunc` into `vodka.MiddlewareFunc`.
# Constants
HTTP methods.
HTTP methods.
HTTP methods.
HTTP methods.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Security.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
Headers.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
MIME types.
HTTP methods.
HTTP methods.
HTTP methods.
HTTP methods.
HTTP methods.
# Variables
Errors.
Errors.
Errors.
Errors.
Errors.
Errors.
Errors.
Errors.
Error handlers.
Error handlers.
# Type aliases
No description provided by the author
No description provided by the author
No description provided by the author