diff --git a/.gitignore b/.gitignore index 0665fbe..9a84990 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ c.out # This pattern will ignore any folders starting with "new_project" new_project*/ +.env + # IDE and OS specific files .vscode/ .idea/ diff --git a/README.md b/README.md index 6db2bc4..a21c675 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,26 @@ -# Go Bootstrapper +# ๐Ÿนโšก Go Bootstrapper **Go Bootstrapper** is a CLI tool that scaffolds production-ready Golang projects โ€” no dependency headaches, no manual setup. Just run a command and get a fully configured project with linters, routers, and structure ready to code. * * * -## Installation + +## โœจ Features + +* ๐Ÿ— **Create new Golang projects instantly** โ€” skip the boilerplate setup. + +* โšก **Framework-ready templates** โ€” built-in support for `Gin`, `Chi`, and more. + +* ๐Ÿ“‚ **Standardized structure** โ€” organized directories: `cmd/`, `internal/`, `router/`, etc. + +* ๐Ÿ”ฎ **Extensible design** โ€” bring your own templates or modify existing ones. + +* ๐Ÿงฑ **Preconfigured tooling** โ€” includes Makefile, linters, and testing setup (coming soon). + + +* * * + +## ๐Ÿ“ฆ Installation Install globally using `go install`: @@ -16,7 +32,7 @@ Once installed, confirm the installation: * * * -## Quick Start ๐Ÿ’จ +## ๐Ÿš€ Quick Start Create a REST API project using **Gin**: @@ -32,30 +48,15 @@ bootstrap new myapp --type=rest --router=gin --db=postgres * * * -## Example Project Structure +## ๐Ÿ“ Example Project Structure ``` - myapp/ -โ”œโ”€โ”€ Makefile -โ”œโ”€โ”€ README.md -โ”œโ”€โ”€ cmd/ -โ”‚ โ””โ”€โ”€ main.go -โ”œโ”€โ”€ internal/ -โ”‚ โ”œโ”€โ”€ config/ -โ”‚ โ”‚ โ””โ”€โ”€ config.go -โ”‚ โ”œโ”€โ”€ handler/ -โ”‚ โ”‚ โ””โ”€โ”€ user_handler.go -โ”‚ โ”œโ”€โ”€ router/ -โ”‚ โ”‚ โ””โ”€โ”€ routes.go -โ”‚ โ””โ”€โ”€ db/ โ† created only if --db flag is passed -โ”‚ โ””โ”€โ”€ db.go -โ””โ”€โ”€ go.mod - +myapp/ โ”œโ”€โ”€ Makefile โ”œโ”€โ”€ README.md โ”œโ”€โ”€ cmd/ โ”‚ โ””โ”€โ”€ main.go โ”œโ”€โ”€ internal/ โ”‚ โ”œโ”€โ”€ config/ โ”‚ โ”‚ โ””โ”€โ”€ config.go โ”‚ โ”œโ”€โ”€ handler/ โ”‚ โ”‚ โ””โ”€โ”€ user_handler.go โ”‚ โ”œโ”€โ”€ router/ โ”‚ โ”‚ โ””โ”€โ”€ routes.go โ”‚ โ””โ”€โ”€ db/ โ† created only if --db flag is passed โ”‚ โ””โ”€โ”€ db.go โ””โ”€โ”€ go.mod ``` * * * -## CLI Options +## โš™๏ธ CLI Options | Flag | Description | Example | | --- | --- | --- | @@ -66,7 +67,7 @@ bootstrap new myapp --type=rest --router=gin --db=postgres * * * -## Why Go Bootstrapper? +## ๐Ÿ’ก Why Go Bootstrapper? Developers often waste time repeating setup tasks โ€” creating folders, configuring routers, writing Makefiles, adding linters, etc. @@ -75,31 +76,34 @@ You focus on business logic โ€” it handles the rest. Itโ€™s like: -> `create-react-app`, but for Golang ๏ฟฝ +> `create-react-app`, but for Golang ๐Ÿน * * * -## Roadmap +## ๐Ÿ›ฃ๏ธ Roadmap * Add `--with-auth` flag for JWT + middleware setup -* `add` command to make CLI tool more extensible to generate ``service``, ``handlers``, ``controllers``. -* Commands like ``build``, ``test``, ``dev``, ``fmt`` to make it more developer friendly, ensuring production ready code. -* ``init`` that will be used for letting users to choose their configurations via ``TUI``. + +* Add Docker & Docker Compose templates + +* Support for Fiber, Echo, and gRPC + +* Generate Swagger / OpenAPI docs + +* Add custom template registry (`bootstrap add template`) * * * -## Contributing +## ๐Ÿค Contributing Contributions, feedback, and ideas are welcome! Feel free to open an issue or PR on [GitHub](https://github.com/upsaurav12/bootstrap). -Consider star the project ๐Ÿ™ - * * * -## License +## ๐Ÿ“„ License Licensed under the **MIT License** ยฉ 2025 [Saurav Upadhyay](https://github.com/upsaurav12) -* * * +* * * \ No newline at end of file diff --git a/api/requestDocker.go b/api/requestDocker.go new file mode 100644 index 0000000..a901761 --- /dev/null +++ b/api/requestDocker.go @@ -0,0 +1,8 @@ +package api + +type DockerRequest struct { +} + +func RequestToDocker(prompt string) { + +} diff --git a/api/requestGroq.go b/api/requestGroq.go new file mode 100644 index 0000000..c2303ed --- /dev/null +++ b/api/requestGroq.go @@ -0,0 +1,80 @@ +package api + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "os" + + "github.com/joho/godotenv" +) + +type ChatMessage struct { + Role string `json:"role"` + Content string `json:"content"` +} + +type GroqChatRequest struct { + Model string `json:"model"` + Messages []ChatMessage `json:"messages"` +} + +func CallingApiToGroq(prompt string) (string, error) { + + _ = godotenv.Load() + + url := os.Getenv("GROQ_API_URL") + key := os.Getenv("GROQ_API_KEY") + + if url == "" || key == "" { + return "", fmt.Errorf("missing GROQ_API_URL or GROQ_API_KEY") + } + + body := GroqChatRequest{ + Model: "llama-3.1-8b-instant", + Messages: []ChatMessage{ + {Role: "user", Content: prompt}, + }, + } + + jsonBody, _ := json.Marshal(body) + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody)) + if err != nil { + return "", err + } + + req.Header.Set("Authorization", "Bearer "+key) + req.Header.Set("Content-Type", "application/json") + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + // READ RAW BODY FIRST + rawBody, _ := io.ReadAll(resp.Body) + // fmt.Println("RAW RESPONSE:", string(rawBody)) + + // Decode into struct + var result struct { + Choices []struct { + Message struct { + Content string `json:"content"` + } `json:"message"` + } `json:"choices"` + } + + if err := json.Unmarshal(rawBody, &result); err != nil { + return "", err + } + + if len(result.Choices) == 0 { + return "", fmt.Errorf("no choices returned โ†’ request was invalid") + } + + return result.Choices[0].Message.Content, nil +} diff --git a/cmd/ai.go b/cmd/ai.go new file mode 100644 index 0000000..403dae8 --- /dev/null +++ b/cmd/ai.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" + "github.com/upsaurav12/bootstrap/api" +) + +var aiCmd = cobra.Command{ + Use: "ai", + Short: "user enters the prompt to the ai", + Long: "user enters the prompt to the ai", + Run: func(cmd *cobra.Command, args []string) { + result, err := api.CallingApiToGroq(userPrompt) + if err != nil { + fmt.Println("error: ", err) + } + + fmt.Println("result: ", result) + }, +} + +var userPrompt string + +func init() { + + rootCmd.AddCommand(&aiCmd) + + aiCmd.Flags().StringVar(&userPrompt, "prompt", " ", "user enter the prompt") +} diff --git a/cmd/init.go b/cmd/init.go new file mode 100644 index 0000000..0770321 --- /dev/null +++ b/cmd/init.go @@ -0,0 +1,9 @@ +package cmd + +import "github.com/spf13/cobra" + +var initCmd = &cobra.Command{} + +func init() { + +} diff --git a/cmd/new.go b/cmd/new.go index 4af1e67..4c5b344 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -1,6 +1,6 @@ /* -Copyright ยฉ 2025 NAME HERE +Copyright ยฉ 2025 Saurav Upadhyay sauravup041103@gmail.com */ @@ -64,9 +64,7 @@ func createNewProject(projectName string, projectRouter string, template string, fmt.Fprintf(out, "Error creating directory: %v\n", err) return } - // Print the template that was passed - // Always add README + Makefile from common renderTemplateDir("common", projectName, TemplateData{ ModuleName: projectName, PortName: projectPort, @@ -134,6 +132,8 @@ func renderTemplateDir(templatePath, destinationPath string, data TemplateData) return err } + fmt.Println("path for tmpl: ", filepath.Base(path)) + // Write file outFile, err := os.Create(targetPath) if err != nil { diff --git a/go.mod b/go.mod index 2b5bc97..abcd3d1 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,38 @@ module github.com/upsaurav12/bootstrap -go 1.23.6 +go 1.24.0 + +toolchain go1.24.6 require ( + github.com/atotto/clipboard v0.1.4 // indirect + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/bubbles v0.21.0 // indirect + github.com/charmbracelet/bubbletea v1.3.10 // indirect + github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect + github.com/charmbracelet/lipgloss v1.1.0 // indirect + github.com/charmbracelet/x/ansi v0.10.1 // indirect + github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect + github.com/charmbracelet/x/term v0.2.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/joho/godotenv v1.5.1 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/termenv v0.16.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + github.com/sahilm/fuzzy v0.1.1 // indirect github.com/spf13/cobra v1.9.1 // indirect github.com/spf13/pflag v1.0.6 // indirect github.com/stretchr/testify v1.10.0 // indirect + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/sys v0.36.0 // indirect + golang.org/x/text v0.3.8 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 725c027..b569362 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,66 @@ +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/bubbles v0.21.0 h1:9TdC97SdRVg/1aaXNVWfFH3nnLAwOXr8Fn6u6mfQdFs= +github.com/charmbracelet/bubbles v0.21.0/go.mod h1:HF+v6QUR4HkEpz62dx7ym2xc71/KBHg+zKwJtMw+qtg= +github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw= +github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4= +github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs= +github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk= +github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY= +github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30= +github.com/charmbracelet/x/ansi v0.10.1 h1:rL3Koar5XvX0pHGfovN03f5cxLbCF2YvLeyz7D2jVDQ= +github.com/charmbracelet/x/ansi v0.10.1/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE= +github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8= +github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs= +github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ= +github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc= +github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA= +github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo= github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0= github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k= +golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/yamlParsing.go b/pkg/yamlParsing.go new file mode 100644 index 0000000..759e1e3 --- /dev/null +++ b/pkg/yamlParsing.go @@ -0,0 +1,4 @@ +package pkg + +type YAML struct { +} diff --git a/templates/common/.env.tmpl b/templates/common/.env.tmpl new file mode 100644 index 0000000..6c8822a --- /dev/null +++ b/templates/common/.env.tmpl @@ -0,0 +1,9 @@ +APP_ENV=development + +# Application Port +PORT={{.PortName}} + +# PostgreSQL +{{ if .DBType == "postgres"}} +DATABASE_URL=postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable +{{- end }} diff --git a/templates/db/postgres/db.go.tmpl b/templates/db/postgres/db.go.tmpl index 3422984..a923579 100644 --- a/templates/db/postgres/db.go.tmpl +++ b/templates/db/postgres/db.go.tmpl @@ -3,25 +3,40 @@ package db import ( "database/sql" "log" + "os" + "time" _ "github.com/lib/pq" ) -var DB *sql.DB +func Connect() (*sql.DB, error) { + connStr := os.Getenv("DATABASE_URL") + if connStr == "" { + connStr = "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" + } -func Connect() { - connStr := "user=postgres password=postgres dbname=app_db sslmode=disable" - var err error - DB, err = sql.Open("postgres", connStr) + db, err := sql.Open("postgres", connStr) if err != nil { - log.Fatalf("โœ– Failed to connect to PostgreSQL: %v", err) + return nil, err } - log.Println("โœ“ Connected to PostgreSQL database") + + // Connection pooling + db.SetMaxOpenConns(25) + db.SetMaxIdleConns(10) + db.SetConnMaxLifetime(5 * time.Minute) + + // Test the actual connection + if err := db.Ping(); err != nil { + return nil, err + } + + log.Println("โœ“ Connected to PostgreSQL") + return db, nil } -func Close() { - if DB != nil { - DB.Close() +func Close(db *sql.DB) { + if db != nil { + db.Close() log.Println("๐Ÿ”Œ PostgreSQL connection closed") } } diff --git a/templates/rest/gin/cmd/main.go.tmpl b/templates/rest/gin/cmd/main.go.tmpl index f321e65..5a97641 100644 --- a/templates/rest/gin/cmd/main.go.tmpl +++ b/templates/rest/gin/cmd/main.go.tmpl @@ -16,9 +16,13 @@ import ( func main() { cfg := config.New() + // Initialize database only if selected {{- if .DBType }} - db.Connect() - defer db.Close() + database, err := db.Connect() + if err != nil { + log.Fatalf("DB init error: %v", err) + } + defer database.Close() {{- end }} r := gin.Default() @@ -29,7 +33,7 @@ func main() { port = "{{.PortName}}" } - log.Printf("๐Ÿš€ starting server on :%s", port) + log.Printf("๐Ÿš€ Starting server on :%s", port) if err := r.Run(":" + port); err != nil { log.Fatal(err) }