44 lines
3.3 KiB
PowerShell
44 lines
3.3 KiB
PowerShell
param([switch]$Migrate, [switch]$InitializeAdmin, [switch]$ResetAdmin, [switch]$Start)
|
|
$ErrorActionPreference = 'Stop'
|
|
$repo = Split-Path $PSScriptRoot
|
|
$configPath = Join-Path $PSScriptRoot 'data/startup.local.json'
|
|
if (!(Test-Path -LiteralPath $configPath)) { throw 'Run Configure-Local.ps1 first.' }
|
|
$config = Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json -AsHashtable
|
|
function Set-ConfigEnvironment($node, $prefix = '') {
|
|
foreach ($key in $node.Keys) {
|
|
$name = if ($prefix) { $prefix + '__' + $key } else { $key }
|
|
$value = $node[$key]
|
|
if ($value -is [System.Collections.IDictionary]) { Set-ConfigEnvironment $value $name }
|
|
elseif ($value -is [array]) { for($i=0;$i -lt $value.Count;$i++) { [Environment]::SetEnvironmentVariable($name+'__'+$i,[string]$value[$i],'Process') } }
|
|
else { [Environment]::SetEnvironmentVariable($name,[string]$value,'Process') }
|
|
}
|
|
}
|
|
Set-ConfigEnvironment $config
|
|
$env:ASPNETCORE_ENVIRONMENT = 'Development'
|
|
$env:CONSUL_URL = 'http://127.0.0.1:18500'
|
|
$services = @(@('admin','Admin.WebApi','Admin.WebApi.dll',5180),@('user','User.WebApi','IdentityService.WebApi.dll',5181),@('contact','ContactService.WebApi','ContactService.WebApi.dll',5182),@('group','GroupService.WebApi','GroupService.WebApi.dll',5183),@('message','MessageService.WebApi','MessageService.WebApi.dll',5184),@('file','FileService.WebApi','FileService.WebApi.dll',5185),@('connector','ConnectorService','ConnectorService.dll',5186))
|
|
$logs = Join-Path $PSScriptRoot 'data/logs'
|
|
New-Item -ItemType Directory -Force -Path $logs | Out-Null
|
|
foreach ($service in $services) {
|
|
$name,$project,$dll,$port = $service
|
|
$env:Management__ServiceName = $name
|
|
$env:Kestrel__Endpoints__Http__Url = "http://127.0.0.1:$port"
|
|
$env:Kestrel__Endpoints__Http__Protocols = 'Http1'
|
|
$env:Kestrel__Endpoints__Grpc__Url = 'http://127.0.0.1:' + ($port+100)
|
|
$env:Kestrel__Endpoints__Grpc__Protocols = 'Http2'
|
|
$binary = Join-Path $repo "$project/bin/Debug/net8.0/$dll"
|
|
if (!(Test-Path -LiteralPath $binary)) { throw "Build $project before starting." }
|
|
if ($Migrate -and $name -ne 'connector') { & dotnet $binary --migrate; if ($LASTEXITCODE -ne 0) { throw "Migration failed: $name" } }
|
|
if ($name -eq 'admin' -and ($InitializeAdmin -or $ResetAdmin)) {
|
|
if (!$env:IM_ADMIN_ACCOUNT -or !$env:IM_ADMIN_PASSWORD) { throw 'Set IM_ADMIN_ACCOUNT and IM_ADMIN_PASSWORD in this process first; no default credentials are created.' }
|
|
$action = if ($InitializeAdmin) { '--init-admin' } else { '--reset-admin' }
|
|
& dotnet $binary $action; if ($LASTEXITCODE -ne 0) { throw 'Administrator initialization failed.' }
|
|
}
|
|
if ($Start) {
|
|
if (Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue) { throw "Port $port is already in use; refusing to start a duplicate $name service." }
|
|
$process = Start-Process -FilePath 'dotnet' -ArgumentList @('"'+$binary+'"') -WorkingDirectory (Split-Path $binary) -WindowStyle Hidden -PassThru -RedirectStandardOutput (Join-Path $logs "$name.out.log") -RedirectStandardError (Join-Path $logs "$name.err.log")
|
|
[IO.File]::WriteAllText((Join-Path $logs "$name.pid"),[string]$process.Id)
|
|
Write-Output "$name started on port $port (PID $($process.Id))."
|
|
}
|
|
}
|