fix new project without --proto (#88)

pull/91/head
Felix Hao 6 years ago committed by GitHub
parent fcba425d39
commit e7ecfe7ec4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      tool/kratos/project.go
  2. 112
      tool/kratos/template.go

@ -69,6 +69,7 @@ var (
_tplTypeAPIProto: _tplAPIProto,
_tplTypeAPIGenerate: _tplAPIGenerate,
_tplTypeMain: _tplMain,
_tplTypeService: _tplService,
_tplTypeChangeLog: _tplChangeLog,
_tplTypeContributors: _tplContributors,
_tplTypeReadme: _tplReadme,
@ -87,13 +88,11 @@ func create() (err error) {
files[_tplTypeGRPCServer] = "/internal/server/grpc/server.go"
files[_tplTypeAPIProto] = "/api/api.proto"
files[_tplTypeAPIGenerate] = "/api/generate.go"
tpls[_tplTypeHTTPServer] = _tplPBHTTPServer
tpls[_tplTypeGRPCServer] = _tplGRPCServer
tpls[_tplTypeGRPCToml] = _tplGRPCToml
tpls[_tplTypeService] = _tplGPRCService
} else {
tpls[_tplTypeHTTPServer] = delgrpc(_tplHTTPServer)
tpls[_tplTypeService] = _tplService
tpls[_tplTypeMain] = delgrpc(_tplMain)
tpls[_tplTypeMain] = _tplGRPCMain
}
if err = os.MkdirAll(p.Path, 0755); err != nil {
return
@ -125,25 +124,6 @@ func genpb() error {
return cmd.Run()
}
func delgrpc(tpl string) string {
var buf bytes.Buffer
lines := strings.Split(tpl, "\n")
for _, l := range lines {
if strings.Contains(l, "grpc") {
continue
}
if strings.Contains(l, "warden") {
continue
}
if strings.Contains(l, "pb") {
continue
}
buf.WriteString(l)
buf.WriteString("\n")
}
return buf.String()
}
func write(name, tpl string) (err error) {
data, err := parse(tpl)
if err != nil {

@ -64,6 +64,56 @@ demoExpire = "24h"
`
_tplMain = `package main
import (
"context"
"flag"
"os"
"os/signal"
"syscall"
"time"
"{{.Name}}/internal/server/http"
"{{.Name}}/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/log"
)
func main() {
flag.Parse()
if err := paladin.Init(); err != nil {
panic(err)
}
log.Init(nil) // debug flag: log.dir={path}
defer log.Close()
log.Info("{{.Name}} start")
svc := service.New()
httpSrv := http.New(svc)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
s := <-c
log.Info("get a signal %s", s.String())
switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
if err := httpSrv.Shutdown(ctx); err != nil {
log.Error("httpSrv.Shutdown error(%v)", err)
}
log.Info("{{.Name}} exit")
svc.Close()
cancel()
time.Sleep(time.Second)
return
case syscall.SIGHUP:
default:
return
}
}
}
`
_tplGRPCMain = `package main
import (
"context"
"flag"
@ -323,6 +373,67 @@ func (s *Service) Close() {
`
_tplHTTPServer = `package http
import (
"net/http"
"{{.Name}}/internal/model"
"{{.Name}}/internal/service"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/log"
bm "github.com/bilibili/kratos/pkg/net/http/blademaster"
)
var (
svc *service.Service
)
// New new a bm server.
func New(s *service.Service) (engine *bm.Engine) {
var (
hc struct {
Server *bm.ServerConfig
}
)
if err := paladin.Get("http.toml").UnmarshalTOML(&hc); err != nil {
if err != paladin.ErrNotExist {
panic(err)
}
}
svc = s
engine = bm.DefaultServer(hc.Server)
initRouter(engine)
if err := engine.Start(); err != nil {
panic(err)
}
return
}
func initRouter(e *bm.Engine) {
e.Ping(ping)
g := e.Group("/{{.Name}}")
{
g.GET("/start", howToStart)
}
}
func ping(ctx *bm.Context) {
if err := svc.Ping(ctx); err != nil {
log.Error("ping error(%v)", err)
ctx.AbortWithStatus(http.StatusServiceUnavailable)
}
}
// example for http request handler.
func howToStart(c *bm.Context) {
k := &model.Kratos{
Hello: "Golang 大法好 !!!",
}
c.JSON(k, nil)
}
`
_tplPBHTTPServer = `package http
import (
"net/http"
@ -385,6 +496,7 @@ func howToStart(c *bm.Context) {
}
`
_tplAPIProto = `// 定义项目 API 的 proto 文件 可以同时描述 gRPC 和 HTTP API
// protobuf 文件参考:
// - https://developers.google.com/protocol-buffers/

Loading…
Cancel
Save