kratos

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2023 License: MIT Imports: 36 Imported by: 2

README

基于kratos进行二开,可以进行项目的快速接入和开发,对新手友好。

项目安装

安装kratos

go install github.com/limes-cloud/kratos/cmd/kratos@latest 

安装protoc

进入一下连接,安装自己合适的版本即可
https://github.com/protocolbuffers/protobuf/releases/

安装扩展包

go install github.com/limes-cloud/kratos/cmd/protoc-gen-go-http@latest &&\
go install github.com/limes-cloud/kratos/cmd/protoc-gen-go-errors@latest &&\
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest &&\
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest &&\
go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest &&\
go install github.com/envoyproxy/protoc-gen-validate@latest

进行自动生成grpc代码配置

  • 1)打开Goland设置
  • 2)找到Tool 》 File Watcher
  • 3)添加custom配置,文件类型选择proto buffer
  • 4)配置一下命令,program 选择你下载的protoc,args配置为以下代码
--proto_path=$FileDir$
--proto_path=$ProjectFileDir$/third_party
--go_out=:.
--go-http_out=.
--go-grpc_out=.
--go-errors_out=.
--validate_out=lang=go:.
$FilePath$

这样你只要保存proto文件就会自动生成对应的grpc go的代码了。

目录说明

internal 我们的业务实现主要在这里
    |- service 主要实现grpc的接口,新加一个proto方法,就需要在这里新增一个对应的方法调用
    |- logic 主要是具体函数的业务逻辑实现
    |- model 主要是具体函数的数据层的操作

配置说明

配置主要在config/config.yaml文件夹下定义,接下来我们对配置字段进行详细讲解。

服务

server:
  http:
    addr: 0.0.0.0:8000
    timeout: 1s
    formatResponse: true
  grpc:
    addr: 0.0.0.0:9000
    timeout: 1s

Documentation

Index

Constants

View Source
const Release = "v2.7.0"

Release is the current kratos version.

Variables

This section is empty.

Functions

func EncoderError

func EncoderError() http.EncodeErrorFunc

EncoderError 错误响应封装

func EncoderResponse

func EncoderResponse() http.EncodeResponseFunc

EncoderResponse 请求响应封装

func NewContextFromApp

func NewContextFromApp(ctx context.Context, s AppInfo) context.Context

NewContextFromApp returns a new Context that carries value.

Types

type App

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

App is an application components lifecycle manager.

func New

func New(opts ...Option) *App

New create an application lifecycle manager.

func (*App) Config

func (a *App) Config() config.Config

Config returns service config.

func (*App) Endpoint

func (a *App) Endpoint() []string

Endpoint returns endpoints.

func (*App) ID

func (a *App) ID() string

ID returns app instance id.

func (*App) Metadata

func (a *App) Metadata() map[string]string

Metadata returns service metadata.

func (*App) Name

func (a *App) Name() string

Name returns service name.

func (*App) Run

func (a *App) Run() error

Run executes all OnStart hooks registered with the application's Lifecycle.

func (*App) Stop

func (a *App) Stop() (err error)

Stop gracefully stops the application.

func (*App) Version

func (a *App) Version() string

Version returns app version.

type AppInfo

type AppInfo interface {
	ID() string
	Name() string
	Version() string
	Metadata() map[string]string
	Endpoint() []string
	Config() config.Config
}

AppInfo is application context value.

func GetAppFromContext

func GetAppFromContext(ctx context.Context) (s AppInfo, ok bool)

GetAppFromContext returns the Transport value stored in ctx, if any.

type Context

type Context interface {
	Logger() *log.Helper
	DB(name ...string) *gorm.DB
	Go(runner pool.Runner) error
	Cert(name string) []byte
	Email() (email.Sender, error)
	ClientIP() string
	Captcha() captcha.Captcha
	JWT() jwt.Jwt
	Authentication() authentication.Authentication
	Ctx() context.Context

	ID() string
	Name() string
	Version() string
	Metadata() map[string]string
	Config() config.Config
	Endpoint() []string

	Deadline() (deadline time.Time, ok bool)
	Done() <-chan struct{}
	Err() error
	Value(key any) interface{}
}

func MustContext

func MustContext(c context.Context) Context

MustContext returns the Transport value stored in ctx, if any.

type LogField

type LogField map[string]interface{}

type Option

type Option func(o *options)

Option is an application option.

func AfterStart

func AfterStart(fn func(context.Context) error) Option

AfterStart run funcs after app starts

func AfterStop

func AfterStop(fn func(context.Context) error) Option

AfterStop run funcs after app stops

func BeforeStart

func BeforeStart(fn func(context.Context) error) Option

BeforeStart run funcs before app starts

func BeforeStop

func BeforeStop(fn func(context.Context) error) Option

BeforeStop run funcs before app stops

func Config

func Config(source config.Source) Option

Config with service config.

func Endpoint

func Endpoint(endpoints ...*url.URL) Option

Endpoint with service endpoint.

func ID

func ID(id string) Option

ID with service id.

func LoggerWith

func LoggerWith(fields LogField) Option

LoggerWith with service logger with field.

func Metadata

func Metadata(md map[string]string) Option

Metadata with service metadata.

func Name

func Name(name string) Option

Name with service name.

func Registrar

func Registrar(r registry.Registrar) Option

Registrar with service registry.

func RegistrarServer

func RegistrarServer(fn registrarServerFn) Option

RegistrarServer with service register server.

func RegistrarTimeout

func RegistrarTimeout(t time.Duration) Option

RegistrarTimeout with registrar timeout.

func Server

func Server(srv ...transport.Server) Option

Server with transport servers.

func Signal

func Signal(sigs ...os.Signal) Option

Signal with exit signals.

func StopTimeout

func StopTimeout(t time.Duration) Option

StopTimeout with app stop timeout.

func Version

func Version(version string) Option

Version with service version.

Jump to

Keyboard shortcuts

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