ginserver

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// desc sort order
	Desc = "desc"
	// asc sort order
	Asc = "asc"
)
View Source
const (
	CREATE = "create"
	DELETE = "delete"
	UPDATE = "update"
	PATCH  = "patch"
	GET    = "get"
	LIST   = "list"
)
View Source
const (
	CORSAllowHeaders = "*"
)

Variables

This section is empty.

Functions

func HookExit

func HookExit(ctx context.Context)

HookExit waits for the parent process to exit It is used to wait for the parent process to exit

func InitGinServer

func InitGinServer(cfg *config.BaseConfig) (*gin.Engine, *log.Logger)

InitGinServer initializes a new gin server with the given configuration

func Run

func Run(r *gin.Engine, cfg *config.ServerConfig, gotCtx bool) context.Context

Run starts the gin server with the given configuration If gotCtx is true, it will return the context with a cancel function, you need to use HookExit to wait for the parent process to exit, HookExit(Run(r, cfg, true)), or you can use Run(r, cfg, false) to block the main process

Types

type DefaultServer

type DefaultServer struct{}

DefaultServer is the default interface for restful api. You can use it to composite your own interface.

func (*DefaultServer) Create

func (d *DefaultServer) Create() (gin.HandlerFunc, error)

Create is the method for router.POST

func (*DefaultServer) Delete

func (d *DefaultServer) Delete() (gin.HandlerFunc, error)

Delete is the method for router.DELETE

func (*DefaultServer) Get

func (d *DefaultServer) Get() (gin.HandlerFunc, error)

Get is the method for router.GET

func (*DefaultServer) List

func (d *DefaultServer) List() (gin.HandlerFunc, error)

List is the method for router.GET with query parameters

func (*DefaultServer) Middlewares

func (d *DefaultServer) Middlewares() []MiddlewaresObject

func (*DefaultServer) Name

func (d *DefaultServer) Name() string

Name return the restful API name, default is empty

func (*DefaultServer) Patch

func (d *DefaultServer) Patch() (gin.HandlerFunc, error)

Patch is the method for router.PATCH

func (*DefaultServer) Update

func (d *DefaultServer) Update() (gin.HandlerFunc, error)

Update is the method for router.PUT

func (*DefaultServer) Version

func (d *DefaultServer) Version() string

Version return the restful API version, default is v1

type ListData

type ListData struct {
	// multiple data
	Items interface{} `json:"items,omitempty"`
	// total count
	Total int `json:"total"`
}

ListData multiple data struct

type MiddlewaresObject

type MiddlewaresObject struct {
	Methods     []string
	Middlewares []gin.HandlerFunc
}

type Request

type Request struct {
	ID    uint64 `param:"id"`
	Page  int    `form:"page"`
	Limit int    `form:"limit"`
	// sort field, default is create time
	Sort string `form:"sort"`
	// sort order
	Order string `form:"order"`
}

Request request object

func (*Request) Default

func (q *Request) Default()

func (*Request) HandleQueryParam

func (q *Request) HandleQueryParam(total int) int

HandleQueryParam handle query params

type Response

type Response struct {
	State State `json:"state"`
	// multiple data is a list, single data is a object
	Data interface{} `json:"data,omitempty"`
}

Response base response struct

func DataResponse

func DataResponse(data interface{}) *Response

DataResponse single data response

func ExceptResponse

func ExceptResponse(code int, msg ...interface{}) *Response

ExceptResponse except response

func ListResponse

func ListResponse(total int, data interface{}) *Response

ListResponse multiple data response

func OkResponse

func OkResponse() *Response

OkResponse success response without data

type RestController

type RestController interface {
	// Create is the method for router.POST
	Create() (gin.HandlerFunc, error)
	// Delete is the method for router.DELETE
	Delete() (gin.HandlerFunc, error)
	// Update is the method for router.PUT
	Update() (gin.HandlerFunc, error)
	// Patch is the method for router.PATCH
	Patch() (gin.HandlerFunc, error)
	// Get is the method for router.GET
	Get() (gin.HandlerFunc, error)
	// List is the method for router.GET with query parameters
	List() (gin.HandlerFunc, error)
	// current api verison
	Version() string
	// curent api path
	Name() string
	// middlewares for current api group
	Middlewares() []MiddlewaresObject
}

RestController restful api controller interface

type RestfulAPI

type RestfulAPI struct {

	// prefix for current resource
	PreParameter string
	// postfix for current resource
	PostParameter string
	// contains filtered or unexported fields
}

RestfulAPI restful api struct

func (*RestfulAPI) Install

func (r *RestfulAPI) Install(e *gin.Engine, rc RestController)

Install install api

type State

type State struct {
	// 0 success, other error
	Code int `json:"code"`
	// success or error message
	Msg string `json:"msg"`
}

State base state struct

Directories

Path Synopsis
Description: This file is used to collect metrics for the service.
Description: This file is used to collect metrics for the service.

Jump to

Keyboard shortcuts

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