Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 101 additions & 88 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,143 @@ package main

import (
"context"
"database/sql"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"sybil-api/internal/metrics"
auth "sybil-api/internal/middleware"
"sybil-api/internal/routes/inference"
"sybil-api/internal/routes/search"
"sybil-api/internal/routes/targon"
"sybil-api/internal/setup"

"sybil-api/internal/middleware"
"sybil-api/internal/routers"
"sybil-api/internal/shared"

"github.com/aidarkhanov/nanoid"
_ "github.com/go-sql-driver/mysql"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
emw "github.com/labstack/echo/v4/middleware"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"

"github.com/manifold-inc/manifold-sdk/lib/eflag"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
core, errs := setup.CreateCore()
if errs != nil {
panic(fmt.Sprintf("Failed creating core: %s", errs))
}
defer core.Shutdown()
// Flags / ENV Variables
writeDSN := flag.String("dsn", "", "Write vitess DSN")
readDSN := flag.String("read-dsn", "", "Write vitess DSN")
metricsAPIKey := flag.String("metrics-api-key", "", "Metrics api key")
redisAddr := flag.String("redis-addr", "", "Redis host:port")
debug := flag.Bool("debug", false, "Debug enabled")

server := echo.New()
e := server.Group("")
e.Use(middleware.CORS())
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
reqID, _ := nanoid.Generate("0123456789abcdefghijklmnopqrstuvwxyz", 28)
logger := core.Log.With(
"request_id", "req_"+reqID,
)
logger = logger.With("externalid", c.Request().Header.Get("X-Dippy-Request-Id"))

cc := &setup.Context{Context: c, Log: logger, Reqid: reqID}
start := time.Now()
err := next(cc)
duration := time.Since(start)
cc.Log.Infow("end_of_request", "status_code", fmt.Sprintf("%d", cc.Response().Status), "duration", duration.String())
metrics.ResponseCodes.WithLabelValues(cc.Path(), fmt.Sprintf("%d", cc.Response().Status)).Inc()
return err
}
})
e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{
StackSize: 1 << 10, // 1 KB
LogErrorFunc: func(c echo.Context, err error, stack []byte) error {
defer func() {
_ = core.Log.Sync()
}()
core.Log.Errorw("Api Panic", "error", err.Error())
return c.String(500, shared.ErrInternalServerError.Err.Error())
},
}))
// Leaving these here, as we will need them when we re-add gsearch
//googleSearchEngineID := flag.String("google-search-engine-id", "", "Google search engine id")
//googleAPIKey := flag.String("google-api-key", "", "Google search api key")
//googleACURL := flag.String("google-ac-url", "", "Google AC URL")

e.GET(("/ping"), func(c echo.Context) error {
return c.String(200, "")
})

userManager := auth.NewUserManager(core.RedisClient, core.RDB, core.Log.With("manager", "user_manager"))
withUser := e.Group("", userManager.ExtractUser)
requiredUser := withUser.Group("", userManager.RequireUser)
err := eflag.SetFlagsFromEnvironment()
if err != nil {
panic(err)
}
flag.Parse()

