App structure

AtlasX Web Service แบ่งโครงสร้างออกเป็นหลาย project ตาม responsibility โดยแต่ละ project ทำหน้าที่แตกต่างกัน และใช้ Shared เป็น library กลางสำหรับ feature ที่ใช้ร่วมกัน

AtlasX.Web.Service/
├── Atlasx.Administrative/     # จัดการ back-office และ admin features
├── Atlasx.Gateway/            # API Gateway สำหรับ routing และ middleware
├── Atlasx.Identity/           # Authentication และ OAuth 2.0
├── Shared/                    # Library กลางที่ใช้ร่วมกันทุก project
├── .env.example               # ตัวอย่างไฟล์ environment variables
├── .gitignore                 # กำหนดไฟล์ที่ไม่ต้อง commit เข้า Git
├── AtlasX.Web.Service.slnx    # Solution file
├── Directory.Build.props      # กำหนด properties ร่วมสำหรับทุก project
├── global.json                # กำหนด .NET SDK version
├── nuget.config               # กำหนด NuGet package sources
└── nuget.config.template      # template สำหรับ nuget.config

Projects

ProjectDescription
Atlasx.Administrativeจัดการ backoffice features เช่น การจัดการ user, role และ permission
Atlasx.Gatewayทำหน้าที่เป็น API Gateway รับ request จาก client และ route ไปยัง service ที่เกี่ยวข้อง
Atlasx.Identityจัดการ authentication และ OAuth 2.0 ครอบคลุม login, token และ session
SharedLibrary กลางที่รวม feature ที่ใช้ร่วมกัน เช่น DataAccess, Auth, Mail, Report, Session

Root Files

ไฟล์Description
.env.exampleตัวอย่าง environment variables ทั้งหมดที่จำเป็น ให้ copy เป็น .env แล้วแก้ค่าก่อนใช้งาน
.gitignoreกำหนดไฟล์ที่ไม่ต้อง commit เข้า Git เช่น .env, build output
AtlasX.Web.Service.slnxSolution file สำหรับเปิดทุก project พร้อมกันใน Visual Studio หรือ Rider
Directory.Build.propsกำหนด properties ร่วมสำหรับทุก project เช่น target framework และ package versions
global.jsonกำหนด .NET SDK version ที่ใช้ใน solution
nuget.configกำหนด NuGet package sources รวมถึง internal feed สำหรับ AtlasX packages
nuget.config.templatetemplate สำหรับสร้าง nuget.config เหมาะสำหรับ setup ครั้งแรก

Getting Started

ขั้นตอนการสร้าง AtlasX Web Service

1. สร้าง Project

ใช้ dotnet new axws template สร้าง project ตามที่ต้องการ ดูรายละเอียดเพิ่มเติมได้ที่ Create a new WebApi

dotnet new axws --name MyService

2. ตั้งค่า NuGet

สร้างไฟล์ nuget.config ที่ root ของ solution เพื่อกำหนด package sources สำหรับ AtlasX และ Telerik packages

3. ตั้งค่า .env

copy ไฟล์ .env.example เป็น .env แล้วแก้ค่าให้ตรงกับ environment ของตัวเอง

cp .env.example .env

4. ตั้งค่า Program.cs

โครงสร้างหลักของ Program.cs มีลำดับการ setup ดังนี้

ขั้นตอนDescription
1. โหลด .envโหลด environment variables จากไฟล์ .env
2. Cryptoตรวจสอบว่ารันในโหมด encrypt/decrypt หรือไม่ ถ้าใช่จะหยุดทำงานทันที
3-4. Builder & Configสร้าง builder และโหลด configuration ตาม environment และผูกค่าเข้า AppSettings
5-6. Services & CORSลงทะเบียน services พื้นฐานและกำหนด CORS policy
7-10. Feature Servicesลงทะเบียน services ตาม feature ที่ต้องการ เช่น Database, Redis, File, OAuth
// 1. โหลด .env (Development เท่านั้น)
EnvironmentUtils.Load();

// 2. ตรวจสอบโหมด encrypt/decrypt (optional)
if (await CryptoConfigurationUtils.TryProcessCryptoAsync(args))
{
    return;
}

// 3. สร้าง builder
var builder = WebApplication.CreateBuilder(args);

// 4. โหลด configuration
await builder.Configuration.AddAppSettings(builder.Environment);
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("App"));

// 5. ลงทะเบียน services
builder.Services.ConfigureWebServer();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddControllersWithViews();

// 6. CORS
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowSpecificOrigins", corsPolicyBuilder =>
    {
        var corsPolicy = builder.Configuration.GetSection("App:CorsPolicy").Get<string[]>() ?? [];
        corsPolicyBuilder.WithOrigins(corsPolicy)
            .AllowAnyHeader()
            .AllowAnyMethod()
            .AllowCredentials()
            .SetIsOriginAllowedToAllowWildcardSubdomains();
    });
});

// 7. Database
builder.Services.AddDataAccess(builder.Configuration);

// 8. Redis / Session
builder.Services.AddCache(builder.Configuration.GetSection("Redis")["ConnectionString"]);
builder.Services.AddScoped<SessionService>();

// 9. File Storage
builder.Services.AddDirectoryAccess(builder.Configuration, "App:FileServer");

// 10. OAuth / Auth
builder.Services.AddOAuth(builder.Configuration);
builder.Services.AddAuth(builder.Configuration);

// Build
var app = builder.Build();

// Middleware
if (app.Environment.IsProduction())
{
    app.UseHsts();
}
else
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseCors("AllowSpecificOrigins");
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

await app.RunAsync();