All Projects → lonng → Nex

lonng / Nex

Aiming to simplify the construction of JSON API service

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Nex

Mortar
Mortar is a GO framework/library for building gRPC (and REST) web services.
Stars: ✭ 492 (+1305.71%)
Mutual labels:  microservice, middleware, dependency-injection
Serve Handler
The foundation of `serve`
Stars: ✭ 349 (+897.14%)
Mutual labels:  middleware, handler
Go Project Sample
Introduce the best practice experience of Go project with a complete project example.通过一个完整的项目示例介绍Go语言项目的最佳实践经验.
Stars: ✭ 344 (+882.86%)
Mutual labels:  microservice, dependency-injection
Krakend
Ultra performant API Gateway with middlewares. A project hosted at The Linux Foundation
Stars: ✭ 4,752 (+13477.14%)
Mutual labels:  microservice, middleware
EvaEngine.js
A micro service development engine for node.js
Stars: ✭ 31 (-11.43%)
Mutual labels:  microservice, dependency-injection
Hunt Framework
A Web framework for D Programming Language. Full-stack high-performance.
Stars: ✭ 256 (+631.43%)
Mutual labels:  microservice, webservice
Hyperf
🚀 A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.
Stars: ✭ 4,206 (+11917.14%)
Mutual labels:  microservice, dependency-injection
Malagu
Malagu is a Serverless First, component-based, platform-independent, progressive application framework based on TypeScript.
Stars: ✭ 184 (+425.71%)
Mutual labels:  microservice, dependency-injection
Restheart
RESTHeart - The REST API for MongoDB
Stars: ✭ 659 (+1782.86%)
Mutual labels:  microservice, webservice
Gongular
A different approach to Go web frameworks
Stars: ✭ 438 (+1151.43%)
Mutual labels:  middleware, dependency-injection
SocketHook
Socket hook is an injector based on EasyHook (win only) which redirect the traffic to your local server.
Stars: ✭ 38 (+8.57%)
Mutual labels:  microservice, dependency-injection
Just Api
💥 Test REST, GraphQL APIs
Stars: ✭ 768 (+2094.29%)
Mutual labels:  microservice, webservice
AspNetCore.Weixin
An ASP.NET Core middleware for Wechat/Weixin message handling and apis. (微信公众平台/接口调用服务)
Stars: ✭ 24 (-31.43%)
Mutual labels:  middleware, handler
Msngr.js
An asynchronous messaging library, written in JavaScript, for node and the web browser
Stars: ✭ 337 (+862.86%)
Mutual labels:  middleware, handler
granitic
Web/micro-services and IoC framework for Golang developers
Stars: ✭ 32 (-8.57%)
Mutual labels:  microservice, dependency-injection
Rocky
Full-featured, middleware-oriented, programmatic HTTP and WebSocket proxy for node.js
Stars: ✭ 357 (+920%)
Mutual labels:  microservice, middleware
Cloud Book
《Spring Cloud 微服务架构进阶》各章节附录源码
Stars: ✭ 142 (+305.71%)
Mutual labels:  microservice, middleware
Activej
ActiveJ is an alternative Java platform built from the ground up. ActiveJ redefines web, high load, and cloud programming in Java, featuring ultimate performance and scalability!
Stars: ✭ 183 (+422.86%)
Mutual labels:  microservice, dependency-injection
Echo
High performance, minimalist Go web framework
Stars: ✭ 21,297 (+60748.57%)
Mutual labels:  microservice, middleware
Zend Expressive
PSR-15 middleware in minutes!
Stars: ✭ 740 (+2014.29%)
Mutual labels:  microservice, middleware

nex

This library aims to simplify the construction of JSON API service, nex.Handler is able to wrap any function to adapt the interface of http.Handler, which unmarshals POST data to a struct automatically.

Demo

中文示例

Benchmark

BenchmarkSimplePlainAdapter_Invoke-8             5000000               318 ns/op              80 B/op          2 allocs/op
BenchmarkSimpleUnaryAdapter_Invoke-8             1000000              1999 ns/op            1296 B/op         16 allocs/op
BenchmarkGenericAdapter_Invoke-8                  500000              2404 ns/op            1456 B/op         17 allocs/op
BenchmarkSimplePlainAdapter_Invoke2-8            1000000              1724 ns/op            1096 B/op         11 allocs/op
BenchmarkSimpleUnaryAdapter_Invoke2-8             500000              3484 ns/op            2313 B/op         25 allocs/op
BenchmarkGenericAdapter_Invoke2-8                 500000              3597 ns/op            2473 B/op         26 allocs/op

Support types

io.ReadCloser      // request.Body
http.Header        // request.Header
nex.Form           // request.Form
nex.PostForm       // request.PostForm
*nex.Form          // request.Form
*nex.PostForm      // request.PostForm
*url.URL           // request.URL
*multipart.Form    // request.MultipartForm
*http.Request      // raw request

Usage

http.Handle("/test", nex.Handler(test))

func test(io.ReadCloser, http.Header, nex.Form, nex.PostForm, *CustomizedRequestType, *url.URL, *multipart.Form) (*CustomizedResponseType, error)

Example

package main

import (
	"errors"
	"fmt"
	"io"
	"net/http"
	"net/url"

	"github.com/lonnng/nex"
)

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

type LoginResponse struct {
	Result string `json:"result"`
}

type ErrorMessage struct {
	Code  int    `json:"code"`
	Error string `json:"error"`
}

func main() {
	// customize error encoder
	nex.SetErrorEncoder(func(err error) interface{} {
		return &ErrorMessage{Code: -1, Error: err.Error()}
	})

	// global middleware
	nex.Before(before1, before2)
	nex.After(after1, after2)

	mux := http.NewServeMux()
	mux.Handle("/test1", nex.Handler(test1))
	mux.Handle("/test2", nex.Handler(test2))
	mux.Handle("/test3", nex.Handler(test3))
	mux.Handle("/test4", nex.Handler(test4))
	mux.Handle("/test5", nex.Handler(test5))
	mux.Handle("/test6", nex.Handler(test6))
	mux.Handle("/test7", nex.Handler(test7))
	mux.Handle("/test8", nex.Handler(test8))
	// add middleware
	mux.Handle("/test9", nex.Handler(test8).Before(before1, before2).After(after1, after2))

	logic := func(ctx context.Context) (context.Context, *testResponse, error) {
		println(ctx.Value("key").(string))
		println(ctx.Value("key2").(string))
		return context.WithValue(ctx, "logic", "logic-value"), &testResponse{}, nil
	}

	before1 := func(ctx context.Context, request *http.Request) (context.Context, error) {
		return context.WithValue(ctx, "key", "value"), nil
	}

	before2 := func(ctx context.Context, request *http.Request) (context.Context, error) {
		println(ctx.Value("key").(string))
		return context.WithValue(ctx, "key2", "value2"), nil
	}

	after1 := func(ctx context.Context, w http.ResponseWriter) (context.Context, error) {
		println(ctx.Value("key").(string))
		println(ctx.Value("key2").(string))
		println(ctx.Value("logic").(string))

		return context.WithValue(ctx, "after1", "after1-value"), nil
	}

	after2 := func(ctx context.Context, w http.ResponseWriter) (context.Context, error) {
		println(ctx.Value("key").(string))
		println(ctx.Value("key2").(string))
		println(ctx.Value("logic").(string))
		println(ctx.Value("after1").(string))

		return context.WithValue(ctx, "key", "value"), nil
	}

	handler := Handler(logic).Before(before1, before2).After(after1, after2)

	mux.Handle("/context", handler)

	http.ListenAndServe(":8080", mux)
}

// POST: regular response
func test1(m *LoginRequest) (*LoginResponse, error) {
	fmt.Printf("%+v\n", m)
	return &LoginResponse{Result: "success"}, nil
}

// POST: error response
func test2(m *LoginRequest) (*LoginResponse, error) {
	fmt.Printf("%+v\n", m)
	return nil, errors.New("error test")
}

// GET: regular response
func test3() (*LoginResponse, error) {
	return &LoginResponse{Result: "success"}, nil
}

// GET: error response
func test4() (*LoginResponse, error) {
	return nil, errors.New("error test")
}

func test5(header http.Header) (*LoginResponse, error) {
	fmt.Printf("%#v\n", header)
	return &LoginResponse{Result: "success"}, nil
}

func test6(form nex.Form) (*LoginResponse, error) {
	fmt.Printf("%#v\n", form)
	// use form helper method
	// start := query.IntOrDefault("start", 0)
	// count := query.IntOrDefault("count", -1)
	return &LoginResponse{Result: "success"}, nil
}

func test7(header http.Header, form nex.Form, body io.ReadCloser) (*LoginResponse, error) {
	fmt.Printf("%#v\n", header)
	fmt.Printf("%#v\n", form)
	return &LoginResponse{Result: "success"}, nil
}

func test8(header http.Header, r *LoginResponse, url *url.URL) (*LoginResponse, error) {
	fmt.Printf("%#v\n", header)
	fmt.Printf("%#v\n", r)
	fmt.Printf("%#v\n", url)
	return &LoginResponse{Result: "success"}, nil
}
curl -XPOST -d '{"username":"test", "password":"test"}' http://localhost:8080/test1
curl -XPOST -d '{"username":"test", "password":"test"}' http://localhost:8080/test2
curl  http://localhost:8080/test3
curl  http://localhost:8080/test4
curl  http://localhost:8080/test5
curl  http://localhost:8080/test6\?test\=test
curl  http://localhost:8080/test7\?test\=tset
curl -XPOST -d '{"username":"test", "password":"test"}' http://localhost:8080/test8\?test\=test

License

Copyright (c) <2016> [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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].