You've already forked CsharpTemplate
Add project files.
This commit is contained in:
14
APITemplate/APITemplate.csproj
Normal file
14
APITemplate/APITemplate.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MySqlConnector" Version="2.2.7" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
25
APITemplate/APITemplate.sln
Normal file
25
APITemplate/APITemplate.sln
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.7.34031.279
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "APITemplate", "APITemplate.csproj", "{EEF75926-CF27-4C86-9827-55D8605EF328}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{EEF75926-CF27-4C86-9827-55D8605EF328}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{EEF75926-CF27-4C86-9827-55D8605EF328}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{EEF75926-CF27-4C86-9827-55D8605EF328}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{EEF75926-CF27-4C86-9827-55D8605EF328}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {4E1FFA94-449A-40F3-AE62-A2D1AC80634A}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
8
APITemplate/Contexts/IDBContext.cs
Normal file
8
APITemplate/Contexts/IDBContext.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace APITemplate.Contexts
|
||||
{
|
||||
public interface IDBContext
|
||||
{
|
||||
Object GetWriteConn();
|
||||
Object GetReadConn();
|
||||
}
|
||||
}
|
31
APITemplate/Contexts/MysqlDBContext.cs
Normal file
31
APITemplate/Contexts/MysqlDBContext.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using MySqlConnector;
|
||||
|
||||
namespace APITemplate.Contexts
|
||||
{
|
||||
public class MysqlDBContext : IDBContext
|
||||
{
|
||||
private IConfiguration _config;
|
||||
private MySqlConnection _connection;
|
||||
private string _connectionStringRead;
|
||||
private string _connectionStringWrite;
|
||||
|
||||
public MysqlDBContext(IConfiguration configuration)
|
||||
{
|
||||
_config = configuration;
|
||||
_connectionStringRead = _config.GetConnectionString("ReadDB").ToString();
|
||||
_connectionStringWrite = _config.GetConnectionString("WriteDB").ToString();
|
||||
}
|
||||
|
||||
public Object GetReadConn()
|
||||
{
|
||||
_connection = new MySqlConnection(_connectionStringWrite);
|
||||
return _connection;
|
||||
}
|
||||
|
||||
public Object GetWriteConn()
|
||||
{
|
||||
_connection = new MySqlConnection(_connectionStringRead);
|
||||
return _connection;
|
||||
}
|
||||
}
|
||||
}
|
32
APITemplate/Controllers/ExampleController.cs
Normal file
32
APITemplate/Controllers/ExampleController.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using APITemplate.Models;
|
||||
using APITemplate.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace APITemplate.Controllers
|
||||
{
|
||||
public class ExampleController : Controller
|
||||
{
|
||||
private ExampleService _svc;
|
||||
public ExampleController(ExampleService svc)
|
||||
{
|
||||
_svc = svc;
|
||||
}
|
||||
|
||||
// POST: ExampleController/Create
|
||||
[HttpPost]
|
||||
[ValidateAntiForgeryToken]
|
||||
public ActionResult Create(Example e)
|
||||
{
|
||||
try
|
||||
{
|
||||
_svc.Insert(e);
|
||||
return Created("",e);
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
7
APITemplate/Models/Example.cs
Normal file
7
APITemplate/Models/Example.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace APITemplate.Models
|
||||
{
|
||||
public class Example
|
||||
{
|
||||
public string ExampleProp { get; set; }
|
||||
}
|
||||
}
|
44
APITemplate/Program.cs
Normal file
44
APITemplate/Program.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using APITemplate.Contexts;
|
||||
using APITemplate.Repositories;
|
||||
using APITemplate.Services;
|
||||
|
||||
namespace APITemplate
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddSingleton<IDBContext, MysqlDBContext>();
|
||||
builder.Services.AddScoped<IExampleRepository, ExampleRepository>();
|
||||
builder.Services.AddScoped<ExampleService>();
|
||||
builder.Services.AddScoped<LogService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
}
|
31
APITemplate/Properties/launchSettings.json
Normal file
31
APITemplate/Properties/launchSettings.json
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:58714",
|
||||
"sslPort": 44336
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"APITemplate": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7069;http://localhost:5230",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
12
APITemplate/Repositories/ExampleRepository.cs
Normal file
12
APITemplate/Repositories/ExampleRepository.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using APITemplate.Models;
|
||||
|
||||
namespace APITemplate.Repositories
|
||||
{
|
||||
public class ExampleRepository : IExampleRepository
|
||||
{
|
||||
public void Insert(Example e)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
9
APITemplate/Repositories/IExampleRepository.cs
Normal file
9
APITemplate/Repositories/IExampleRepository.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using APITemplate.Models;
|
||||
|
||||
namespace APITemplate.Repositories
|
||||
{
|
||||
public interface IExampleRepository
|
||||
{
|
||||
void Insert(Example e);
|
||||
}
|
||||
}
|
21
APITemplate/Services/ExampleService.cs
Normal file
21
APITemplate/Services/ExampleService.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using APITemplate.Models;
|
||||
using APITemplate.Repositories;
|
||||
|
||||
namespace APITemplate.Services
|
||||
{
|
||||
public class ExampleService
|
||||
{
|
||||
private IExampleRepository _repo;
|
||||
private LogService _log;
|
||||
public ExampleService(IExampleRepository repo, LogService log)
|
||||
{
|
||||
_log = log;
|
||||
_repo = repo;
|
||||
}
|
||||
|
||||
public void Insert(Example example)
|
||||
{
|
||||
_repo.Insert(example);
|
||||
}
|
||||
}
|
||||
}
|
260
APITemplate/Services/LogService.cs
Normal file
260
APITemplate/Services/LogService.cs
Normal file
@ -0,0 +1,260 @@
|
||||
using APITemplate.Contexts;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace APITemplate.Services
|
||||
{
|
||||
public class LogService
|
||||
{
|
||||
private LogType[] _logtype;
|
||||
private IDBContext _db;
|
||||
public LogService(IConfiguration config, IDBContext dbcontext)
|
||||
{
|
||||
_db = dbcontext;
|
||||
_logtype = config.GetSection("Logger").Get<LogType[]>();
|
||||
}
|
||||
|
||||
public LogService(LogType[] logtype)
|
||||
{
|
||||
_logtype = logtype;
|
||||
}
|
||||
|
||||
public void Debug(string s, string trace = "", string cmd = "")
|
||||
{
|
||||
WriteLog("Debug", s, "", trace, cmd);
|
||||
}
|
||||
|
||||
public void Debug(string s, string toko, string trace = "", string cmd = "")
|
||||
{
|
||||
WriteLog("Debug", s, toko, trace, cmd);
|
||||
}
|
||||
|
||||
public void Info(string s, string trace = "", string cmd = "")
|
||||
{
|
||||
WriteLog("Info", s, "", trace, cmd);
|
||||
}
|
||||
|
||||
public void Info(string s, string toko, string trace = "", string cmd = "")
|
||||
{
|
||||
WriteLog("Info", s, toko, trace, cmd);
|
||||
}
|
||||
|
||||
public void Warning(string s, string trace = "", string cmd = "")
|
||||
{
|
||||
WriteLog("Warn", s, "", trace, cmd);
|
||||
}
|
||||
|
||||
public void Warning(string s, string toko, string trace = "", string cmd = "")
|
||||
{
|
||||
WriteLog("Warn", s, toko, trace, cmd);
|
||||
}
|
||||
|
||||
public void Error(string s, string trace = "", string cmd = "")
|
||||
{
|
||||
WriteLog("Error", s, "", trace, cmd);
|
||||
}
|
||||
|
||||
public void Error(string s, string toko, string trace = "", string cmd = "")
|
||||
{
|
||||
WriteLog("Error", s, toko, trace, cmd);
|
||||
}
|
||||
|
||||
private void WriteLog(string tipe, string s, string toko, string trace = "", string cmd = "")
|
||||
{
|
||||
foreach (LogType l in _logtype)
|
||||
{
|
||||
if (
|
||||
(l.MinimumLevel.ToLower() == "error" && tipe.ToLower() == "error") ||
|
||||
(l.MinimumLevel.ToLower() == "warn" && (tipe.ToLower() == "warn" || tipe.ToLower() == "warn")) ||
|
||||
(l.MinimumLevel.ToLower() == "info" && tipe.ToLower() != "debug") ||
|
||||
(l.MinimumLevel.ToLower() == "debug")
|
||||
)
|
||||
{
|
||||
switch (l.Name.ToLower())
|
||||
{
|
||||
case "console":
|
||||
ConsoleLog(s, toko, tipe, trace);
|
||||
break;
|
||||
|
||||
case "text":
|
||||
TextLog(l.Path, s, toko, tipe, trace, cmd);
|
||||
break;
|
||||
|
||||
case "json":
|
||||
JsonLog(l.Path, s, toko, tipe, trace, cmd);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static void ConsoleLog(string s, string toko, string tipe, string trace = "")
|
||||
{
|
||||
Console.Write($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} ");
|
||||
if (tipe.ToLower() == "info")
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Green;
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.Write($"[INFO ]");
|
||||
Console.ResetColor();
|
||||
if (toko == "")
|
||||
Console.WriteLine($" {s}");
|
||||
else
|
||||
Console.WriteLine($" ({toko}) {s}");
|
||||
}
|
||||
else if (tipe.ToLower() == "warn")
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Yellow;
|
||||
Console.ForegroundColor = ConsoleColor.Black;
|
||||
Console.Write($"[WARN ]");
|
||||
Console.ResetColor();
|
||||
if (toko == "")
|
||||
Console.WriteLine($" {s}");
|
||||
else
|
||||
Console.WriteLine($" ({toko}) {s}");
|
||||
}
|
||||
else if (tipe.ToLower() == "error")
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Red;
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.Write($"[ERROR]");
|
||||
Console.ResetColor();
|
||||
if (toko == "")
|
||||
Console.WriteLine($" {s}");
|
||||
else
|
||||
Console.WriteLine($" ({toko}) {s}");
|
||||
if (trace != "")
|
||||
Console.WriteLine(Environment.NewLine + trace);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"[DEBUG] {s}");
|
||||
}
|
||||
}
|
||||
|
||||
private static void TextLog(string path, string s, string toko, string level, string trace = "", string cmd = "")
|
||||
{
|
||||
if (path is null)
|
||||
{
|
||||
path = Environment.CurrentDirectory + "\\logs";
|
||||
}
|
||||
|
||||
string filename;
|
||||
if (toko == "")
|
||||
{
|
||||
filename = @$"{path}\log-{DateTime.Now:MMM-yyyy}.log";
|
||||
}
|
||||
else
|
||||
{
|
||||
filename = @$"{path}\toko\{toko} log-{DateTime.Now:MMM-yyyy}.log";
|
||||
}
|
||||
|
||||
bool written = false;
|
||||
|
||||
Directory.CreateDirectory(path);
|
||||
Directory.CreateDirectory(path + "\\toko");
|
||||
|
||||
s = System.Text.RegularExpressions.Regex.Replace(s, @"\p{C}+", ">").Replace(Environment.NewLine, ">");
|
||||
trace = System.Text.RegularExpressions.Regex.Replace(trace, @"\p{C}+", ">").Replace(Environment.NewLine, ">");
|
||||
cmd = System.Text.RegularExpressions.Regex.Replace(cmd, @"\p{C}+", ">").Replace(Environment.NewLine, ">");
|
||||
|
||||
level = level.PadRight(5).ToUpper();
|
||||
|
||||
while (!written)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (StreamWriter sw = System.IO.File.AppendText(filename))
|
||||
{
|
||||
sw.WriteLine(@$"{DateTime.Now:dd-MM-yy HH:mm:ss}|[{level}]|{s}|{trace}|{cmd}");
|
||||
}
|
||||
written = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void DBLog(string s, string toko, string level, string trace = "", string cmd = "")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private static void JsonLog(string path, string s, string toko, string level, string trace = "", string cmd = "")
|
||||
{
|
||||
if (path is null)
|
||||
{
|
||||
path = Environment.CurrentDirectory + "\\logs";
|
||||
}
|
||||
|
||||
string filename;
|
||||
if (toko == "")
|
||||
{
|
||||
filename = @$"{path}\log-{DateTime.Now:MMM-yyyy}.json";
|
||||
}
|
||||
else
|
||||
{
|
||||
filename = @$"{path}\toko\{toko} log-{DateTime.Now:MMM-yyyy}.json";
|
||||
}
|
||||
bool written = false;
|
||||
|
||||
Directory.CreateDirectory(path);
|
||||
Directory.CreateDirectory(path + "\\toko");
|
||||
|
||||
while (!written)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<JsonLog> loglist;
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
string jsondata = System.IO.File.ReadAllText(filename);
|
||||
loglist = JsonSerializer.Deserialize<List<JsonLog>>(jsondata);
|
||||
}
|
||||
else
|
||||
{
|
||||
loglist = new List<JsonLog>();
|
||||
}
|
||||
|
||||
loglist.Add(new JsonLog()
|
||||
{
|
||||
Time = DateTime.Now,
|
||||
Mesage = s,
|
||||
Toko = toko,
|
||||
Level = level,
|
||||
Trace = trace,
|
||||
Cmd = cmd,
|
||||
});
|
||||
File.WriteAllText(filename, JsonSerializer.Serialize(loglist));
|
||||
written = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class LogType
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string MinimumLevel { get; set; }
|
||||
public string Path { get; set; }
|
||||
}
|
||||
|
||||
public class JsonLog
|
||||
{
|
||||
public DateTime Time { get; set; }
|
||||
public string Toko { get; set; }
|
||||
public string Level { get; set; }
|
||||
public string Mesage { get; set; }
|
||||
public string Trace { get; set; }
|
||||
public string Cmd { get; set; }
|
||||
}
|
||||
}
|
8
APITemplate/appsettings.Development.json
Normal file
8
APITemplate/appsettings.Development.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
17
APITemplate/appsettings.json
Normal file
17
APITemplate/appsettings.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"Logger": [
|
||||
{
|
||||
"Name": "Console",
|
||||
"MinimumLevel": "Info"
|
||||
},
|
||||
{
|
||||
"Name": "Text",
|
||||
"MinimumLevel": "Debug"
|
||||
}
|
||||
],
|
||||
"ConnectionStrings": {
|
||||
"ReadDB": "server=localhost;port=3306;user id=root; password=root1234;Persist Security Info=True;pooling=false;connection timeout=500;allow user variables=True;Allow Zero DateTime=True;Convert Zero Datetime=True;SslMode:none",
|
||||
"WriteDB": "server=localhost;port=3306;user id=root; password=root1234;Persist Security Info=True;pooling=false;connection timeout=500;allow user variables=True;Allow Zero DateTime=True;Convert Zero Datetime=True;SslMode:none"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
Reference in New Issue
Block a user