Skip to content

Commit 9cb2cb9

Browse files
committed
refactor: improve logging and error handling in configuration loading
- Replaced fmt.Println with log.Printf for better error logging. - Added logs to indicate the source of configuration (environment variables or config file). - Enhanced error handling for database connection failures, allowing graceful handling in serverless environments. - Implemented a check for database availability in the API endpoint.
1 parent 0d886e4 commit 9cb2cb9

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

api/entrypoint.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
package api
1414

1515
import (
16-
"fmt"
16+
"log"
1717
"net/http"
1818
"os"
1919
"regexp"
@@ -53,6 +53,7 @@ func getConfigFromEnvOrFile() *Set {
5353

5454
// Try to get configuration from environment variables first
5555
if dbHost := os.Getenv("CLICKHOUSE_HOST"); dbHost != "" {
56+
log.Printf("Using environment variables for configuration")
5657
set.ClickhouseConf.Host = dbHost
5758
set.ClickhouseConf.Port = getEnvOrDefault("CLICKHOUSE_PORT", "9000")
5859
set.ClickhouseConf.Username = getEnvOrDefault("CLICKHOUSE_USERNAME", "default")
@@ -64,9 +65,11 @@ func getConfigFromEnvOrFile() *Set {
6465
}
6566

6667
// Fall back to config file if environment variables are not set
68+
log.Printf("Environment variables not found, trying config file")
6769
yamlFile, err := os.ReadFile("./config.yml")
6870
if err != nil {
69-
fmt.Println("Error reading config file:", err.Error())
71+
log.Printf("Error reading config file: %v", err)
72+
log.Printf("Using default configuration values")
7073
// Return default values if both env vars and config file fail
7174
set.ClickhouseConf.Host = "localhost"
7275
set.ClickhouseConf.Port = "9000"
@@ -79,8 +82,9 @@ func getConfigFromEnvOrFile() *Set {
7982
}
8083
err = yaml.Unmarshal(yamlFile, &set)
8184
if err != nil {
82-
fmt.Println("Error parsing config file:", err.Error())
85+
log.Printf("Error parsing config file: %v", err)
8386
}
87+
log.Printf("Configuration loaded from config file")
8488
return &set
8589
}
8690

@@ -93,6 +97,7 @@ func getEnvOrDefault(key, defaultValue string) string {
9397

9498
var (
9599
app *gin.Engine
100+
db *gorm.DB
96101
)
97102

98103
func init() {
@@ -107,9 +112,13 @@ func init() {
107112

108113
// Connect Clickhouse
109114
dsn := "clickhouse://" + dbUsername + ":" + dbPassword + "@" + dbHost + ":" + dbPort + "/" + dbName
110-
db, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{})
115+
var err error
116+
db, err = gorm.Open(clickhouse.Open(dsn), &gorm.Config{})
111117
if err != nil {
112-
panic("failed to connect database")
118+
log.Printf("Failed to connect to database: %v", err)
119+
log.Printf("DSN: %s", dsn)
120+
// Don't panic in serverless environment, let the function handle errors gracefully
121+
db = nil
113122
}
114123

115124
gin.SetMode(gin.ReleaseMode)
@@ -126,6 +135,15 @@ func init() {
126135
})
127136

128137
app.Any("/wb", func(c *gin.Context) {
138+
// Check if database is available
139+
if db == nil {
140+
c.JSON(http.StatusInternalServerError, gin.H{
141+
"code": http.StatusInternalServerError,
142+
"message": "Database connection not available",
143+
})
144+
return
145+
}
146+
129147
re := regexp.MustCompile(`\d+`)
130148
u := c.Query("u")
131149
key := re.FindString(u)

0 commit comments

Comments
 (0)