All Projects → gopub → wine

gopub / wine

Licence: other
A lightweight and flexible framework to help build elegant web API

Programming Languages

go
31211 projects - #10 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to wine

HttpServerLite
TCP-based simple HTTP and HTTPS server, written in C#.
Stars: ✭ 44 (+12.82%)
Mutual labels:  webserver, http-server, restful-api
Elli
Simple, robust and performant Erlang web server
Stars: ✭ 194 (+397.44%)
Mutual labels:  webserver, api-server, http-server
Pure Http
✨ The simple web framework for Node.js with zero dependencies.
Stars: ✭ 139 (+256.41%)
Mutual labels:  webserver, http-server
Rayo.js
Micro framework for Node.js
Stars: ✭ 170 (+335.9%)
Mutual labels:  webserver, http-server
Tinywebserver
🔥 Linux下C++轻量级Web服务器
Stars: ✭ 4,720 (+12002.56%)
Mutual labels:  webserver, http-server
cpp-rest-api
RESTFul Web service by C++, implemented basic REST endpoints and RESTVerbs (GET,POST,PUT,DELETE).
Stars: ✭ 13 (-66.67%)
Mutual labels:  http-server, restful-api
Proxy.py
⚡⚡⚡Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on Network monitoring, controls & Application development, testing, debugging
Stars: ✭ 1,291 (+3210.26%)
Mutual labels:  webserver, http-server
Fiery
A flexible and lightweight web server
Stars: ✭ 203 (+420.51%)
Mutual labels:  webserver, http-server
Nico
A HTTP2 web server for reverse proxy and single page application, automatically apply for ssl certificate, Zero-Configuration.
Stars: ✭ 43 (+10.26%)
Mutual labels:  webserver, http-server
EthernetWebServer SSL
Simple TLS/SSL Ethernet WebServer, HTTP Client and WebSocket Client library for for AVR, Portenta_H7, Teensy, SAM DUE, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, nRF52 and RASPBERRY_PI_PICO boards using Ethernet shields W5100, W5200, W5500, ENC28J60 or Teensy 4.1 NativeEthernet/QNEthernet. It now supports Ethernet TLS/SSL Client. The library supports …
Stars: ✭ 40 (+2.56%)
Mutual labels:  webserver, http-server
Router.cr
Minimum High Performance Middleware for Crystal Web Server.
Stars: ✭ 231 (+492.31%)
Mutual labels:  webserver, http-server
Crow
A Fast and Easy to use microframework for the web.
Stars: ✭ 1,718 (+4305.13%)
Mutual labels:  webserver, http-server
Igropyr
a async http server base on libuv for Chez Scheme
Stars: ✭ 85 (+117.95%)
Mutual labels:  webserver, http-server
Suave
Suave is a simple web development F# library providing a lightweight web server and a set of combinators to manipulate route flow and task composition.
Stars: ✭ 1,196 (+2966.67%)
Mutual labels:  webserver, http-server
Octane
A web server modeled after express in Rust.
Stars: ✭ 136 (+248.72%)
Mutual labels:  webserver, http-server
Jennet
A simple HTTP web framework written in Pony
Stars: ✭ 72 (+84.62%)
Mutual labels:  webserver, http-server
go-pangu
rest api web server based on go(High availability, high security, high performance)
Stars: ✭ 45 (+15.38%)
Mutual labels:  api-server, restful-api
Webcpp
用C++开发web服务器框架
Stars: ✭ 23 (-41.03%)
Mutual labels:  webserver, http-server
Embedio
A tiny, cross-platform, module based web server for .NET
Stars: ✭ 1,007 (+2482.05%)
Mutual labels:  webserver, http-server
Criollo
A powerful Cocoa web framework and HTTP server for macOS, iOS and tvOS.
Stars: ✭ 229 (+487.18%)
Mutual labels:  webserver, http-server

WINE

Wine is a lightweight web framework for quickly writing web applications/services in Go.

Install

    $ go get -u -v github.com/gopub/wine

Quick start

Create ./hello.go

    package main
    
    import "github.com/gopub/wine"
    
    func main() {
    	s := wine.NewServer(nil)
    	s.Get("/hello", func(ctx context.Context, req *wine.Request) wine.Responder {
    		return wine.Text("Hello, Wine!")
    	})
    	s.Run(":8000")
    }

Run and test:

    $ go run hello.go
    $ curl http://localhost:8000/hello
    $ Hello, Wine!

JSON Rendering

    s := wine.NewServer(nil)
    s.Get("/time", func(ctx context.Context, req *wine.Request) wine.Responder {
    	return wine.JSON(http.StatusOK, map[string]interface{}{"time":time.Now().Unix()})
    })
    s.Run(":8000")

