Skip to content

Commit

Permalink
Feat/serverless (#144)
Browse files Browse the repository at this point in the history
* feat(gen): update gen sdk

* feat(jzero): update serverless design and route2code
  • Loading branch information
jaronnie authored Dec 9, 2024
1 parent 8fca9d0 commit 60cd912
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
12 changes: 10 additions & 2 deletions .template/frame/api/app/serverless/serverless.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import (
"github.com/zeromicro/go-zero/rest"
)

type Serverless struct {
SvcCtx *svc.ServiceContext // 服务上下文
HandlerFunc func(server *rest.Server, svcCtx *svc.ServiceContext) // 服务路由
}

// Serverless please replace coreSvcCtx any type to real core svcCtx
func Serverless(server *rest.Server, coreSvcCtx any) {
func New(coreSvcCtx any) *Serverless {
var c config.Config
if err := conf.Load(filepath.Join("plugins", "{{ .DirName }}", "etc", "etc.yaml"), &c); err != nil {
Expand All @@ -21,5 +26,8 @@ func Serverless(server *rest.Server, coreSvcCtx any) {
config.C = c

svcCtx := svc.NewServiceContext(c)
handler.RegisterHandlers(server, svcCtx)
return &Serverless{
SvcCtx: svcCtx,
HandlerFunc: handler.RegisterHandlers,
}
}{{end}}
39 changes: 33 additions & 6 deletions .template/plugins/api/route2code.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,50 @@ package handler

import (
"net/http"
"path"
"strings"
"sync"

casbinutil "github.com/casbin/casbin/v2/util"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/search"
)

var RoutesCodesMap = map[string]string{
{{ range $v := .Routes }}"{{ $v.Method | upper }}:{{ $v.Path }}":"{{ replace `/` `:` $v.Group | FirstLower }}:{{ $v.Handler | FirstLower}}",
{{ end }}
}

var (
MST map[string]*search.Tree
once sync.Once
)

func Route2Code(r *http.Request) string {
for k, v := range RoutesCodesMap {
if splits := strings.Split(k, ":"); len(splits) >= 2 && splits[0] == strings.ToUpper(r.Method) {
if casbinutil.KeyMatch2(r.URL.Path, strings.Join(splits[1:], ":")) {
return v
once.Do(func() {
RegisterRoute2Code(RoutesCodesMap)
})
if tree, ok := MST[strings.ToUpper(r.Method)]; ok {
if result, ok := tree.Search(path.Clean(r.URL.Path)); ok {
return result.Item.(string)
}
}

return "unknown"
}

func RegisterRoute2Code(maps map[string]string) {
if MST == nil {
MST = make(map[string]*search.Tree)
}
for k, v := range maps {
if splits := strings.Split(k, ":"); len(splits) >= 2 {
if _, ok := MST[splits[0]]; ok {
logx.Must(MST[splits[0]].Add(path.Clean(strings.Join(splits[1:], ":")), v))
} else {
tree := search.NewTree()
logx.Must(tree.Add(strings.Join(splits[1:], ":"), v))
MST[splits[0]] = tree
}
}
}
return "unknown_code"
}
6 changes: 5 additions & 1 deletion .template/plugins/api/serverless_plugins.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
)

func LoadPlugins(server *rest.Server, svcCtx *svc.ServiceContext) {
{{ range $v := .Plugins }}{{ $v.Path | base }}.Serverless(server, svcCtx)
{{ range $v := .Plugins }}
{
serverless := {{ $v.Path | base }}.New(svcCtx)
serverless.HandlerFunc(server, serverless.SvcCtx)
}
{{end}}
}
11 changes: 10 additions & 1 deletion cmd/serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ Copyright © 2024 jaronnie <[email protected]>
package cmd

import (
"os"
"path/filepath"

"github.com/spf13/cobra"

"github.com/jzero-io/jzero/config"
"github.com/jzero-io/jzero/embeded"
"github.com/jzero-io/jzero/internal/serverless/serverlessbuild"
"github.com/jzero-io/jzero/internal/serverless/serverlessdelete"
"github.com/jzero-io/jzero/internal/serverless/serverlessnew"
Expand All @@ -28,6 +32,7 @@ var serverlessNewCmd = &cobra.Command{
if config.C.Serverless.New.Module == "" {
config.C.Serverless.New.Module = args[0]
}
embeded.Home = config.C.Serverless.Home
return serverlessnew.Run(args)
},
Args: cobra.ExactArgs(1),
Expand All @@ -38,6 +43,7 @@ var serverlessBuildCmd = &cobra.Command{
Short: "jzero serverless build",
Long: `jzero serverless build.`,
RunE: func(cmd *cobra.Command, args []string) error {
embeded.Home = config.C.Serverless.Home
return serverlessbuild.Run()
},
}
Expand All @@ -47,19 +53,22 @@ var serverlessDeleteCmd = &cobra.Command{
Short: "jzero serverless delete",
Long: `jzero serverless delete.`,
RunE: func(cmd *cobra.Command, args []string) error {
embeded.Home = config.C.Serverless.Home
return serverlessdelete.Run()
},
}

func init() {
wd, _ := os.Getwd()

rootCmd.AddCommand(serverlessCmd)
serverlessCmd.AddCommand(serverlessBuildCmd)
serverlessCmd.AddCommand(serverlessDeleteCmd)
serverlessCmd.AddCommand(serverlessNewCmd)

serverlessCmd.PersistentFlags().StringP("home", "", filepath.Join(wd, ".template"), "set templates path")
serverlessNewCmd.Flags().BoolP("core", "", false, "is core serverless")
serverlessNewCmd.Flags().StringP("module", "m", "", "set go module")
serverlessNewCmd.Flags().StringP("home", "", "", "set templates path")
serverlessNewCmd.Flags().StringP("remote", "r", "https://github.com/jzero-io/templates", "remote templates repo")
serverlessNewCmd.Flags().StringP("branch", "b", "", "remote templates repo branch")
serverlessNewCmd.Flags().StringP("frame", "", "api", "use frame")
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ type UpgradeConfig struct {
}

type ServerlessConfig struct {
Home string `mapstructure:"home"` // 使用的模板文件目录

New NewConfig `mapstructure:"new"`
Delete ServerlessDeleteConfig `mapstructure:"delete"`
}
Expand Down

0 comments on commit 60cd912

Please sign in to comment.