start logging -- also clarified env checks
This commit is contained in:
33
main.go
33
main.go
@@ -1,22 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"git.dubyatp.xyz/chat-api-server/api"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func checkEnvVars(keys []string) (bool, []string) {
|
||||
var missing []string
|
||||
for _, key := range keys {
|
||||
if _, ok := os.LookupEnv(key); !ok {
|
||||
missing = append(missing, key)
|
||||
}
|
||||
}
|
||||
return len(missing) == 0, missing
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Start logger
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
requiredEnvVars := []string{"SCYLLA_CLUSTER", "SCYLLA_KEYSPACE"}
|
||||
|
||||
// Initialize dotenv file
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
// Check if environment variables are defined by OS before erroring out
|
||||
_, exists := os.LookupEnv("SCYLLA_CLUSTER")
|
||||
if !exists {
|
||||
log.Fatal("Required environment variables are not added, and no .env file was found")
|
||||
}
|
||||
logger.Info("No .env file loaded, will try OS environment variables")
|
||||
}
|
||||
|
||||
// Check if environment variables were defined by the OS before erroring out
|
||||
exists, missingVars := checkEnvVars(requiredEnvVars)
|
||||
if !exists {
|
||||
slog.Error("Missing environment variables", "missing", missingVars)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
slog.Info("Starting the API Server...")
|
||||
api.Start()
|
||||
}
|
||||
|
Reference in New Issue
Block a user