webx

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2025 License: MIT Imports: 25 Imported by: 0

README

WebX

概述

WebX 是一个封装好的 Web 服务开发框架

包含以下功能:

  • systemd 守护进程
  • config 自动加载配置
  • handler
    • 原始 gin.HandlerFunc(*gin.Content)
    • 二次封装接口 webx.HandlerFunc[T,P any] func(context.Context,T) (P, error)

目录结构

  • /webx : WebX 框架
  • /config : 配置文件
  • /service : 业务服务
  • /handler : 业务接口

使用

go get -u github.com/virzz/webx@latest

Documentation

Overview

Example
package main

import (
	"context"

	"github.com/gin-contrib/requestid"
	"github.com/gin-gonic/gin"

	"github.com/virzz/ginx/code"
	"github.com/virzz/ginx/req"
	"github.com/virzz/ginx/rsp"
	"github.com/virzz/vlog"

	"github.com/virzz/webx"
)

const (
	id   = "com.ctfhub.app.webx.test"
	name = "webx-test"
	desc = "WebxTest"
)

var (
	Version = "latest"
	Commit  = "unknown"
)

// config.go

type Config struct {
	//lint:ignore SA5008 Ignore JSON option "squash"
	webx.Config `json:",inline,squash" yaml:",inline"`
	// Define custom configuration here
	Custom CustomConfig `json:"custom" yaml:"custom"`
}

type CustomConfig struct{}

var Conf = &Config{}

func main() {
	app := &webx.App{
		ID: id, Name: name, Description: desc,
		Version: Version, Commit: Commit,
	}
	if err := webx.Run(context.Background(), app, Conf); err != nil {
		panic(err)
	}
}

func init() {
	webx.POST("/test", HandleTest)
}

type (
	TestReq struct {
		Example string `json:"example" form:"example" uri:"example"`
	}
	TestRsp struct{}
)

func HandleTest(c *gin.Context, req TestReq) (rsp TestRsp, err error) {
	return rsp, nil
}

func HandleGinTest(c *gin.Context) {
	rid := requestid.Get(c)
	// 解析入参
	var _req TestReq
	if err := req.ShouldBind(c, &_req); err != nil {
		vlog.Error("Failed to bind req", "requestid", rid, "err", err.Error())
		c.AbortWithStatusJSON(400, rsp.E(code.ParamInvalid, "Error:"+err.Error()))
		return
	}
	// 业务处理
	if _req.Example != "test" {
		vlog.Error("Failed to bind req", "requestid", rid, "err", "Example is not test")
		c.AbortWithStatusJSON(500, rsp.E(code.UnknownErr, "Error: Example is not test"))
		return
	}
	// 返回结果
	c.JSON(200, rsp.OK())
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCommand

func AddCommand(cmd ...*cobra.Command)

func Any

func Any[T, P any](p string, h HandlerFunc[T, P])

Any register a Any method handler

func AppID

func AppID() string

func Commit

func Commit() string

func CreateCmd

func CreateCmd(cfg Configer) *cobra.Command

func Desc

func Desc() string

func Description

func Description() string

func Execute

func Execute(ctx context.Context, cfg Configer) error

func GET

func GET[T, P any](p string, h HandlerFunc[T, P])

GET register a GET method handler

func GinAny

func GinAny(p string, h gin.HandlerFunc)

GinAny register a gin Any method handler

func GinGET

func GinGET(p string, h gin.HandlerFunc)

GinGET register a gin GET method handler

func GinPOST

func GinPOST(p string, h gin.HandlerFunc)

GinPOST register a gin POST method handler

func ID

func ID() string

func InitCmd

func InitCmd(cfg Configer, models ...any) *cobra.Command

func MigrateCmd

func MigrateCmd(cfg Configer, models ...any) *cobra.Command

func Name

func Name() string

func POST

func POST[T, P any](p string, h HandlerFunc[T, P])

POST register a POST method handler

func Register

func Register(f ginx.RegisterFunc)

Register register any method handler

func RegisterHandle

func RegisterHandle[T, P any](m, p string, h HandlerFunc[T, P])

RegisterHandle register any method handler

func Run

func Run(ctx context.Context, app *App, cfg Configer) error

func SetPreInit

func SetPreInit(f PreInitFunc)

func SetValidate

func SetValidate(f ValidateFunc)

func Version

func Version() string

func Warpper

func Warpper[T, P any](handle HandlerFunc[T, P]) gin.HandlerFunc

func WarpperRaw

func WarpperRaw(handle HandlerRawFunc) gin.HandlerFunc

Types

type App

type App struct {
	ID          string
	Name        string
	Description string
	Version     string
	Commit      string
}

type Config

type Config struct {
	HTTP ginx.Config `json:"http" yaml:"http"`
	DB   db.Config   `json:"db" yaml:"db"`
}

func (*Config) GetDB

func (c *Config) GetDB() *db.Config

func (*Config) GetHTTP

func (c *Config) GetHTTP() *ginx.Config

func (*Config) Validate

func (c *Config) Validate() error

type Configer

type Configer interface {
	Validate() error
	GetHTTP() *ginx.Config
	GetDB() *db.Config
}
var Conf Configer

type HandlerFunc

type HandlerFunc[T, P any] func(ctx *gin.Context, req T) (rsp P, err error)

type HandlerRawFunc

type HandlerRawFunc func(ctx *gin.Context) (rsp *rsp.Rsp, err error)

type PreInitFunc

type PreInitFunc func(ctx context.Context) error

type ValidateFunc

type ValidateFunc func() error

Directories

Path Synopsis
handler
model

Jump to

Keyboard shortcuts

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