mirror of
https://github.com/techgarage-ir/MTWireGuard.git
synced 2025-08-29 22:29:24 +02:00
Use MinimalAPI for API back-end.
This commit is contained in:
parent
24c99bf691
commit
0dad0d1a04
6 changed files with 623 additions and 0 deletions
113
Application/MinimalAPI/ConfigurationController.cs
Normal file
113
Application/MinimalAPI/ConfigurationController.cs
Normal file
|
@ -0,0 +1,113 @@
|
|||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MTWireGuard.Application.Models.Mikrotik;
|
||||
using MTWireGuard.Application.Repositories;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using MTWireGuard.Application.Models.Models.Responses;
|
||||
using MTWireGuard.Application.Models.Requests;
|
||||
using MTWireGuard.Application.Models;
|
||||
using MikrotikAPI.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MTWireGuard.Application.MinimalAPI
|
||||
{
|
||||
internal class ConfigurationController
|
||||
{
|
||||
public static async Task<Ok<object>> Resources(
|
||||
[FromServices] IMikrotikRepository API)
|
||||
{
|
||||
var info = await API.GetInfo();
|
||||
var ramUsed = 100 - info.FreeRAMPercentage;
|
||||
var hddUsed = 100 - info.FreeHDDPercentage;
|
||||
|
||||
var output = new JsonResult(new
|
||||
{
|
||||
HDD = new
|
||||
{
|
||||
Total = info.TotalHDD,
|
||||
Used = info.UsedHDD,
|
||||
Free = info.FreeHDD,
|
||||
Percentage = Convert.ToByte(hddUsed)
|
||||
},
|
||||
RAM = new
|
||||
{
|
||||
Total = info.TotalRAM,
|
||||
Used = info.UsedRAM,
|
||||
Free = info.FreeRAM,
|
||||
Percentage = Convert.ToByte(ramUsed)
|
||||
},
|
||||
info.CPULoad,
|
||||
info.UPTime
|
||||
}).Value;
|
||||
return TypedResults.Ok(output);
|
||||
}
|
||||
|
||||
public static async Task<Results<Ok<DNS>, ProblemHttpResult>> DNS(
|
||||
[FromServices] IMikrotikRepository API)
|
||||
{
|
||||
return TypedResults.Ok(await API.GetDNS());
|
||||
}
|
||||
|
||||
public static async Task<Ok<object>> Information(
|
||||
[FromServices] IMikrotikRepository API)
|
||||
{
|
||||
var info = await API.GetInfo();
|
||||
var identity = await API.GetName();
|
||||
var dns = await API.GetDNS();
|
||||
|
||||
var dnsValues = new List<string>();
|
||||
dnsValues.AddRange(dns.Servers.Split(','));
|
||||
dnsValues.AddRange(dns.DynamicServers.Split(','));
|
||||
|
||||
var output = new JsonResult(new
|
||||
{
|
||||
Identity = identity.Name,
|
||||
DNS = dnsValues,
|
||||
Device = new
|
||||
{
|
||||
info.BoardName,
|
||||
info.Architecture
|
||||
},
|
||||
info.Version,
|
||||
IP = Environment.GetEnvironmentVariable("MT_PUBLIC_IP")
|
||||
}).Value;
|
||||
return TypedResults.Ok(output);
|
||||
}
|
||||
|
||||
public static async Task<Results<Ok<IdentityViewModel>, ProblemHttpResult>> Identity(
|
||||
[FromServices] IMikrotikRepository API)
|
||||
{
|
||||
return TypedResults.Ok(await API.GetName());
|
||||
}
|
||||
|
||||
public static async Task<Ok<ToastMessage>> IdentityUpdate(
|
||||
[FromServices] IMikrotikRepository API,
|
||||
[FromServices] IMapper mapper,
|
||||
[FromBody] UpdateIdentityRequest request)
|
||||
{
|
||||
var model = mapper.Map<IdentityUpdateModel>(request);
|
||||
var update = await API.SetName(model);
|
||||
var message = mapper.Map<ToastMessage>(update);
|
||||
return TypedResults.Ok(message);
|
||||
}
|
||||
|
||||
public static async Task<Ok<ToastMessage>> DNSUpdate(
|
||||
[FromServices] IMikrotikRepository API,
|
||||
[FromServices] IMapper mapper,
|
||||
[FromBody] UpdateDNSRequest request)
|
||||
{
|
||||
var model = mapper.Map<DNSUpdateModel>(request);
|
||||
model.Servers.Remove(string.Empty);
|
||||
model.Servers.Remove(" ");
|
||||
var update = await API.SetDNS(model);
|
||||
var message = mapper.Map<ToastMessage>(update);
|
||||
return TypedResults.Ok(message);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue