generated from pkg/go-template
feat :
This commit is contained in:
@@ -1,7 +1,75 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go.dedis.ch/protobuf"
|
||||
)
|
||||
|
||||
type QueryCondition struct {
|
||||
Page *int
|
||||
Size *int
|
||||
QueryMap map[string]string
|
||||
}
|
||||
|
||||
type (
|
||||
ints []int
|
||||
strs []string
|
||||
scripts []Script
|
||||
)
|
||||
|
||||
type Script struct {
|
||||
ScriptName string `json:"script_name" gorm:"column:script_name"`
|
||||
ScriptIntro string `json:"script_intro" gorm:"column:script_intro"`
|
||||
ScriptTag ints `json:"script_tag" gorm:"column:script_tag"`
|
||||
ScriptScore float64 `json:"script_score" gorm:"column:script_score"`
|
||||
GroupDuration int `json:"group_duration" gorm:"column:group_duration"`
|
||||
ScriptCoverUrl string `json:"script_cover_url" gorm:"column:script_cover_url"`
|
||||
ScriptTextContext string `json:"script_text_context" gorm:"column:script_text_context"`
|
||||
ScriptPlotScore float64 `json:"script_plot_score" gorm:"column:script_plot_score"`
|
||||
ScriptImageContent strs `json:"script_image_content" gorm:"column:script_image_content"`
|
||||
ScriptMalePlayer int `json:"script_male_player" gorm:"column:script_male_player"`
|
||||
ScriptFemalePlayer int `json:"script_female_player" gorm:"column:script_female_player"`
|
||||
ScriptDifficultDegree string `json:"script_difficult_degree" gorm:"column:script_difficult_degree"`
|
||||
ScriptPlayerLimit int `json:"script_player_limit" gorm:"column:script_player_limit"`
|
||||
Uuid string `json:"uuid" gorm:"column:uuid" valid:"no_empty"`
|
||||
ScriptComplexScore float64 `json:"script_complex_score" gorm:"column:script_complex_score"`
|
||||
Qid string `json:"qid" gorm:"column:qid"`
|
||||
IsDel int `json:"-" gorm:"column:is_del"`
|
||||
}
|
||||
|
||||
type Scripts struct {
|
||||
Scripts scripts `json:"scripts" `
|
||||
}
|
||||
|
||||
type (
|
||||
Tag struct {
|
||||
Value string `json:"value" gorm:"column:value"`
|
||||
Uuid int `json:"uuid" gorm:"primary_key" valid:"no_empty"`
|
||||
IsDel int `json:"-" gorm:"column:is_del" value:"1|0"`
|
||||
Cs []Category `json:"categories,omitempty" gorm:"many2many:c_tag;foreignKey:Uuid;joinForeignKey:TagUid;References:Uuid;JoinReferences:CategoryUid" valid:"no_empty"`
|
||||
}
|
||||
)
|
||||
|
||||
type (
|
||||
Category struct {
|
||||
Value string `json:"value" gorm:"column:value"`
|
||||
Uuid string `json:"uuid" gorm:"primary_key" valid:"no_empty"`
|
||||
IsDel int `json:"-" gorm:"column:is_del"`
|
||||
Tags []Tag `json:"tags,omitempty" gorm:"many2many:c_tag;foreignKey:Uuid;joinForeignKey:CategoryUid;References:Uuid;JoinReferences:TagUid" valid:"no_empty"`
|
||||
}
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("我是个示例")
|
||||
w := &bytes.Buffer{}
|
||||
types := []interface{}{
|
||||
Tag{},
|
||||
Category{},
|
||||
}
|
||||
err := protobuf.GenerateProtobufDefinition(w, types, nil, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(w.String())
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type (
|
||||
ints []int
|
||||
strs []string
|
||||
)
|
||||
|
||||
type Scripts struct {
|
||||
ScriptName string `json:"script_name" gorm:"column:script_name"`
|
||||
ScriptIntro string `json:"script_intro" gorm:"column:script_intro"`
|
||||
ScriptTag ints `json:"script_tag" gorm:"column:script_tag"`
|
||||
ScriptScore float64 `json:"script_score" gorm:"column:script_score"`
|
||||
GroupDuration int `json:"group_duration" gorm:"column:group_duration"`
|
||||
ScriptCoverUrl string `json:"script_cover_url" gorm:"column:script_cover_url"`
|
||||
ScriptTextContext string `json:"script_text_context" gorm:"column:script_text_context"`
|
||||
ScriptPlotScore float64 `json:"script_plot_score" gorm:"column:script_plot_score"`
|
||||
ScriptImageContent strs `json:"script_image_content" gorm:"column:script_image_content"`
|
||||
ScriptMalePlayer int `json:"script_male_player" gorm:"column:script_male_player"`
|
||||
ScriptFemalePlayer int `json:"script_female_player" gorm:"column:script_female_player"`
|
||||
ScriptDifficultDegree string `json:"script_difficult_degree" gorm:"column:script_difficult_degree"`
|
||||
ScriptPlayerLimit int `json:"script_player_limit" gorm:"column:script_player_limit"`
|
||||
Uuid string `json:"uuid" gorm:"column:uuid" valid:"no_empty"`
|
||||
ScriptComplexScore float64 `json:"script_complex_score" gorm:"column:script_complex_score"`
|
||||
Qid string `json:"qid" gorm:"column:qid"`
|
||||
IsDel int `json:"-" gorm:"column:is_del"`
|
||||
}
|
||||
|
||||
func (m *Scripts) TableName() string {
|
||||
return "scripts"
|
||||
}
|
||||
|
||||
func (p ints) Value() (driver.Value, error) {
|
||||
return json.Marshal(p)
|
||||
}
|
||||
|
||||
func (p *ints) Scan(input interface{}) error {
|
||||
return json.Unmarshal(input.([]byte), &p)
|
||||
}
|
||||
|
||||
func (p strs) Value() (driver.Value, error) {
|
||||
return json.Marshal(p)
|
||||
}
|
||||
|
||||
func (p *strs) Scan(input interface{}) error {
|
||||
return json.Unmarshal(input.([]byte), &p)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Finish struct {
|
||||
IsDone bool
|
||||
Err error
|
||||
}
|
||||
|
||||
type Worker interface {
|
||||
Work(ctx context.Context, finishChan chan<- Finish)
|
||||
}
|
||||
|
||||
type WorkerInterFace struct {
|
||||
}
|
||||
|
||||
func (w WorkerInterFace) Work(ctx context.Context, finishChan chan<- Finish) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func Run(timeout time.Duration, ctx context.Context, workers ...Worker) error {
|
||||
cancel, cancelFunc := context.WithTimeout(ctx, timeout)
|
||||
finishSignal := make(chan Finish)
|
||||
errs := make([]error, 0)
|
||||
defer cancelFunc()
|
||||
for _, worker := range workers {
|
||||
go worker.Work(cancel, finishSignal)
|
||||
}
|
||||
for i := 0; i < len(workers); i++ {
|
||||
if f := <-finishSignal; !f.IsDone {
|
||||
errs = append(errs, f.Err)
|
||||
if len(errs) == 1 {
|
||||
cancelFunc()
|
||||
}
|
||||
}
|
||||
}
|
||||
close(finishSignal)
|
||||
if len(errs) != 0 {
|
||||
return errs[0]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func SafeSend(ch chan<- Finish, value Finish) {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
|
||||
}
|
||||
}()
|
||||
ch <- value // panic if ch is closed
|
||||
}
|
||||
|
||||
func Watcher(ctx context.Context, ch chan<- Finish) {
|
||||
go func() {
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
//无论主动还是被动推出。总会
|
||||
case <-ctx.Done():
|
||||
SafeSend(ch, Finish{
|
||||
IsDone: false,
|
||||
Err: errors.New(""),
|
||||
})
|
||||
break loop
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package pkg
|
||||
@@ -1,31 +0,0 @@
|
||||
package servesr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murder"
|
||||
"google.golang.org/grpc"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
type helloRequet struct {
|
||||
murder.UnimplementedHelloWorldServer
|
||||
}
|
||||
|
||||
func (helloRequet) Login(ctx context.Context, name *murder.HelloRequest) (*murder.HelloResponse, error) {
|
||||
return &murder.HelloResponse{Token: name.Code}, nil
|
||||
}
|
||||
|
||||
func RpcServer() {
|
||||
lis, err := net.Listen("tcp", ":3000")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
s := grpc.NewServer()
|
||||
murder.RegisterHelloWorldServer(s, &helloRequet{})
|
||||
fmt.Println("lark server run in :3000")
|
||||
if err := s.Serve(lis); err != nil {
|
||||
log.Fatalf("failed to serve: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package servesr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/service/token/internal/model"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/service/token/internal/pkg"
|
||||
"git.icechen.cn/monorepo/backend/pkg/orm"
|
||||
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murders"
|
||||
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murders/script"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Script struct {
|
||||
murders.UnimplementedMurdersServer
|
||||
pkg.WorkerInterFace
|
||||
queryMap *murders.QueryCondition
|
||||
scriptModel *[]model.Scripts
|
||||
}
|
||||
|
||||
func (s *Script) GetScripts(ctx context.Context, queryMap *murders.QueryCondition) (*script.Scripts, error) {
|
||||
s.queryMap = queryMap
|
||||
err := pkg.Run(1*time.Second, ctx, s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (s *Script) Work(ctx context.Context, finishChan chan<- pkg.Finish) {
|
||||
go pkg.Watcher(ctx, finishChan)
|
||||
db, err := orm.GetContextDB(ctx, orm.DB)
|
||||
if err != nil {
|
||||
pkg.SafeSend(finishChan, pkg.Finish{
|
||||
IsDone: false,
|
||||
Err: err,
|
||||
})
|
||||
}
|
||||
|
||||
i := new([]model.Scripts)
|
||||
if num := db.Find(i).RowsAffected; num < 0 {
|
||||
pkg.SafeSend(finishChan, pkg.Finish{
|
||||
IsDone: false,
|
||||
Err: errors.New("RowsAffected < 0"),
|
||||
})
|
||||
}
|
||||
|
||||
s.scriptModel = i
|
||||
pkg.SafeSend(finishChan, pkg.Finish{
|
||||
IsDone: true,
|
||||
Err: nil,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package servesr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.icechen.cn/monorepo/backend/pkg/proto/brahma/murders"
|
||||
"git.icechen.cn/monorepo/backend/pkg/rpc"
|
||||
ctxLogger "github.com/luizsuper/ctxLoggers"
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/grpc"
|
||||
"net"
|
||||
)
|
||||
|
||||
func RpcServer() {
|
||||
lis, err := net.Listen("tcp", ":3000")
|
||||
if err != nil {
|
||||
ctxLogger.Info(nil, "brahma server start up error", zap.String("error", err.Error()))
|
||||
}
|
||||
s := grpc.NewServer(grpc.UnaryInterceptor(rpc.Interceptor))
|
||||
murders.RegisterMurdersServer(s, &Script{})
|
||||
fmt.Println("lark server run in :3000")
|
||||
if err := s.Serve(lis); err != nil {
|
||||
ctxLogger.Info(nil, "brahma server start up error", zap.String("error", err.Error()))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package servesr
|
||||
@@ -1,9 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
servesr "git.icechen.cn/monorepo/backend/app/brahma/service/token/internal/server"
|
||||
)
|
||||
import servesr "git.icechen.cn/monorepo/backend/app/brahma/service/token/internal/server"
|
||||
|
||||
func main() {
|
||||
servesr.RpcServer()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user