2025-03-05 13:50:43 +00:00
// This API currently manages all user entities to be managed. This allows users of the service to change required entities and processes.
//
// The entities in question:
//
2025-03-10 11:03:00 +00:00
// - Client - A client is a single client of the Doczy project, all configurations and clients for a client will be in reference to the respective client.
2025-03-05 13:50:43 +00:00
//
2026-01-14 17:59:04 +00:00
// - Collector - A collector specifies document processing settings for a given client.
2025-03-05 13:50:43 +00:00
//
2025-03-10 11:03:00 +00:00
// - Sample - A sample is a subset of documents to be used for testing. When it is active a client will only process documents in the sample.
2025-03-05 13:50:43 +00:00
//
2025-03-10 11:03:00 +00:00
// - Export - An export is a manually started process which outputs the results for a client to a given location.
2026-01-14 17:59:04 +00:00
//
// Note: Query functionality has been removed from this API. See remove_query_plan.md for details.
2024-12-18 18:54:48 +00:00
package main
import (
"context"
2025-01-31 13:43:55 +00:00
"fmt"
"log/slog"
"os"
2025-03-05 12:05:46 +00:00
2026-03-04 23:00:50 +00:00
queryapi "queryorchestration/api/queryAPI"
2025-08-20 19:01:13 +00:00
"queryorchestration/internal/backgroundtask"
2025-01-21 18:24:14 +00:00
"queryorchestration/internal/client"
2026-03-04 23:00:50 +00:00
clientupdate "queryorchestration/internal/client/update"
2025-07-11 19:27:14 +00:00
"queryorchestration/internal/cognitoauth"
2025-03-10 11:03:00 +00:00
"queryorchestration/internal/collector"
2026-03-04 23:00:50 +00:00
collectorset "queryorchestration/internal/collector/set"
2025-01-31 13:43:55 +00:00
"queryorchestration/internal/document"
2026-03-04 23:00:50 +00:00
documentbatch "queryorchestration/internal/document/batch"
2026-03-17 17:06:57 +00:00
documentdownload "queryorchestration/internal/document/download"
2026-03-04 23:00:50 +00:00
documentupload "queryorchestration/internal/document/upload"
2026-01-22 18:17:27 +00:00
"queryorchestration/internal/eula"
2024-12-24 17:13:48 +00:00
"queryorchestration/internal/export"
2025-11-26 19:23:42 +00:00
"queryorchestration/internal/fieldextraction"
"queryorchestration/internal/folder"
"queryorchestration/internal/label"
2025-03-17 18:14:15 +00:00
"queryorchestration/internal/server/api"
2025-05-27 15:28:46 +00:00
"queryorchestration/internal/serviceconfig"
2026-02-26 12:33:35 +00:00
awsc "queryorchestration/internal/serviceconfig/aws"
2025-09-18 21:06:21 +00:00
"queryorchestration/internal/serviceconfig/build"
2025-05-27 15:28:46 +00:00
"queryorchestration/internal/serviceconfig/objectstore"
2025-03-10 11:03:00 +00:00
"queryorchestration/internal/serviceconfig/queue/clientsync"
2026-03-04 23:00:50 +00:00
"queryorchestration/internal/uisettings"
2026-02-26 12:33:35 +00:00
"queryorchestration/internal/usermanagement"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider"
2025-01-31 13:43:55 +00:00
"github.com/getkin/kin-openapi/openapi3"
2024-12-18 18:54:48 +00:00
_ "github.com/lib/pq"
)
2025-03-17 18:14:15 +00:00
type QueryAPIConfig struct {
api . BaseConfig
2025-03-10 11:03:00 +00:00
clientsync . ClientSyncConfig
2025-05-27 15:28:46 +00:00
objectstore . ObjectStoreConfig
2025-08-20 19:01:13 +00:00
BackgroundRunner * backgroundtask . Runner
2025-02-12 19:00:25 +00:00
}
2024-12-18 18:54:48 +00:00
func main ( ) {
2025-09-18 21:06:21 +00:00
// Print version information before any environment checks
build . PrintVersionInfo ( "queryAPI" )
2024-12-18 18:54:48 +00:00
ctx := context . Background ( )
2025-03-17 18:14:15 +00:00
cfg := & QueryAPIConfig { }
2025-01-31 13:43:55 +00:00
cfg . RegisterHandlersFunc = func ( ) ( * openapi3 . T , error ) {
exp := export . New ( )
2025-03-10 13:00:57 +00:00
col := collector . New ( cfg )
colupdate := collectorset . New ( cfg , & collectorset . Services {
2025-03-11 18:15:49 +00:00
Collector : col ,
2025-01-23 14:56:20 +00:00
} )
2025-03-10 13:00:57 +00:00
cli := client . New ( cfg )
2025-04-22 19:57:35 +00:00
cliUpdate := clientupdate . New ( cfg , & clientupdate . Services {
Client : cli ,
} )
2025-02-12 19:00:25 +00:00
doc := document . New ( cfg )
2025-05-27 15:28:46 +00:00
docup := documentupload . New ( cfg )
2026-03-17 17:06:57 +00:00
docdownload := documentdownload . New ( cfg )
2025-08-05 07:03:35 -07:00
docbatch := documentbatch . New ( cfg )
2025-11-26 19:23:42 +00:00
fieldext := fieldextraction . New ( cfg )
fld := folder . New ( cfg )
lbl := label . New ( cfg )
2026-01-22 18:17:27 +00:00
eul := eula . New ( cfg )
2026-03-04 23:00:50 +00:00
uiSvc := uisettings . New ( cfg )
2025-11-26 19:23:42 +00:00
2025-03-17 18:14:15 +00:00
services := & queryapi . Services {
2026-03-17 17:06:57 +00:00
Export : exp ,
Collector : col ,
CollectorSet : colupdate ,
Client : cli ,
ClientUpdate : cliUpdate ,
Document : doc ,
DocumentUpload : docup ,
DocumentDownload : docdownload ,
DocumentBatch : docbatch ,
UISettings : uiSvc ,
FieldExtraction : fieldext ,
Folder : fld ,
Label : lbl ,
Eula : eul ,
2025-01-15 19:45:51 +00:00
}
2025-07-11 19:27:14 +00:00
cons := queryapi . NewControllers ( services , cfg )
2025-01-15 19:45:51 +00:00
2025-03-17 18:14:15 +00:00
queryapi . RegisterHandlers ( cfg . Router , cons )
2025-01-15 19:45:51 +00:00
2025-03-17 18:14:15 +00:00
swagger , err := queryapi . GetSwagger ( )
2025-01-15 19:45:51 +00:00
if err != nil {
2025-06-23 15:58:20 +00:00
return nil , fmt . Errorf ( "error loading swagger: %w" , err )
2025-01-15 19:45:51 +00:00
}
2025-01-31 13:43:55 +00:00
return swagger , nil
2024-12-18 18:54:48 +00:00
}
2025-04-17 15:59:14 -07:00
// Both of these operations (InitializeConfig and InitializeAuthConfig)
// would be better off in one function but since there are services
// that need the config but do not use auth we will keep them separate for now.
2025-03-18 16:28:00 -07:00
// This must be done before the rbac.InitializeAuthProvider
2025-03-18 15:52:58 -07:00
errInitializingConfig := serviceconfig . InitializeConfig ( cfg )
if errInitializingConfig != nil {
slog . Error ( errInitializingConfig . Error ( ) )
os . Exit ( 1 )
}
2025-04-11 16:46:05 -07:00
// Authentication specific config.
2025-04-17 15:59:14 -07:00
cfg . BaseURL = fmt . Sprintf ( "%s:%d" , cfg . BaseURL , cfg . Port )
errorInitializingAuthConfig := cfg . InitializeAuthConfig ( cfg . BaseURL , cfg . GetLogger ( ) )
fmt . Printf ( "base url after InitializeAuthConfig: %s\n" , cfg . BaseURL )
2025-04-11 16:46:05 -07:00
// join initErr with errorInitializingAuthConfig
if errorInitializingAuthConfig != nil {
slog . Error ( errorInitializingAuthConfig . Error ( ) )
os . Exit ( 1 )
}
2025-03-18 15:52:58 -07:00
2025-07-11 19:27:14 +00:00
// Validate Permit.io configuration before starting server
if err := cognitoauth . ValidatePermitIOConfiguration ( ) ; err != nil {
slog . Error ( "Permit.io configuration validation failed" , "error" , err )
os . Exit ( 1 )
}
2026-02-26 12:33:35 +00:00
// Create Cognito SDK client for environment attribute registration and middleware use.
// This client is created once and reused for the lifetime of the process.
awsCfg , err := awsc . GetAWSConfig ( ctx )
if err != nil {
slog . Error ( "Failed to load AWS config for Cognito client" , "error" , err )
os . Exit ( 1 )
}
cognitoClient := cognitoidentityprovider . NewFromConfig ( awsCfg )
// Ensure the custom:environment_id attribute is registered on the Cognito user pool.
// This is safe to call on every startup (idempotent). Failure is non-fatal because
// the attribute may already exist or the environment may not support this API (e.g. LocalStack).
if err := usermanagement . EnsureEnvironmentIDAttribute ( ctx , cognitoClient , cfg . GetAuthUserPoolID ( ) , cfg . GetLogger ( ) ) ; err != nil {
cfg . GetLogger ( ) . Warn ( "Failed to ensure Cognito environment_id attribute" , "error" , err )
} else {
cfg . GetLogger ( ) . Info ( "Cognito custom attribute custom:environment_id is registered" )
}
2025-08-20 19:01:13 +00:00
// Initialize S3 client before creating the server (needed for background worker)
2026-02-26 12:33:35 +00:00
err = cfg . SetStoreClient ( ctx )
2025-01-31 13:43:55 +00:00
if err != nil {
slog . Error ( err . Error ( ) )
os . Exit ( 1 )
}
2024-12-18 18:54:48 +00:00
2025-08-20 19:01:13 +00:00
server , err := api . New ( ctx , cfg )
2025-05-27 15:28:46 +00:00
if err != nil {
slog . Error ( err . Error ( ) )
os . Exit ( 1 )
}
2025-01-10 11:12:03 +00:00
server . Listen ( )
2024-12-18 18:54:48 +00:00
}