Parameters

Request.Params() returns all parameters from URL query, post form, cookies, and custom header fields.

    s := wine.NewServer(nil)
    s.Post("feedback", func(ctx context.Context, req *wine.Request) wine.Responder {
        text := req.Params().String("text")
        email := req.Params().String("email")
        return wine.Text("Feedback:" + text + " from " + email)
    })
    s.Run(":8000")

Support parameters in query string

    $ curl -X POST "http://localhost:8000/feedback?text=crash&[email protected]"

Support parameters in form

    $ curl -X POST -d "text=crash&[email protected]" http://localhost:8000/feedback

Support parameters in json

    $ curl -X POST -H "Content-Type:application/json" 
           -d '{"text":"crash", "email":"[email protected]"}' 
           http://localhost:8000/feedback

Parameters in URL Path

Path parameters are also supported in order to provide elegant RESTFul API.
Single parameter in one segment:

    s := wine.NewServer(nil) 
    s.Get("/items/{id}", func(ctx context.Context, req *wine.Request) wine.Responder {
        id := req.Params().String("id")
        return wine.Text("item id: " + id)
    }) 
    s.Run(":8000")

Model Binding

If an endpoint is bound with a model, request's parameters will be unmarshalled into an instance of the same model type.
If the model implements interface wine.Validator, then it will be validated before passed to handler.

type Item struct {
    ID      string      `json:"id"`
    Price   float32     `json:"price"`
}
func (i *Item) Validate() error {
    if i.ID == "" {
        return errors.BadRequest("missing id")
    }
    if i.Price <= 0 {
        return errors.BadRequest("missing or invalid price")
    }
    return nil 
}

func main() {
    s := wine.NewServer(nil) 
    s.Post("/items", CreateItem).SetModel(&Item{}) 
    s.Run(":8000")
}
func CreateItem(ctx context.Context, req *wine.Request) wine.Responder {
    // It's safe to get *Item, and item has been validated
    item := req.Model.(*Item)
    // TODO: save item into database
    return item
}

Use Interceptor

Intercept and preprocess requests

    func Log(ctx context.Context, req *wine.Request) wine.Responder {
    	st := time.Now()  
    	//pass request to the next handler
    	result := wine.Next(ctx, request)
    	cost := float32((time.Since(st) / time.Microsecond)) / 1000.0
    	req := request.Request()
    	log.Printf("%.3fms %s %s", cost, req.Method, req.RequestURI)
    	return result
    } 
func main() { s := wine.NewServer(nil) //Log every request r := s.Use(Log) r.Get("/hello", func(ctx context.Context, req *wine.Request) wine.Responder { return wine.Text("Hello, Wine!") }) s.Run(":8000") }

Grouping Route

  
    func CheckSessionID(ctx context.Context, req *wine.Request) wine.Responder {
    	sid := req.Params().String("sid")
    	//check sid
    	if len(sid) == 0 {
    		return errors.BadRequest("missing sid")
    	} 
    	return wine.Next(ctx, request)
    }
    
    func GetUserProfile(ctx context.Context, req *wine.Request) wine.Responder  {
    	//...
    }
    
    func GetUserFriends(ctx context.Context, req *wine.Request) wine.Responder  {
    	//...
    }
    
    func GetServerTime(ctx context.Context, req *wine.Request) wine.Responder  {
    	//...
    }
    
    func main() {
    	s := wine.NewServer(nil)
    
    	//Create "accounts" group and add interceptor CheckSessionID
    	g := s.Group("accounts").Use(CheckSessionID)
    	g.Get("{user_id}/profile", GetUserProfile)
    	g.Get("{user_id}/friends/{page}/{size}", GetUserFriends)
    
    	s.Get("time", GetServerTime)
    
    	s.Run(":8000")
    }  

Run it:

Running at :8000 ...
GET   /time/ main.GetServerTime
GET   /accounts/{user_id}/friends/{page}/{size}    main.CheckSessionID, main.GetUserFriends
GET   /accounts/{user_id}/profile/    main.CheckSessionID, main.GetUserProfile

Auth

It's easy to turn on basic auth.

s := wine.NewServer(nil)
s.Use(wine.NewBasicAuthHandler(map[string]string{
	"admin": "123",
	"tom":   "456",
}, ""))
s.StaticDir("/", "./html")
s.Run(":8000")

Recommendations

Wine designed for modular web applications/services is not a general purpose web server. It should be used behind a web server such as Nginx, Caddy which provide compression, security features.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].