257 lines
8.1 KiB
Go
257 lines
8.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"droboticflow/internal/ai"
|
|
"droboticflow/internal/api"
|
|
"droboticflow/internal/config"
|
|
"droboticflow/internal/retry"
|
|
"droboticflow/internal/store"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func connectMongo(ctx context.Context, mongoURI string, attempts int, delay time.Duration) (*mongo.Client, error) {
|
|
return retry.Do(ctx, attempts, delay, "mongodb connection", func(ctx context.Context, _ int) (*mongo.Client, error) {
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoURI).SetMonitor(store.ReadCommandMonitor()))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := client.Ping(ctx, nil); err != nil {
|
|
_ = client.Disconnect(ctx)
|
|
return nil, err
|
|
}
|
|
return client, nil
|
|
})
|
|
}
|
|
|
|
func spaHandler(apiHandler http.Handler, distDir string) http.Handler {
|
|
fileServer := http.FileServer(http.Dir(distDir))
|
|
indexPath := filepath.Join(distDir, "index.html")
|
|
serveIndex := func(writer http.ResponseWriter, request *http.Request) {
|
|
writer.Header().Set("Cache-Control", "no-store")
|
|
writer.Header().Set("Pragma", "no-cache")
|
|
writer.Header().Set("Expires", "0")
|
|
http.ServeFile(writer, request, indexPath)
|
|
}
|
|
|
|
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
if strings.HasPrefix(request.URL.Path, "/api/") || strings.HasPrefix(request.URL.Path, "/worker-api/") {
|
|
apiHandler.ServeHTTP(writer, request)
|
|
return
|
|
}
|
|
if request.URL.Path == "/" {
|
|
serveIndex(writer, request)
|
|
return
|
|
}
|
|
targetPath := filepath.Join(distDir, filepath.Clean(request.URL.Path))
|
|
// /assets/ files are content-hashed and /vendor/ files live in
|
|
// version-pinned directories, so both cache immutably and must 404 on
|
|
// miss instead of falling back to the SPA index: a module import that
|
|
// receives index.html fails with a confusing syntax error.
|
|
assetLike := strings.HasPrefix(request.URL.Path, "/assets/") || strings.HasPrefix(request.URL.Path, "/vendor/")
|
|
if _, err := os.Stat(targetPath); err == nil {
|
|
if assetLike {
|
|
writer.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
|
}
|
|
if servePrecompressedAsset(writer, request, targetPath) {
|
|
return
|
|
}
|
|
fileServer.ServeHTTP(writer, request)
|
|
return
|
|
}
|
|
if assetLike {
|
|
writer.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0")
|
|
http.NotFound(writer, request)
|
|
return
|
|
}
|
|
serveIndex(writer, request)
|
|
})
|
|
}
|
|
|
|
var precompressedContentTypes = map[string]string{
|
|
".js": "text/javascript; charset=utf-8",
|
|
".css": "text/css; charset=utf-8",
|
|
".wasm": "application/wasm",
|
|
}
|
|
|
|
func requestAcceptsGzip(request *http.Request) bool {
|
|
wildcard := false
|
|
for _, encoding := range request.Header.Values("Accept-Encoding") {
|
|
for _, part := range strings.Split(encoding, ",") {
|
|
fields := strings.Split(part, ";")
|
|
name := strings.ToLower(strings.TrimSpace(fields[0]))
|
|
quality := 1.0
|
|
for _, parameter := range fields[1:] {
|
|
pair := strings.SplitN(strings.TrimSpace(parameter), "=", 2)
|
|
if len(pair) == 2 && strings.EqualFold(pair[0], "q") {
|
|
value, err := strconv.ParseFloat(pair[1], 64)
|
|
if err != nil || value < 0 || value > 1 {
|
|
quality = 0
|
|
} else {
|
|
quality = value
|
|
}
|
|
}
|
|
}
|
|
if name == "gzip" {
|
|
return quality > 0
|
|
}
|
|
if name == "*" {
|
|
wildcard = quality > 0
|
|
}
|
|
}
|
|
}
|
|
return wildcard
|
|
}
|
|
|
|
// servePrecompressedAsset streams the build-time .gz sibling for compressible
|
|
// static assets when the client accepts gzip. It reports whether it handled
|
|
// the request; callers fall back to the plain file server otherwise. Range
|
|
// requests always fall back so byte ranges stay valid against the raw asset.
|
|
func servePrecompressedAsset(writer http.ResponseWriter, request *http.Request, targetPath string) bool {
|
|
contentType, compressible := precompressedContentTypes[filepath.Ext(targetPath)]
|
|
if !compressible {
|
|
return false
|
|
}
|
|
// Keep shared caches from mixing gzip and identity responses.
|
|
writer.Header().Add("Vary", "Accept-Encoding")
|
|
if !requestAcceptsGzip(request) || request.Header.Get("Range") != "" {
|
|
return false
|
|
}
|
|
if request.Method != http.MethodGet && request.Method != http.MethodHead {
|
|
return false
|
|
}
|
|
file, err := os.Open(targetPath + ".gz")
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer func() { _ = file.Close() }()
|
|
info, err := file.Stat()
|
|
if err != nil || info.IsDir() {
|
|
return false
|
|
}
|
|
writer.Header().Set("Content-Type", contentType)
|
|
writer.Header().Set("Content-Encoding", "gzip")
|
|
writer.Header().Set("Content-Length", strconv.FormatInt(info.Size(), 10))
|
|
writer.Header().Set("Accept-Ranges", "none")
|
|
if request.Method == http.MethodHead {
|
|
return true
|
|
}
|
|
writer.WriteHeader(http.StatusOK)
|
|
_, _ = io.Copy(writer, file)
|
|
return true
|
|
}
|
|
|
|
func main() {
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
cfg, err := config.LoadValidated()
|
|
if err != nil {
|
|
fmt.Printf("[api] invalid configuration: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
client, err := connectMongo(ctx, cfg.MongoURI, cfg.MongoConnectRetries, time.Duration(cfg.MongoConnectRetryDelayMS)*time.Millisecond)
|
|
if err != nil {
|
|
fmt.Printf("[api] failed to connect mongo: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer func() {
|
|
_ = client.Disconnect(context.Background())
|
|
}()
|
|
|
|
dataStore := store.NewMongoStore(client.Database(cfg.MongoDB))
|
|
if os.Getenv("ANNOTATION_PROCESSOR_MODE") == "processor" {
|
|
checkIndexes := dataStore.EnsureIndexes
|
|
mode := os.Getenv("DROBOTICFLOW_DATABASE_BOOTSTRAP")
|
|
if mode != "" && mode != "migrate" {
|
|
checkIndexes = func(ctx context.Context) error { return bootstrapAPI(ctx, dataStore, mode) }
|
|
}
|
|
if err := checkIndexes(ctx); err != nil {
|
|
fmt.Printf("[annotation-processor] indexes: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
parallel := 2
|
|
if value, err := strconv.Atoi(os.Getenv("ANNOTATION_PROCESSOR_CONCURRENCY")); err == nil {
|
|
parallel = value
|
|
}
|
|
health := &http.Server{Addr: "0.0.0.0:3003", ReadHeaderTimeout: 5 * time.Second, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/ready" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if e := dataStore.Ready(r.Context()); e != nil {
|
|
http.Error(w, "store unavailable", 503)
|
|
return
|
|
}
|
|
w.WriteHeader(200)
|
|
_, _ = w.Write([]byte("annotation processor ready"))
|
|
})}
|
|
go func() {
|
|
if e := health.ListenAndServe(); e != nil && e != http.ErrServerClosed {
|
|
stop()
|
|
}
|
|
}()
|
|
fmt.Println("[annotation-processor] durable worker and index precompute started")
|
|
err := api.RunAnnotationProcessor(ctx, dataStore, cfg, parallel, os.Getenv("ANNOTATION_INDEX_BACKFILL") == "1")
|
|
shutdown, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
_ = health.Shutdown(shutdown)
|
|
if err != nil && ctx.Err() == nil {
|
|
fmt.Printf("[annotation-processor] failed: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
}
|
|
if err := bootstrapAPI(ctx, dataStore, os.Getenv("DROBOTICFLOW_DATABASE_BOOTSTRAP")); err != nil {
|
|
fmt.Printf("[api] database bootstrap: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
aiService := ai.NewService(cfg.AIAPIBaseURL, cfg.AIAPIKey, cfg.AIModel)
|
|
handler := spaHandler(api.NewHandlerWithConfig(dataStore, aiService, cfg), cfg.DistDir)
|
|
server := &http.Server{
|
|
Addr: fmt.Sprintf("0.0.0.0:%d", cfg.Port),
|
|
Handler: handler,
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
ReadTimeout: 30 * time.Second,
|
|
WriteTimeout: 2 * time.Minute,
|
|
IdleTimeout: 2 * time.Minute,
|
|
}
|
|
|
|
fmt.Printf("[api] listening on http://0.0.0.0:%d\n", cfg.Port)
|
|
serverErrors := make(chan error, 1)
|
|
go func() {
|
|
serverErrors <- server.ListenAndServe()
|
|
}()
|
|
|
|
select {
|
|
case err := <-serverErrors:
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
fmt.Printf("[api] failed to start: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
case <-ctx.Done():
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
fmt.Println("[api] shutting down")
|
|
if err := server.Shutdown(shutdownCtx); err != nil {
|
|
fmt.Printf("[api] graceful shutdown failed: %v\n", err)
|
|
_ = server.Close()
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|