messaging-center/cmd/mc/main.go

72 lines
1.6 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/inou-ai/messaging-center/internal/api"
"github.com/inou-ai/messaging-center/internal/core"
"github.com/inou-ai/messaging-center/internal/store"
)
func main() {
configPath := flag.String("config", "config.yaml", "Path to config file")
flag.Parse()
// Load configuration
cfg, err := core.LoadConfig(*configPath)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
// Ensure storage directory exists
if err := os.MkdirAll(cfg.Storage.Path, 0755); err != nil {
log.Fatalf("Failed to create storage directory: %v", err)
}
// Ensure database directory exists
dbDir := filepath.Dir(cfg.Database.Path)
if dbDir != "." && dbDir != "" {
if err := os.MkdirAll(dbDir, 0755); err != nil {
log.Fatalf("Failed to create database directory: %v", err)
}
}
// Initialize store
store, err := store.New(cfg.Database.Path)
if err != nil {
log.Fatalf("Failed to initialize store: %v", err)
}
defer store.Close()
// Create API server
srv := api.NewServer(cfg, store)
// Start HTTP server
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
httpServer := &http.Server{
Addr: addr,
Handler: srv,
}
// Handle shutdown
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGTERM)
go func() {
log.Printf("Starting messaging-center on %s", addr)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("HTTP server error: %v", err)
}
}()
<-done
log.Println("Shutting down...")
}