Skip to content

Commit b38471f

Browse files
author
Vic Shóstak
authored
Merge pull request #13 from create-go-app/dev
Update ENV; Add conn URL builder
2 parents 0e82fa7 + e1f516f commit b38471f

File tree

6 files changed

+78
-20
lines changed

6 files changed

+78
-20
lines changed

.env.example

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# Server settings:
2-
SERVER_URL="0.0.0.0:5000"
3-
SERVER_EMAIL="no-reply@example.com"
4-
SERVER_EMAIL_PASSWORD="secret"
2+
SERVER_HOST="0.0.0.0"
3+
SERVER_PORT=5000
4+
SERVER_READ_TIMEOUT=60
55

66
# JWT settings:
77
JWT_SECRET_KEY="secret"
88
JWT_REFRESH_KEY="refresh"
99

1010
# Database settings:
11-
DB_SERVER_URL="host=localhost port=5432 user=postgres password=password dbname=postgres sslmode=disable"
11+
DB_HOST="localhost"
12+
DB_PORT=5432
13+
DB_USER="postgres"
14+
DB_PASSWORD="password"
15+
DB_NAME="postgres"
16+
DB_SSL_MODE="disable"
1217
DB_MAX_CONNECTIONS=100
1318
DB_MAX_IDLE_CONNECTIONS=10
14-
DB_MAX_LIFETIME_CONNECTIONS=2
15-
16-
# SMTP severs settings:
17-
SMTP_SERVER="smtp.example.com"
18-
SMTP_PORT=25
19+
DB_MAX_LIFETIME_CONNECTIONS=2

pkg/configs/server_config.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@ package configs
33
import (
44
"net/http"
55
"os"
6+
"strconv"
67
"time"
78

9+
"github.com/create-go-app/net_http-go-template/pkg/utils"
810
"github.com/gorilla/mux"
911
)
1012

1113
// ServerConfig func for configuration net/http app.
1214
func ServerConfig(router *mux.Router) *http.Server {
15+
// Define server settings:
16+
serverConnURL, _ := utils.ConnectionURLBuilder("server")
17+
readTimeoutSecondsCount, _ := strconv.Atoi(os.Getenv("SERVER_READ_TIMEOUT"))
18+
19+
// Return server configuration.
1320
return &http.Server{
14-
Handler: router,
15-
Addr: os.Getenv("SERVER_URL"),
16-
WriteTimeout: 60 * time.Second,
17-
ReadTimeout: 60 * time.Second,
21+
Handler: router,
22+
Addr: serverConnURL,
23+
ReadTimeout: time.Second * time.Duration(readTimeoutSecondsCount),
1824
}
1925
}

pkg/routes/swagger_routes.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ package routes
22

33
import (
44
"net/http"
5-
"os"
65

6+
"github.com/create-go-app/net_http-go-template/pkg/utils"
77
"github.com/gorilla/mux"
88

99
httpSwagger "github.com/swaggo/http-swagger"
1010
)
1111

1212
// SwaggerRoutes func for describe group of Swagger routes.
1313
func SwaggerRoutes(router *mux.Router) {
14+
// Define server settings:
15+
serverConnURL, _ := utils.ConnectionURLBuilder("server")
16+
1417
// Build Swagger route.
1518
getSwagger := httpSwagger.Handler(
16-
httpSwagger.URL("http://"+os.Getenv("SERVER_URL")+"/swagger/doc.json"),
19+
httpSwagger.URL("http://"+serverConnURL+"/swagger/doc.json"),
1720
httpSwagger.DeepLinking(true),
1821
httpSwagger.DocExpansion("none"),
1922
httpSwagger.DomID("#swagger-ui"),

pkg/utils/connection_url_builder.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
// ConnectionURLBuilder func for building URL connection.
9+
func ConnectionURLBuilder(n string) (string, error) {
10+
// Define URL to connection.
11+
var url string
12+
13+
// Switch given names.
14+
switch n {
15+
case "postgres":
16+
// URL for PostgreSQL connection.
17+
url = fmt.Sprintf(
18+
"host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
19+
os.Getenv("DB_HOST"),
20+
os.Getenv("DB_PORT"),
21+
os.Getenv("DB_USER"),
22+
os.Getenv("DB_PASSWORD"),
23+
os.Getenv("DB_NAME"),
24+
os.Getenv("DB_SSL_MODE"),
25+
)
26+
case "server":
27+
// URL for server connection.
28+
url = fmt.Sprintf(
29+
"%s:%s",
30+
os.Getenv("SERVER_HOST"),
31+
os.Getenv("SERVER_PORT"),
32+
)
33+
default:
34+
// Return error message.
35+
return "", fmt.Errorf("connection name '%v' is not supported", n)
36+
}
37+
38+
// Return connection URL.
39+
return url, nil
40+
}

pkg/utils/start_server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func StartServerWithGracefulShutdown(server *http.Server) {
1616

1717
// Run our server in a goroutine so that it doesn't block.
1818
go func() {
19+
log.Println("Server is starting...")
1920
if err := server.ListenAndServe(); err != nil {
2021
log.Println(err)
2122
}
@@ -43,6 +44,6 @@ func StartServerWithGracefulShutdown(server *http.Server) {
4344
// Optionally, you could run srv.Shutdown in a goroutine and block on
4445
// <-ctx.Done() if your application should wait for other services
4546
// to finalize based on context cancellation.
46-
log.Println("shutting down")
47+
log.Println("Server is shutting down...")
4748
os.Exit(0)
4849
}

platform/database/postgres.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77
"time"
88

9+
"github.com/create-go-app/net_http-go-template/pkg/utils"
910
"github.com/jmoiron/sqlx"
1011

1112
_ "github.com/jackc/pgx/v4/stdlib" // load pgx driver for PostgreSQL
@@ -18,21 +19,27 @@ func PostgreSQLConnection() (*sqlx.DB, error) {
1819
maxIdleConn, _ := strconv.Atoi(os.Getenv("DB_MAX_IDLE_CONNECTIONS"))
1920
maxLifetimeConn, _ := strconv.Atoi(os.Getenv("DB_MAX_LIFETIME_CONNECTIONS"))
2021

22+
// Build PostgreSQL connection URL.
23+
postgresConnURL, err := utils.ConnectionURLBuilder("postgres")
24+
if err != nil {
25+
return nil, err
26+
}
27+
2128
// Define database connection for PostgreSQL.
22-
db, err := sqlx.Connect("pgx", os.Getenv("DB_SERVER_URL"))
29+
db, err := sqlx.Connect("pgx", postgresConnURL)
2330
if err != nil {
24-
return nil, fmt.Errorf("error, not connected to PostgreSQL database, %w", err)
31+
return nil, fmt.Errorf("error, not connected to database, %w", err)
2532
}
2633

27-
// Set database connection settings.
34+
// Set database connection settings:
2835
db.SetMaxOpenConns(maxConn) // the default is 0 (unlimited)
2936
db.SetMaxIdleConns(maxIdleConn) // defaultMaxIdleConns = 2
3037
db.SetConnMaxLifetime(time.Duration(maxLifetimeConn)) // 0, connections are reused forever
3138

3239
// Try to ping database.
3340
if err := db.Ping(); err != nil {
3441
defer db.Close() // close database connection
35-
return nil, fmt.Errorf("error, not sent ping to PostgreSQL database, %w", err)
42+
return nil, fmt.Errorf("error, not sent ping to database, %w", err)
3643
}
3744

3845
return db, nil

0 commit comments

Comments
 (0)