51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
namespace MycroForge.CLI.Features;
|
|
|
|
public sealed class Api : IFeature
|
|
{
|
|
#region Main
|
|
|
|
private static readonly string[] HelloRouter =
|
|
[
|
|
"from fastapi import APIRouter",
|
|
"from fastapi.responses import JSONResponse",
|
|
"from fastapi.encoders import jsonable_encoder",
|
|
"",
|
|
"router = APIRouter()",
|
|
"",
|
|
"@router.get(\"/{name}\")",
|
|
"async def greet(name: str):",
|
|
"\treturn JSONResponse(status_code=200, content=jsonable_encoder({'greeting': f\"Hello, {name}!\"}))"
|
|
];
|
|
|
|
private static readonly string[] Main =
|
|
[
|
|
"from fastapi import FastAPI",
|
|
"app = FastAPI()",
|
|
"",
|
|
"from api.routers import hello",
|
|
"app.include_router(prefix=\"/hello\", router=hello.router)"
|
|
];
|
|
|
|
#endregion
|
|
|
|
public const string FeatureName = "api";
|
|
|
|
public string Name => FeatureName;
|
|
|
|
public async Task ExecuteAsync(ProjectContext context)
|
|
{
|
|
if (context.Config.Features.Contains(FeatureName))
|
|
{
|
|
Console.WriteLine($"Feature {FeatureName} has already been initialized.");
|
|
return;
|
|
}
|
|
|
|
await context.CreateFile("api/routers/hello.py", HelloRouter);
|
|
|
|
var main = await context.ReadFile("main.py");
|
|
main = string.Join('\n', Main) + main;
|
|
await context.WriteFile("main.py", main);
|
|
|
|
context.Config.Features.Add(FeatureName);
|
|
}
|
|
} |