common

package
v0.0.0-...-fcb87a8 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package common 核心类型定义

Index

Constants

View Source
const (
	// CodeCommonError 通用的错误代码
	CodeCommonError = 5000 + iota
	//CodeBadRequest 请求的解析错误
	CodeBadRequest
	// CodeInvalidInputField 输入的字段格式错误
	CodeInvalidInputField
	// CodeDBError 数据库错误
	CodeDBError
	// CodeSessionInvalidOrExpired 会话无效或者已经过期
	CodeSessionInvalidOrExpired
	// CodeRequireLogin 需要登录
	CodeRequireLogin
)

错误码定义

Variables

View Source
var (
	// ErrorInvalidSession 会话无效或者已经过期
	ErrorInvalidSession = &APIError{
		Code:    CodeSessionInvalidOrExpired,
		Message: "Session is invalid or expired",
	}
	// ErrorUnauthorized 用户未登录
	ErrorUnauthorized = &APIError{
		Code:    CodeRequireLogin,
		Message: "User unauthorized",
	}
)

错误定义

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Code       int
	Message    string
	InnerError error
}

APIError api错误定义

func NewAPIError

func NewAPIError(code int, message string, innerError ...error) *APIError

NewAPIError 初始化一个新的错误

type APIResponse

type APIResponse struct {
	Code    int         `json:"code"`
	Data    interface{} `json:"data"`
	Message string      `json:"message"`
}

APIResponse 给客户端的json响应

type AppConfig

type AppConfig struct {
	ListenAddress  string          `yaml:"listen_address"` //http服务监听地址端口
	Timezone       string          //时区
	Database       *DatabaseConfig //数据库配置
	EnableRestDocs bool            `yaml:"enable_rest_docs"` //是否挂载rest api文档
	CommonLogFile  string          `yaml:"common_log_file"`  //普通日志文件
	ErrorLogFile   string          `yaml:"error_log_file"`   //错误日志文件路径
}

AppConfig 应用配置

func LoadAppConfig

func LoadAppConfig() (*AppConfig, error)

LoadAppConfig 加载工作目录下的配置文件

func LoadAppConfigFromFile

func LoadAppConfigFromFile(configFile *os.File) (*AppConfig, error)

LoadAppConfigFromFile 从yaml文件中加载配置

type DBContext

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

DBContext 包裹数据库handle的结构体

func InitDBContext

func InitDBContext(ctx context.Context, databaseConfig *DatabaseConfig) (*DBContext, error)

InitDBContext 初始化新的数据库连接

func (*DBContext) Client

func (c *DBContext) Client() *mongo.Client

Client 获取mongodb client handle

func (*DBContext) Close

func (c *DBContext) Close(ctx context.Context) error

Close 关闭连接

func (*DBContext) Collection

func (c *DBContext) Collection(shortName string) *mongo.Collection

Collection 获取集合的完整名称

func (*DBContext) CollectionName

func (c *DBContext) CollectionName(shortName string) string

CollectionName 获取集合句柄

func (*DBContext) Database

func (c *DBContext) Database() *mongo.Database

Database 获取数据库handle

type DatabaseConfig

type DatabaseConfig struct {
	URI              string // mongodb数据连接字符串
	Name             string //数据库名称
	CollectionPrefix string `yaml:"collection_prefix"` //集合前缀
}

DatabaseConfig 数据库配置

type Migration

type Migration interface {
	// Name 迁移的名称
	Name() string
	// Up 执行迁移
	Up() error
	// Down 回滚迁移
	Down() error
}

Migration 数据迁移接口声明

type MigrationLog

type MigrationLog struct {
	ID        int       `bson:"_id"` //id
	Name      string    //名称
	Batch     int       //批次
	CreatedAt time.Time `bson:"created_at"` //迁移执行的时间
}

MigrationLog 数据迁移记录

Jump to

Keyboard shortcuts

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