-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-base.sh
executable file
·136 lines (101 loc) · 2.38 KB
/
generate-base.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
!/bin/bash
# chmod +x generate-base.sh
# ./generate-base.sh
# ---------------- CONFIG ----------------
echo "Creating config file . . ."
mkdir "config"
cat <<EOL > "config/config.go"
package config
import (
"log"
"os"
godotenv "github.com/joho/godotenv"
)
type (
Config struct {
App App
// Db Db
// Jwt Jwt
}
App struct {
Name string
Url string
Stage string
}
)
func LoadConfig(path string) Config {
if err := godotenv.Load(path); err != nil {
log.Fatal("Error loading .env file : %s", err.Error())
}
return Config{
App: App{
Name: os.Getenv("APP_NAME"),
Url: os.Getenv("APP_URL"),
Stage: os.Getenv("APP_STAGE"),
},
}
}
EOL
# ---------------- CONFIG ----------------
# ---------------- ENV ----------------
echo "Creating ENV file . . ."
mkdir "env"
cat <<EOL > "env/.env"
APP_STAGE=dev
APP_NAME=${FILENAME}
APP_URL=:5000
EOL
# ---------------- ENV ----------------
# ---------------- SERVER ----------------
echo "Creating SERVER file . . ."
mkdir "server"
cat <<EOL > "server/server.go"
package server
type (
server struct {
app any
db any
cfg *config.Config
}
)
func (s *server) gracefulShutdown(pctx context.Context, quit <-chan os.Signal) {
log.Printf("Starting service: %s", s.cfg.App.Name)
<-quit
log.Printf("Shutting down service: %s", s.cfg.App.Name)
// depend on which library you use to shutdown the app in this case its fiber
// if err := s.app.Shutdown(); err != nil {
// log.Fatalf("Error: %v", err)
// }
}
func (s *server) httpListening() {
// base on library in this case it's fiber
// if err := s.app.Listen(":5000"); err != nil && err != http.ErrServerClosed {
// log.Fatalf("Error: %v", err)
// }
}
func Start(pctx context.Context, cfg *config.Config, db any) {
s := &server{
db: db,
cfg: cfg,
// app: fiber.New(fiber.Config{
// AppName: "testing",
// BodyLimit: 10 * 1024 * 1024,
// ReadTimeout: 10 * time.Second,
// WriteTimeout: 20 * time.Second,
// JSONEncoder: json.Marshal,
// JSONDecoder: json.Unmarshal,
//}),
app: nil,
}
// Body Limit
// app.Settings.MaxRequestBodySize = 10 * 1024 * 1024 // 10 MB
// Call the server service here
// Graceful Shutdown
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
go s.gracefulShutdown(pctx, quit)
// Listening
s.httpListening()
}
EOL
# ---------------- SERVER ----------------