stargo

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

README

Usage

main.go

import (
	"flag"
	"service/app/internal/server"
	pb "your/proto/path/v1"
 
	"github.com/starfork/stargo"
	"github.com/starfork/stargo/store/mysql"
	"github.com/starfork/stargo/store/redis"
	"google.golang.org/grpc/reflection"
)

func main() { 
	cf := flag.String("c", "../../config/debug.yaml", "config file path")
	flag.Parse()
	sc := server.LoadConfig(*cf)
	c := sc.Server
	app := stargo.New(
		stargo.Org("park"),
		stargo.Name("app"),
		stargo.Config(sc.Server), 
		stargo.UnaryInterceptor(your inteceprot1),
		stargo.UnaryInterceptor(your inteceprot2),
        ...
	)

	s := app.Server()
	if c.Environment == "debug" {
		reflection.Register(s)
	}
	pb.RegisterAppServer(s, server.New(app))
	app.Run()
}

server/handler.go

type handler struct {
	logger logger.Logger 
	c      cache.Cache  
	pb.UnimplementedAppServer
}

// New handler
func New(app *stargo.App) *handler {
    rdc:=app.Store("redis").(*redis.Redis).GetInstance() 
	h := &handler{
		logger: app.GetLogger(), 
		c:      credis.New(rdc),
	}  
	return h
}

更多参考 stargo-examples

环境、工具

protobuf 安装


#Debian
apt-get install protobuf-compiler
#Ubuntu
apt-get install protobuf-compiler
#Alpine
apk add protobuf
#Arch Linux
pacman -S protobuf
#Kali Linux
apt-get install protobuf-compiler
#CentOS
yum install protobuf-compiler
#Fedora
dnf install protobuf-compiler
 
#OS X
brew install protobuf
 
#Raspbian
apt-get install protobuf-compiler
 
#Docker
docker run cmd.cat/protoc protoc

环境相关

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/favadi/protoc-go-inject-tag@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest 
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest
go install golang.org/x/tools/cmd/stringer@latest 

工具相关

grpc-client-cli 命令行调试工具
go install github.com/vadimi/grpc-client-cli/cmd/grpc-client-cli@latest

vscocde 插件
go install github.com/yoheimuta/protolint/cmd/protolint@latest
gostar 项目生成工具
go install  github.com/starfork/gostar@latest
相关库
slice操作相关

https://github.com/starfork/go-slice

加密

https://github.com/starfork/go-crypto

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ENV_DEV        = "dev"        //本地测试环境
	ENV_DOCKER     = "docker"     //docker模式
	ENV_PRODUCTION = "production" //正式环境
)

Functions

This section is empty.

Types

type App

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

App App

func New

func New(opt ...Option) *App

func (*App) GetConfig added in v0.0.3

func (s *App) GetConfig() *config.Config

func (*App) GetLogger added in v0.0.3

func (s *App) GetLogger() logger.Logger

func (*App) RegisterService added in v0.0.4

func (s *App) RegisterService(sd *grpc.ServiceDesc, ss any) *App

func (*App) Restart

func (s *App) Restart()

Restart server

func (*App) Run

func (s *App) Run()

Run server

func (*App) Server

func (s *App) Server() *grpc.Server

Server

func (*App) Service added in v0.0.3

func (s *App) Service() naming.Service

返回标准服务格式

func (*App) Stop

func (s *App) Stop()

Stop server

func (*App) Store added in v0.0.6

func (s *App) Store(name string, st ...store.Store) store.Store

获取或者创建一个store

type Option

type Option func(o *Options)

Option Option

func Addr added in v0.0.3

func Addr(c string) Option

func Config added in v0.0.3

func Config(c *config.Config) Option

func Name added in v0.0.3

func Name(c string) Option

func Org added in v0.0.3

func Org(c string) Option

func Server

func Server(opt ...grpc.ServerOption) Option

Server option

func StreamnIterceptor added in v0.0.6

func StreamnIterceptor(opt grpc.StreamServerInterceptor) Option

StreamInterceptor Stream server interceptor

func UnaryInterceptor

func UnaryInterceptor(opt grpc.UnaryServerInterceptor) Option

UnaryInterceptor Unary server interceptor

type Options

type Options struct {
	Org, Name, Addr   string
	Config            *config.Config
	UnaryInterceptor  []grpc.UnaryServerInterceptor
	StreamInterceptor []grpc.StreamServerInterceptor

	Server []grpc.ServerOption
}

Options 参数

func DefaultOptions

func DefaultOptions() Options

DefaultOptions default options

Directories

Path Synopsis
api
jwt
debug
interceptor
ratelimit
Package ratelimit provides an efficient token bucket implementation that can be used to limit the rate of arbitrary things.
Package ratelimit provides an efficient token bucket implementation that can be used to limit the rate of arbitrary things.
util
geo
web

Jump to

Keyboard shortcuts

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