mirror of
https://github.com/anotherhadi/iknowyou.git
synced 2026-04-12 00:47:26 +02:00
init
This commit is contained in:
62
back/cmd/server/main.go
Normal file
62
back/cmd/server/main.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/anotherhadi/iknowyou/config/env"
|
||||
internalapi "github.com/anotherhadi/iknowyou/internal/api"
|
||||
"github.com/anotherhadi/iknowyou/internal/registry"
|
||||
"github.com/anotherhadi/iknowyou/internal/search"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg, err := env.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("env: %v", err)
|
||||
}
|
||||
|
||||
manager := search.NewManager(cfg.ConfigPath, registry.Factories, cfg.SearchTTL, cfg.CleanupInterval)
|
||||
defer manager.Stop()
|
||||
|
||||
if cfg.Demo {
|
||||
manager.InjectDemoSearches()
|
||||
log.Println("demo mode enabled")
|
||||
}
|
||||
|
||||
router := internalapi.NewRouter(manager, registry.Factories, cfg.ConfigPath, cfg.FrontDir, cfg.Demo)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", cfg.Port),
|
||||
Handler: router,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
go func() {
|
||||
log.Printf("listening on :%d (config: %s)", cfg.Port, cfg.ConfigPath)
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("server error: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
<-ctx.Done()
|
||||
log.Println("shutting down...")
|
||||
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
log.Fatalf("graceful shutdown failed: %v", err)
|
||||
}
|
||||
|
||||
log.Println("stopped")
|
||||
}
|
||||
Reference in New Issue
Block a user