2024-02-13 20:52:10 +00:00
|
|
|
package temporal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"go.temporal.io/server/common/authorization"
|
|
|
|
"go.temporal.io/server/common/config"
|
|
|
|
"go.temporal.io/server/common/log"
|
2024-02-18 21:37:17 +00:00
|
|
|
"go.temporal.io/server/common/primitives"
|
2024-02-13 20:52:10 +00:00
|
|
|
"go.temporal.io/server/schema/sqlite"
|
|
|
|
t "go.temporal.io/server/temporal"
|
|
|
|
)
|
|
|
|
|
2024-02-18 21:37:17 +00:00
|
|
|
func NewServer(cfg *config.Config, tokenKeyProvider authorization.TokenKeyProvider) (t.Server, error) {
|
2024-02-13 20:52:10 +00:00
|
|
|
logger := log.NewZapLogger(log.BuildZapLogger(log.Config{
|
|
|
|
Stdout: true,
|
2024-06-02 19:53:51 +00:00
|
|
|
Level: "debug",
|
2024-02-13 20:52:10 +00:00
|
|
|
OutputFile: "",
|
|
|
|
}))
|
|
|
|
|
|
|
|
sqlConfig := cfg.Persistence.DataStores[PersistenceStoreName].SQL
|
|
|
|
|
|
|
|
// Apply migrations if file does not already exist
|
|
|
|
if _, err := os.Stat(sqlConfig.DatabaseName); os.IsNotExist(err) {
|
|
|
|
// Check if any of the parent dirs are missing
|
|
|
|
dir := filepath.Dir(sqlConfig.DatabaseName)
|
|
|
|
if _, err := os.Stat(dir); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sqlite.SetupSchema(sqlConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pre-create namespaces
|
|
|
|
var namespaces []*sqlite.NamespaceConfig
|
|
|
|
for _, ns := range []string{"default"} {
|
|
|
|
namespaces = append(namespaces, sqlite.NewNamespaceConfig(cfg.ClusterMetadata.CurrentClusterName, ns, false))
|
|
|
|
}
|
|
|
|
if err := sqlite.CreateNamespaces(sqlConfig, namespaces...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-18 21:37:17 +00:00
|
|
|
authorizer := authorization.NewDefaultAuthorizer()
|
|
|
|
claimMapper := authorization.NewDefaultJWTClaimMapper(tokenKeyProvider, &cfg.Global.Authorization, logger)
|
2024-02-13 20:52:10 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
interruptChan := make(chan interface{}, 1)
|
|
|
|
go func() {
|
|
|
|
if doneChan := ctx.Done(); doneChan != nil {
|
|
|
|
s := <-doneChan
|
|
|
|
interruptChan <- s
|
|
|
|
} else {
|
|
|
|
s := <-t.InterruptCh()
|
|
|
|
interruptChan <- s
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return t.NewServer(
|
|
|
|
t.WithConfig(cfg),
|
2024-02-18 21:37:17 +00:00
|
|
|
t.ForServices([]string{
|
|
|
|
string(primitives.FrontendService),
|
|
|
|
string(primitives.HistoryService),
|
|
|
|
string(primitives.MatchingService),
|
2024-02-19 14:23:17 +00:00
|
|
|
string(primitives.WorkerService),
|
|
|
|
string(primitives.InternalFrontendService),
|
2024-02-18 21:37:17 +00:00
|
|
|
}),
|
2024-02-13 20:52:10 +00:00
|
|
|
t.WithLogger(logger),
|
2024-02-18 21:37:17 +00:00
|
|
|
t.InterruptOn(interruptChan),
|
2024-02-13 20:52:10 +00:00
|
|
|
t.WithAuthorizer(authorizer),
|
|
|
|
t.WithClaimMapper(func(cfg *config.Config) authorization.ClaimMapper {
|
|
|
|
return claimMapper
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|