71 lines
1.8 KiB
C#
71 lines
1.8 KiB
C#
|
using HashidsNet;
|
|||
|
using Newtonsoft.Json;
|
|||
|
|
|||
|
namespace UrlShortener;
|
|||
|
public class Config
|
|||
|
{
|
|||
|
public int Port {get;set;}=5555;
|
|||
|
public string HashIdsSalt {get;set;}="";
|
|||
|
|
|||
|
public string UrlRoot {get;set;}="";
|
|||
|
}
|
|||
|
class Program
|
|||
|
{
|
|||
|
|
|||
|
static void Main(string[] args)
|
|||
|
{
|
|||
|
Config config = LoadConfig();
|
|||
|
IHashids ids = new Hashids(config.HashIdsSalt,11);
|
|||
|
UrlShortenerApp app=new UrlShortenerApp(config,ids);
|
|||
|
app.Run();
|
|||
|
}
|
|||
|
|
|||
|
static Config LoadConfig()
|
|||
|
{
|
|||
|
Config? config=null;
|
|||
|
Directory.CreateDirectory("data");
|
|||
|
string configFile = Path.Combine("data","config.json");
|
|||
|
if(File.Exists(configFile))
|
|||
|
{
|
|||
|
config=JsonConvert.DeserializeObject<Config>(File.ReadAllText(configFile));
|
|||
|
if(config == null)
|
|||
|
{
|
|||
|
File.Delete(configFile);
|
|||
|
return LoadConfig();
|
|||
|
}
|
|||
|
}else{
|
|||
|
|
|||
|
config = new Config();
|
|||
|
|
|||
|
Console.Write("Enter Port [default: 5555]: ");
|
|||
|
var read = Console.ReadLine();
|
|||
|
if(!string.IsNullOrWhiteSpace(read) && int.TryParse(read,out var port))
|
|||
|
{
|
|||
|
config.Port = port;
|
|||
|
}
|
|||
|
read = "";
|
|||
|
|
|||
|
|
|||
|
while(string.IsNullOrWhiteSpace(read))
|
|||
|
{
|
|||
|
Console.Write("Enter Url Root: ");
|
|||
|
read = Console.ReadLine();
|
|||
|
}
|
|||
|
|
|||
|
config.UrlRoot = read;
|
|||
|
|
|||
|
Console.Write("Enter Hashids.net salt: ");
|
|||
|
read = Console.ReadLine();
|
|||
|
if(string.IsNullOrWhiteSpace(read))
|
|||
|
{
|
|||
|
read = Convert.ToBase64String(System.Security.Cryptography.RandomNumberGenerator.GetBytes(32));
|
|||
|
}
|
|||
|
config.HashIdsSalt = read;
|
|||
|
File.WriteAllText(configFile,JsonConvert.SerializeObject(config));
|
|||
|
|
|||
|
}
|
|||
|
if(config == null) return new Config();
|
|||
|
return config;
|
|||
|
}
|
|||
|
}
|