inferenceGroup := requiredUser.Group("/v1")
inferenceManager, inferenceErr := inference.NewInferenceManager(core.WDB, core.RDB, core.RedisClient, core.Log, core.Debug)
if inferenceErr != nil {
panic(inferenceErr)
// Write DB init
writeDB, err := sql.Open("mysql", *writeDSN)
if err != nil {
panic(fmt.Sprintf("failed initializing sqlClient: %s", err))
}
defer inferenceManager.ShutDown()

inferenceGroup.GET("/models", inferenceManager.Models)
inferenceGroup.POST("/chat/completions", inferenceManager.ChatRequest)
inferenceGroup.POST("/completions", inferenceManager.CompletionRequest)
inferenceGroup.POST("/embeddings", inferenceManager.EmbeddingRequest)
inferenceGroup.POST("/responses", inferenceManager.ResponsesRequest)
inferenceGroup.POST("/chat/history/new", inferenceManager.CompletionRequestNewHistory)
inferenceGroup.PATCH("/chat/history/:history_id", inferenceManager.UpdateHistory)

searchGroup := requiredUser.Group("/search")
searchManager, err := search.NewSearchManager(inferenceManager.ProcessOpenaiRequest)
err = writeDB.Ping()
if err != nil {
panic(err)
panic(fmt.Sprintf("failed ping to sql db: %s", err))
}

searchGroup.POST("/images", searchManager.GetImages)
searchGroup.POST("", searchManager.Search)
searchGroup.GET("/autocomplete", searchManager.GetAutocomplete)
searchGroup.POST("/sources", searchManager.GetSources)
// Read db init
readDB, err := sql.Open("mysql", *readDSN)
if err != nil {
panic(fmt.Sprintf("failed initializing readSqlClient: %s", err))
}
err = readDB.Ping()
if err != nil {
panic(fmt.Sprintf("failed to ping read replica sql db: %s", err))
}

requiredAdmin := requiredUser.Group("", userManager.RequireAdmin)
targonGroup := requiredAdmin.Group("/models")
targonManager, targonErr := targon.NewTargonManager(core.WDB, core.RDB, core.RedisClient, core.Log)
if targonErr != nil {
panic(targonErr)
// Load Redis connection
redisClient := redis.NewClient(&redis.Options{
Addr: *redisAddr,
Password: "",
DB: 0,
})
if err := redisClient.Ping(context.Background()).Err(); err != nil {
panic(fmt.Sprintf("failed ping to redis db: %s", err))
}
targonGroup.POST("", targonManager.CreateModel)
targonGroup.DELETE("/:uid", targonManager.DeleteModel)
targonGroup.PATCH("", targonManager.UpdateModel)

metricsGroup := server.Group("/metrics")
metricsGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
defer func() {
if redisClient != nil {
_ = redisClient.Close()
}
if writeDB != nil {
_ = writeDB.Close()
}
if readDB != nil {
_ = readDB.Close()
}
}()

var logger *zap.Logger
if !*debug {
logger, err = zap.NewProduction()
if err != nil {
panic("Failed init logger")
}
}
if *debug {
logger, err = zap.NewDevelopment()
if err != nil {
panic("Failed init logger")
}
}
log := logger.Sugar()

e := echo.New()
e.GET(("/ping"), func(c echo.Context) error {
return c.String(200, "")
})
e.GET("/metrics", echo.WrapHandler(promhttp.Handler()), func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
apiKey, err := shared.ExtractAPIKey(c)
if err != nil {
return c.String(401, "Missing or invalid API key")
}

if apiKey != core.Env.MetricsAPIKey {
if apiKey != *metricsAPIKey {
return c.String(401, "Unauthorized API key")
}
return next(c)
}
})
metricsGroup.GET("", echo.WrapHandler(promhttp.Handler()))
base := e.Group("")
base.Use(emw.CORS())
base.Use(middleware.NewRecoverMiddleware(log))
base.Use(middleware.NewTrackMiddleware(log))

middleware.InitUserMiddleware(redisClient, readDB, log)

// Register routes
err = routers.RegisterAdminRoutes(base, writeDB, readDB, redisClient, log)
if err != nil {
panic(err)
}
shutdown, err := routers.RegisterInferenceRoutes(base, writeDB, readDB, redisClient, log, *debug)
if err != nil {
panic(err)
}
defer shutdown()

go func() {
if err := server.Start(":80"); err != nil && err != http.ErrServerClosed {
server.Logger.Fatal("shutting down the server")
if err := e.Start(":80"); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal("shutting down the server")
}
}()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
Expand All @@ -135,7 +148,7 @@ func main() {

ctx, cancel := context.WithTimeout(context.Background(), shared.DefaultShutdownTimeout)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
server.Logger.Fatal(err)
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module sybil-api

go 1.24.2
go 1.25.1

require (
github.com/aidarkhanov/nanoid v1.0.8
github.com/go-sql-driver/mysql v1.8.0
github.com/google/uuid v1.6.0
github.com/labstack/echo/v4 v4.11.4
github.com/manifold-inc/manifold-sdk v0.0.2
github.com/prometheus/client_golang v1.23.2
github.com/redis/go-redis/v9 v9.5.1
go.uber.org/zap v1.27.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zG
github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/manifold-inc/manifold-sdk v0.0.2 h1:wSm9L67ocMkoCpHsCgC597M9+VvE+lLykDDnyrqYDVs=
github.com/manifold-inc/manifold-sdk v0.0.2/go.mod h1:N8LYSdOvfOGzfAGzcoxDzGbWMd9bkkbfPDFftrFcSCU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down
Loading
Loading