update environment configuration for local development, enhance database initialization, and improve Nginx routing for admin access
This commit is contained in:
1 parent
2e061dff28
commit
1ae0ad121b
10 files changed
+185
-37
No files matched your search
+46
-12
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -56,11 +57,25 @@ type Presence struct {
|
||||
|
||||
// initRedis initializes Redis for presence, rate limiting, and pub/sub (ADR-007)
|
||||
func initRedis() {
|
||||
redisClient = redis.NewClient(&redis.Options{
|
||||
Addr: os.Getenv("REDIS_URL"),
|
||||
Password: "",
|
||||
DB: 0,
|
||||
})
|
||||
redisAddress := os.Getenv("REDIS_URL")
|
||||
if redisAddress == "" {
|
||||
redisAddress = "redis:6379"
|
||||
}
|
||||
|
||||
var options *redis.Options
|
||||
var err error
|
||||
if strings.Contains(redisAddress, "://") {
|
||||
options, err = redis.ParseURL(redisAddress)
|
||||
if err != nil {
|
||||
log.Printf("Redis configuration warning: %v (rate limiting and presence will be disabled)", err)
|
||||
redisClient = nil
|
||||
return
|
||||
}
|
||||
} else {
|
||||
options = &redis.Options{Addr: redisAddress}
|
||||
}
|
||||
|
||||
redisClient = redis.NewClient(options)
|
||||
if err := redisClient.Ping(context.Background()).Err(); err != nil {
|
||||
log.Printf("Redis connection warning: %v (rate limiting and presence will be disabled)", err)
|
||||
redisClient = nil
|
||||
@@ -72,12 +87,31 @@ func initRedis() {
|
||||
// initDB initializes SQLite for message history and moderation audit log (ADR-006)
|
||||
func initDB() {
|
||||
var err error
|
||||
db, err = gorm.Open(sqlite.Open("chat.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
log.Fatal("Failed to connect to SQLite:", err)
|
||||
databaseURL := strings.TrimSpace(os.Getenv("DATABASE_URL"))
|
||||
driverName := "sqlite"
|
||||
|
||||
switch {
|
||||
case databaseURL == "":
|
||||
db, err = gorm.Open(sqlite.Open("chat.db"), &gorm.Config{})
|
||||
case strings.HasPrefix(databaseURL, "sqlite://"):
|
||||
sqlitePath := strings.TrimPrefix(databaseURL, "sqlite://")
|
||||
if sqlitePath == "" {
|
||||
sqlitePath = "chat.db"
|
||||
}
|
||||
db, err = gorm.Open(sqlite.Open(sqlitePath), &gorm.Config{})
|
||||
case strings.HasPrefix(databaseURL, "postgres://") || strings.HasPrefix(databaseURL, "postgresql://"):
|
||||
driverName = "postgres"
|
||||
db, err = gorm.Open(postgres.Open(databaseURL), &gorm.Config{})
|
||||
default:
|
||||
log.Fatalf("Unsupported DATABASE_URL scheme: %s", databaseURL)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to %s database: %v", driverName, err)
|
||||
}
|
||||
|
||||
db.AutoMigrate(&Message{}, &ModerationLog{})
|
||||
log.Println("SQLite database initialized (chat history and moderation log)")
|
||||
log.Printf("%s database initialized (chat history and moderation log)", strings.Title(driverName))
|
||||
}
|
||||
|
||||
// broadcast sends a message to all connected clients in a room and saves to DB
|
||||
@@ -110,7 +144,7 @@ func handleConnection(w http.ResponseWriter, r *http.Request) {
|
||||
// Announce presence (ADR-007)
|
||||
username := r.URL.Query().Get("username")
|
||||
room := r.URL.Query().Get("room")
|
||||
if username != "" && room != "" {
|
||||
if redisClient != nil && username != "" && room != "" {
|
||||
redisClient.Set(context.Background(), fmt.Sprintf("presence:%s:%s", room, username), "online", 60*time.Second)
|
||||
broadcast(Message{Room: room, Username: "system", Text: username + " joined " + room, Type: "system"})
|
||||
}
|
||||
@@ -119,7 +153,7 @@ func handleConnection(w http.ResponseWriter, r *http.Request) {
|
||||
mutex.Lock()
|
||||
delete(clients, conn)
|
||||
mutex.Unlock()
|
||||
if username != "" && room != "" {
|
||||
if redisClient != nil && username != "" && room != "" {
|
||||
redisClient.Del(context.Background(), fmt.Sprintf("presence:%s:%s", room, username))
|
||||
broadcast(Message{Room: room, Username: "system", Text: username + " left " + room, Type: "system"})
|
||||
}
|
||||
@@ -230,6 +264,6 @@ func main() {
|
||||
port = "3000"
|
||||
}
|
||||
|
||||
log.Printf("Chat Backend starting on :%s (WebSocket at /ws, health at /health, SQLite persistence enabled)", port)
|
||||
log.Printf("Chat Backend starting on :%s (WebSocket at /ws, health at /health, database persistence enabled)", port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||
}
|
||||
Reference in new issue
Block a user