91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
nethttp "net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
"trankilou.fr/lassistanoque/backend/internal/gateway"
|
|
"trankilou.fr/lassistanoque/backend/internal/http"
|
|
)
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(
|
|
newServeCmd(),
|
|
)
|
|
}
|
|
|
|
func newServeCmd() *cobra.Command {
|
|
var short bool
|
|
cmd := &cobra.Command{
|
|
Use: "serve",
|
|
Short: "Run server",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
runServe()
|
|
},
|
|
}
|
|
cmd.Flags().BoolVarP(&short, "short", "s", false, "show only version number")
|
|
return cmd
|
|
}
|
|
|
|
func runServe() {
|
|
|
|
defer Close()
|
|
|
|
// http server
|
|
httpRouter := http.NewRouter(http.Dependencies{
|
|
StorageService: GetStorageService(),
|
|
AuthService: GetAuthService(),
|
|
UserService: GetUserService(),
|
|
ProviderService: GetProviderService(),
|
|
AgentService: GetAgentService(),
|
|
ChatService: GetChatService(),
|
|
ToolService: GetToolService(),
|
|
TokenManager: GetTokenManager(),
|
|
})
|
|
|
|
gatewayRouter := gateway.NewRouter(gateway.Dependencies{})
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
// start Brain router
|
|
go func() {
|
|
if err := gatewayRouter.Start(); err != nil && !errors.Is(err, nethttp.ErrServerClosed) {
|
|
slog.Error("failed to start llm server", "error", err)
|
|
}
|
|
}()
|
|
|
|
// start HTTP router
|
|
go func() {
|
|
if err := httpRouter.Start(); err != nil && !errors.Is(err, nethttp.ErrServerClosed) {
|
|
slog.Error("failed to start http server", "error", err)
|
|
}
|
|
}()
|
|
|
|
<-ctx.Done()
|
|
|
|
// stop HTTP service
|
|
ctx, cancel1 := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel1()
|
|
if err := httpRouter.Stop(ctx); err != nil {
|
|
slog.Error("failed to stop http server", "error", err)
|
|
}
|
|
slog.Info("HTTP server stopped gracefully")
|
|
|
|
// stop LLM service
|
|
ctx, cancel2 := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel2()
|
|
if err := gatewayRouter.Stop(ctx); err != nil {
|
|
slog.Error("failed to stop llm server", "error", err)
|
|
}
|
|
slog.Info("LLM server stopped gracefully")
|
|
|
|
}
|