Skip to content

A lightweight structual web api framework based on gin framework and validator

License

Notifications You must be signed in to change notification settings

tanzy2018/groute

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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}

About

A lightweight structual web api framework based on gin framework and validator

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages