groute

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2019 License: MIT Imports: 29 Imported by: 0

README

groute

A lightweight structual web framework based on gin framework and validator

Simple example

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/tanzy2018/groute"
)

type Student struct{}

type Params struct {
	Id   int    `form:"id" binding:"required,min=10" err-required:"id is required" err-min:"id must be greater than 10 or equal to 10"`
	Name string `form:"name" binding:"required" err-required:"name is required"`
}

func (s *Student) Info() groute.Interface {
	return groute.NewInterface(
		groute.Interface{
			Param:  Params{},
			Method: "GET",
			Path:   "/info",
		},
		func(c *groute.Context) {
			params := c.Param.(*Params)
			c.GinContext.JSON(200, gin.H{
				"id":   params.Id,
				"name": params.Name,
			})
			return
		},
	)
}

func (s *Student) Score() groute.Interface {
	return groute.NewInterface(
		groute.Interface{
			Param:  Params{},
			Method: "GET",
			Path:   "/score",
		},
		func(c *groute.Context) {
			params := c.Param.(*Params)
			c.GinContext.JSON(200, gin.H{
				"id":    params.Id,
				"name":  params.Name,
				"score": 100,
			})
			return
		},
	)
}

func main() {
	engine := gin.Default()
	api := groute.NewRouter(
		groute.WithVaidatorV9("zh"),
		groute.WithRouter(engine.Group("/student")),
	)
	api.Add(&Student{})
	engine.Run()
}


Run this simple example
go run main.go
Output of simple example

request:curl http://localhost:8080/student/score\?id\=9\&name\=lin
output: {"code":402,"msg":{"id":"id must be greater than 10 or equal to 10"},"state":0}

request:curl curl http://localhost:8080/student/score\?id\=91\&name\=lin
output:{"id":91,"name":"lin","score":100}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	sync.Mutex
	// GinContext - reuse the gin Context.
	GinContext *gin.Context
	// ClientContext - context used to call the backend service.
	ClientContext context.Context
	// Param - store the requested body data.
	Param interface{}
	// ErrCode - custome http code when return the error hints.
	ErrCode interface{}
	// Extra - data read from special middleware will be set here.
	Extra map[string]interface{}
	// ErrHandle - handle error hints when validate failed.
	ErrHandle ErrHandle
}

Context request context,implementing the *gin.Context

type ErrHandle

type ErrHandle func(ctx *Context, err interface{})

ErrHandle - handle the validator error.

type ErrHandleFunc

type ErrHandleFunc func(*Context) error

ErrHandleFunc -

type ErrHandleFuncChain

type ErrHandleFuncChain []ErrHandleFunc

ErrHandleFuncChain - handle function chain.

type HandleFunc

type HandleFunc func(*Context)

HandleFunc - handle function.

type Interface

type Interface struct {
	// SyncHandleFunc - the special middleware to handle the request param in synchronous way
	// but only excutes after all the AsyncHandleFunc successing,because some
	// param may rely on result of the asynchronouse handlefuncs.
	SyncHandleFunc ErrHandleFuncChain
	// AsyncHandleFunc - the special middleware to handle the request param in asynchronous way.
	AsyncHandleFunc ErrHandleFuncChain
	// Path - starts with "/".
	Path string
	// Method - one of `POST,GET,DELETE,PUT,HEAD,PATCH`,case insensitive.
	Method string
	// Param - requrest params
	Param interface{}
	// Handle function that handles the business logic .
	Handle HandleFunc
	// ErrHandle
	ErrHandle ErrHandle
}

Interface define the router interface

func NewInterface

func NewInterface(inter Interface, handle func(c *Context)) Interface

NewInterface - create a new Interface instance.

type Option

type Option func(*Options)

Option - optional config the router.

func WithClientContext

func WithClientContext(ctx context.Context) Option

WithClientContext - set context used for call the backend services.

func WithErrHandle

func WithErrHandle(errHandle ErrHandle) Option

WithErrHandle - set error handle for validator.

func WithErrMsgTagPrefix

func WithErrMsgTagPrefix(prefix string) Option

WithErrMsgTagPrefix - set the errprefix tag which define the error hints.

func WithMiddlerware

func WithMiddlerware(middleware ...gin.HandlerFunc) Option

WithMiddlerware - set the global middleware for this router.

func WithRouter

func WithRouter(router gin.IRouter) Option

WithRouter - set the route.

func WithVaidatorV9

func WithVaidatorV9(locale string) Option

WithVaidatorV9 - set validator v9 supported locale:en,fr,id,ja,nl,pt_BR,tr,zh,zh_tw;default en

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options - option config to initailize the router.

type Router

type Router struct {
	*Options
}

Router global router manager

func NewRouter

func NewRouter(options ...Option) Router

NewRouter create a new router

func (*Router) Add

func (r *Router) Add(in interface{})

Add add route

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL