Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The RootPkg parameter is added for the api template. #3785

Open
6xiaowu9 opened this issue Dec 12, 2023 · 6 comments
Open

The RootPkg parameter is added for the api template. #3785

6xiaowu9 opened this issue Dec 12, 2023 · 6 comments

Comments

@6xiaowu9
Copy link

6xiaowu9 commented Dec 12, 2023

When I customized the template, I found that the package name parameter was not given, which would cause my template and project to be strongly bound, but I don't want this.

refer:#2063
I don't know why this task was closed, but this should be very useful to everyone

@6xiaowu9 6xiaowu9 changed the title feat: The RootPkg parameter is added for the api template. The RootPkg parameter is added for the api template. Dec 12, 2023
@kesonan
Copy link
Collaborator

kesonan commented Dec 14, 2023

What application scenarios require specifying rootPkg? Currently goctl does not support external variables

@6xiaowu9
Copy link
Author

What application scenarios require specifying rootPkg? Currently goctl does not support external variables

I need to use it when customizing the template. I don't want the package name using the template to be customized, but I want to use the currently generated package name. There may be different processing methods for different services, such as the official public Response example. , and I need to use the configuration center in my actual needs. The expected template is as follows:

api/handler.tpl

package {{.PkgName}}

import (
	"net/http"

	"github.com/zeromicro/go-zero/rest/httpx"
	"{{.RootPkg}}/internal/response"
	{{.ImportPackages}}
)

func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		{{if .HasRequest}}var req types.{{.RequestType}}
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		{{end}}l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)
		{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})
        {{if .HasResp}}response.Response(w, resp, err){{else}}response.Response(w, nil, err){{end}}
	}
}

api/main.tpl

package main

import (
	"fmt"
        "{{.rootPkg}}/internal/initialize"
	{{.importPackages}}
)


func main() {

	var c config.Config
        err := initialize.InitConfigFromNacos(func(content string) error {
		return conf.LoadConfigFromYamlBytes([]byte(content), &c)
	})
	if err != nil {
		panic(fmt.Errorf("initialize.InitConfigFromNacos err: %s", err.Error()))
	}

	server := rest.MustNewServer(c.RestConf)
	defer server.Stop()

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}

@6xiaowu9
Copy link
Author

a better solution is to open module variables.

@6xiaowu9
Copy link
Author

Hello, could you please open the module variable in the template to the outside? @kesonan

@6xiaowu9
Copy link
Author

#3786

I submitted a PR here. If you don’t have time to process it and this PR is available, you can combine it. Due to project progress, I can only use my version of goctl for development now. Please reply as soon as possible.

@kesonan
Copy link
Collaborator

kesonan commented Dec 18, 2023

I have already open go module variable for go template on api and rpc generator. use #3798 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants