Melkeydev/go-blueprint

[Feature Request] unifying db and non db framework templates

Closed this issue · 3 comments

Tell us about your feature request

While working on #174 I noticed that the database and non database templates for all frameworks are very similar. I would like to combine all these files and add if conditions in the template itself to check if the database endpoint and dependencies are required or not. This would also make it easier to add a conditional websocket endpoint in the same file.

I'm using the chi files as an example here but the others are also very similar

$ diff ./cmd/template/framework/files/routes/chi.go.tmpl ./cmd/template/framework/files/dbRoutes/chi.go.tmpl
5a6
> 	"encoding/json"
11d11
< 
18a19
> 	r.Get("/health", s.healthHandler)
34c35,40
< }
\ No newline at end of file
---
> }
> 
> func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {
> 	jsonResp, _ := json.Marshal(s.db.Health())
> 	_, _ = w.Write(jsonResp)
> }

Disclaimer

  • I agree

This is a good idea. If you want, you can do it. I am just interested in how you would implement that. Would it be similar to .env injections or another approach?

case "env":
        if p.DBDriver != "none" {

            envBytes := [][]byte{
                tpl.GlobalEnvTemplate(),
                p.DBDriverMap[p.DBDriver].templater.Env(),
            }
            createdTemplate := template.Must(template.New(fileName).Parse(string(bytes.Join(envBytes, []byte("\n")))))
            err = createdTemplate.Execute(createdFile, p)

        } else {
            createdTemplate := template.Must(template.New(fileName).Parse(string(tpl.GlobalEnvTemplate())))
            err = createdTemplate.Execute(createdFile, p)
        }

I can't rly work on anything for a while actually.

We'll leave it open, perhaps someone else will take care of it, or when you have time, you can tackle this one.