Categorygithub.com/insionng/vodka
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

namevalue
nameJoe Smith
email[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

namevalue
nameJoe Smith
email[email protected]
avataravatar
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 or XML or form payload into Go struct based on Content-Type request header.
  • Render response as JSON or XML 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

MiddlewareDescription
BodyLimitLimit request body
LoggerLog HTTP requests
RecoverRecover from panics
GzipSend gzip HTTP response
BasicAuthHTTP basic authentication
JWTAuthJWT authentication
SecureProtection against attacks
CORSCross-Origin Resource Sharing
CSRFCross-Site Request Forgery
StaticServe static files
HTTPSRedirectRedirect HTTP requests to HTTPS
HTTPSWWWRedirectRedirect HTTP requests to WWW HTTPS
WWWRedirectRedirect non WWW requests to WWW
NonWWWRedirectRedirect WWW requests to non WWW
AddTrailingSlashAdd trailing slash to the request URI
RemoveTrailingSlashRemove trailing slash from the request URI
MethodOverrideOverride request method
Learn More

Third-party Middleware

MiddlewareDescription
vodkapermKeeping track of users, login states and permissions.
vodkapprofAdapt 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

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

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# 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.
POST
HTTP methods.
HTTP methods.
HTTP methods.

# Variables

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author