123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using Comal.Classes;
- using Comal.Stores;
- using GenHTTP.Api.Protocol;
- using GenHTTP.Modules.IO;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.Rpc;
- using InABox.Wpf.Reports;
- using PRS.Shared;
- using PRSServer;
- using PRSServices;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using RequestMethod = GenHTTP.Api.Protocol.RequestMethod;
- namespace PRSLicensing;
- public class LicensingHandler : Handler<LicensingHandlerProperties>
- {
- public override void Init(LicensingHandlerProperties properties)
- {
- }
- private IResponseBuilder LicenseSummary(IRequest request)
- {
- var summary = new LicenseSummary {
- LicenseFees = new() {
- { typeof(CoreLicense).EntityName(), 7.99 },
- { typeof(DigitalFormsLicense).EntityName(), 3.99 },
- { typeof(SchedulingControlLicense).EntityName(), 1.99 },
- { typeof(TimeManagementLicense).EntityName(), 2.99 },
- { typeof(AccountsPayableLicense).EntityName(), 1.99 },
- { typeof(GPSTrackerLicense).EntityName(), 2.99 },
- { typeof(LogisticsLicense).EntityName(), 4.99 },
- { typeof(ScheduleEngineLicense).EntityName(), 2.99 },
- { typeof(QuotesManagementLicense).EntityName(), 4.99 },
- { typeof(LeaveManagementLicense).EntityName(), 2.99 },
- { typeof(TaskManagementLicense).EntityName(), 1.99 },
- { typeof(WarehouseLicense).EntityName(), 5.99 },
- { typeof(ProjectManagementLicense).EntityName(), 4.99 },
- { typeof(ManufacturingLicense).EntityName(), 4.99 },
- { typeof(ProductManagementLicense).EntityName(), 2.99 },
- { typeof(EquipmentLicense).EntityName(), 2.99 },
- { typeof(HumanResourcesLicense).EntityName(), 2.99 },
- { typeof(AccountsReceivableLicense).EntityName(), 1.99 }
- },
- TimeDiscounts = new() {
- { 1, 0.00 },
- { 3, 5.00 },
- { 6, 5.00 },
- { 12, 10.00 }
- },
- UserDiscounts = new() {
- { 1, 00.31 },
- { 6, 08.63 },
- { 11, 16.94 },
- { 21, 25.25 },
- { 51, 33.57 }
- }
- };
- return request.Respond().Status(ResponseStatus.OK).Content(Serialization.Serialize(summary));
- }
- private IResponseBuilder RenewLicense(IRequest request)
- {
- return request.Respond().Status(ResponseStatus.NotFound);
- }
- private IResponseBuilder Ping(IRequest request)
- {
- return request.Respond().Status(ResponseStatus.OK);
- }
- private IResponseBuilder HandleGET(IRequest request)
- {
- var endpoint = request.Target.Current?.Value.ToLower() ?? "";
- if(endpoint == nameof(LicenseSummary).ToLower())
- {
- return LicenseSummary(request);
- }
- else if(endpoint == nameof(Ping).ToLower())
- {
- return Ping(request);
- }
- return request.Respond().Status(ResponseStatus.NotFound);
- }
- private IResponseBuilder HandlePOST(IRequest request)
- {
- var endpoint = string.Join('/', request.Target.GetRemaining());
- if(endpoint == nameof(RenewLicense).ToLower())
- {
- return RenewLicense(request);
- }
- return request.Respond().Status(ResponseStatus.NotFound);
- }
- public override ValueTask<IResponse?> HandleAsync(IRequest request)
- {
- try
- {
- switch (request.Method.KnownMethod)
- {
- case RequestMethod.GET:
- return new ValueTask<IResponse?>(HandleGET(request).Build());
- case RequestMethod.POST:
- return new ValueTask<IResponse?>(HandlePOST(request).Build());
- default:
- Logger.Send(LogType.Error, ClientFactory.UserID, $"Request method {request.Method.RawMethod} unknown");
- return new ValueTask<IResponse?>(request.Respond().Status(ResponseStatus.MethodNotAllowed).Build());
- }
- }
- catch (Exception eListen)
- {
- Logger.Send(LogType.Error, ClientFactory.UserID, eListen.Message);
- return new ValueTask<IResponse?>(request.Respond().Status(ResponseStatus.InternalServerError).Build());
- }
- }
- }
- public class LicensingHandlerProperties
- {
- }
- public class LicensingEngine : Engine<LicensingEngineProperties>
- {
- private Listener<LicensingHandler, LicensingHandlerProperties> listener;
- public override void Run()
- {
- Logger.Send(LogType.Information, "", "Starting..");
- if (string.IsNullOrWhiteSpace(Properties.Server))
- {
- Logger.Send(LogType.Error, "", "Server is blank!");
- return;
- }
- var transport = new RpcClientPipeTransport(DatabaseServerProperties.GetPipeName(Properties.Server, true));
- ClientFactory.SetClientType(typeof(RpcClient<>), Platform.WebEngine, Version, transport);
- CheckConnection();
-
- Logger.Send(LogType.Information, "", "Registering Classes");
- StoreUtils.RegisterClasses();
- CoreUtils.RegisterClasses();
- ComalUtils.RegisterClasses();
- PRSSharedUtils.RegisterClasses();
- ReportUtils.RegisterClasses();
- ConfigurationUtils.RegisterClasses();
- Logger.Send(LogType.Information, "", "Starting Listener on port " + Properties.ListenPort);
- try
- {
- listener = new Listener<LicensingHandler, LicensingHandlerProperties>(new LicensingHandlerProperties());
- listener.InitHTTPS((ushort)Properties.ListenPort, CertificateFileName());
- Logger.Send(LogType.Information, "", "Starting Web Listener on port " + Properties.ListenPort);
- listener.Start();
- }
- catch (Exception eListen)
- {
- Logger.Send(LogType.Error, ClientFactory.UserID, eListen.Message);
- }
- }
- private string CertificateFileName() => Properties.CertificateFile;
- private void CheckConnection()
- {
- // Wait for server connection
- while (!Client.Ping())
- {
- Logger.Send(LogType.Error, "", "Database server unavailable. Trying again in 30 seconds...");
- Task.Delay(30_000).Wait();
- Logger.Send(LogType.Information, "", "Retrying connection...");
- }
- ClientFactory.SetBypass();
- }
- public override void Stop()
- {
- Logger.Send(LogType.Information, "", "Stopping");
- listener?.Stop();
- }
- }
- public class LicensingEngineProperties : ServerProperties
- {
- [ComboLookupEditor(typeof(LicensingDatabaseServerLookupGenerator))]
- [EditorSequence(1)]
- public string Server { get; set; }
- [IntegerEditor]
- [EditorSequence(2)]
- public int ListenPort { get; set; }
- [EditorSequence(3)]
- [FileNameEditor("Certificate Files (*.pfx)|*.pfx")]
- public string CertificateFile { get; set; }
- public override ServerType Type()
- {
- return ServerType.Other;
- }
- }
- public class LicensingDatabaseServerLookupGenerator : LookupGenerator<LicensingEngineProperties>
- {
- public LicensingDatabaseServerLookupGenerator(LicensingEngineProperties[] items) : base(items)
- {
- }
- protected override void DoGenerateLookups()
- {
- var config = new LocalConfiguration<ServerSettings>(CoreUtils.GetCommonAppData("PRSServer"), "");
- var servers = config.LoadAll();
- foreach (var server in servers.Select(x => x.Value.CreateServer(x.Key)))
- {
- if (server.Type == ServerType.Database)
- {
- AddValue(server.Key, server.Name);
- }
- }
- }
- }
|