Skip to content

Commit a61348b

Browse files
committed
add: db flag with options such as postgres, mysql, and so on
1 parent fc1b87e commit a61348b

File tree

8 files changed

+208
-14
lines changed

8 files changed

+208
-14
lines changed

cmd/new.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var newCmd = &cobra.Command{
4545
var projectType string
4646
var projectPort string
4747
var projectRouter string
48+
var DBType string
4849

4950
func init() {
5051
// Add the new command to the rootCmd
@@ -54,6 +55,7 @@ func init() {
5455
newCmd.Flags().StringVar(&projectType, "type", "", "type of the project")
5556
newCmd.Flags().StringVar(&projectPort, "port", "", "port of the project")
5657
newCmd.Flags().StringVar(&projectRouter, "router", "", "router of the project")
58+
newCmd.Flags().StringVar(&DBType, "db", "", "data type of the project")
5759
}
5860

5961
func createNewProject(projectName string, projectRouter string, template string, out io.Writer) {
@@ -73,19 +75,37 @@ func createNewProject(projectName string, projectRouter string, template string,
7375
renderTemplateDir("rest"+"/"+projectRouter, projectName, TemplateData{
7476
ModuleName: projectName,
7577
PortName: projectPort,
78+
DBType: DBType,
7679
})
7780

7881
if err != nil {
7982
fmt.Fprintf(out, "Error rendering templates: %v\n", err)
8083
return
8184
}
8285

83-
fmt.Fprintf(out, "Created '%s' successfully\n", projectName)
86+
if DBType != "" {
87+
dbTemplatePath := "db/" + DBType
88+
err := renderTemplateDir(dbTemplatePath, filepath.Join(projectName, "internal", "db"), TemplateData{
89+
ModuleName: projectName,
90+
PortName: projectPort,
91+
DBType: DBType,
92+
})
93+
if err != nil {
94+
95+
fmt.Fprintf(out, "Error rendering DB templates: %v\n", err)
96+
return
97+
}
98+
99+
fmt.Fprintf(out, "✓ Added database support for '%s'\n", DBType)
100+
}
101+
102+
fmt.Fprintf(out, "✓ Created '%s' successfully\n", projectName)
84103
}
85104

86105
type TemplateData struct {
87106
ModuleName string
88107
PortName string
108+
DBType string
89109
}
90110

91111
func renderTemplateDir(templatePath, destinationPath string, data TemplateData) error {

templates/common/Makefile.tmpl

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
1+
# Project variables
2+
APP_NAME := {.ModuleName}
3+
BIN_DIR := bin
4+
MAIN_FILE := ./cmd/main.go
5+
PKG := ./...
6+
7+
# Go parameters
8+
GO ?= go
9+
LINTER := golangci-lint
10+
11+
.PHONY: all run build clean lint test tidy deps help
12+
13+
all: build
14+
15+
## Run the application
116
run:
2-
go run ./cmd/server/main.go
17+
@echo ">> Running $(APP_NAME)..."
18+
@$(GO) run $(MAIN_FILE)
319

20+
## Build the binary
421
build:
5-
go build -o bin/server ./cmd/server/main.go
22+
@echo ">> Building binary..."
23+
@mkdir -p $(BIN_DIR)
24+
@$(GO) build -o $(BIN_DIR)/$(APP_NAME) $(MAIN_FILE)
25+
@echo "✅ Build complete: $(BIN_DIR)/$(APP_NAME)"
626

27+
## Clean build artifacts
28+
clean:
29+
@echo ">> Cleaning..."
30+
@rm -rf $(BIN_DIR)
31+
@$(GO) clean
32+
@echo "🧹 Clean complete"
33+
34+
## Lint the codebase
735
lint:
8-
golangci-lint run ./...
36+
@echo ">> Running linter..."
37+
@$(LINTER) run $(PKG)
938

39+
## Run unit tests with coverage
1040
test:
11-
go test ./...
41+
@echo ">> Running tests..."
42+
@$(GO) test -v -cover $(PKG)
43+
44+
## Format and tidy modules
45+
tidy:
46+
@echo ">> Tidying up..."
47+
@$(GO) fmt $(PKG)
48+
@$(GO) mod tidy
49+
50+
## Install dependencies
51+
deps:
52+
@echo ">> Installing dependencies..."
53+
@$(GO) mod download
54+
55+
## Help menu
56+
help:
57+
@echo ""
58+
@echo "Available targets:"
59+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[32m%-12s\033[0m %s\n", $$1, $$2}'
60+
@echo ""

templates/db/mongo/db.go.tmpl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package db
2+
3+
import (
4+
"context"
5+
"log"
6+
"time"
7+
8+
"go.mongodb.org/mongo-driver/mongo"
9+
"go.mongodb.org/mongo-driver/mongo/options"
10+
)
11+
12+
var Client *mongo.Client
13+
14+
func Connect() {
15+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
16+
defer cancel()
17+
18+
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
19+
if err != nil {
20+
log.Fatalf("✖ Failed to connect to MongoDB: %v", err)
21+
}
22+
23+
Client = client
24+
log.Println("✓ Connected to MongoDB")
25+
}
26+
27+
func Close() {
28+
if Client != nil {
29+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
30+
defer cancel()
31+
Client.Disconnect(ctx)
32+
log.Println("🔌 MongoDB connection closed")
33+
}
34+
}

templates/db/mysql/db.go.tmpl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package db
2+
3+
import (
4+
"database/sql"
5+
"log"
6+
7+
_ "github.com/go-sql-driver/mysql"
8+
)
9+
10+
var DB *sql.DB
11+
12+
func Connect() {
13+
connStr := "user:password@tcp(localhost:3306)/app_db"
14+
var err error
15+
DB, err = sql.Open("mysql", connStr)
16+
if err != nil {
17+
log.Fatalf("✖ Failed to connect to MySQL: %v", err)
18+
}
19+
log.Println("✓ Connected to MySQL database")
20+
}
21+
22+
func Close() {
23+
if DB != nil {
24+
DB.Close()
25+
log.Println("🔌 MySQL connection closed")
26+
}
27+
}

templates/db/postgres/db.go.tmpl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package db
2+
3+
import (
4+
"database/sql"
5+
"log"
6+
7+
_ "github.com/lib/pq"
8+
)
9+
10+
var DB *sql.DB
11+
12+
func Connect() {
13+
connStr := "user=postgres password=postgres dbname=app_db sslmode=disable"
14+
var err error
15+
DB, err = sql.Open("postgres", connStr)
16+
if err != nil {
17+
log.Fatalf("✖ Failed to connect to PostgreSQL: %v", err)
18+
}
19+
log.Println("✓ Connected to PostgreSQL database")
20+
}
21+
22+
func Close() {
23+
if DB != nil {
24+
DB.Close()
25+
log.Println("🔌 PostgreSQL connection closed")
26+
}
27+
}

templates/db/sqllite/db.go.tmpl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package db
2+
3+
import (
4+
"database/sql"
5+
"log"
6+
7+
_ "github.com/mattn/go-sqlite3"
8+
)
9+
10+
var DB *sql.DB
11+
12+
func Connect() {
13+
var err error
14+
DB, err = sql.Open("sqlite3", "app.db")
15+
if err != nil {
16+
log.Fatalf("✖ Failed to connect to SQLite: %v", err)
17+
}
18+
log.Println("✓ Connected to SQLite database")
19+
}
20+
21+
func Close() {
22+
if DB != nil {
23+
DB.Close()
24+
log.Println("🔌 SQLite connection closed")
25+
}
26+
}

templates/embed.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ import "embed"
99
//
1010
//go:embed common
1111
//go:embed rest
12+
//go:embed db
1213
var FS embed.FS

templates/rest/gin/cmd/main.go.tmpl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,35 @@ package main
22

33
import (
44
"log"
5-
"{{ .ModuleName }}/internal/router"
6-
"{{ .ModuleName }}/internal/config"
5+
6+
"{{.ModuleName}}/internal/config"
7+
"{{.ModuleName}}/internal/router"
78

89
"github.com/gin-gonic/gin"
10+
11+
{{- if .DBType }}
12+
"{{.ModuleName}}/internal/db"
13+
{{- end }}
914
)
1015

1116
func main() {
12-
// Load config
1317
cfg := config.New()
1418

15-
// Setup Gin
16-
r := gin.Default()
19+
{{- if .DBType }}
20+
db.Connect()
21+
defer db.Close()
22+
{{- end }}
1723

18-
// Register routes
24+
r := gin.Default()
1925
router.RegisterRoutes(r)
2026

21-
// Start server
22-
log.Printf("Server starting on port %s...", cfg.Port)
23-
if err := r.Run(":" + cfg.Port); err != nil {
27+
port := cfg.Port
28+
if port == "" {
29+
port = "{{.PortName}}"
30+
}
31+
32+
log.Printf("🚀 starting server on :%s", port)
33+
if err := r.Run(":" + port); err != nil {
2434
log.Fatal(err)
2535
}
2636
}

0 commit comments

Comments
 (0)