Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2020 Robert Bosch GmbH |
| 3 | * Author: Constantin Ziesche (constantin.ziesche@bosch.com) |
| 4 | * |
| 5 | * This program and the accompanying materials are made available under the |
| 6 | * terms of the Eclipse Public License 2.0 which is available at |
| 7 | * http://www.eclipse.org/legal/epl-2.0 |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | *******************************************************************************/ |
| 11 | using BaSyx.Utils.Settings.Types; |
| 12 | using Microsoft.AspNetCore.Builder; |
| 13 | using Microsoft.AspNetCore.Hosting; |
| 14 | using Microsoft.Extensions.DependencyInjection; |
| 15 | using Microsoft.Extensions.Logging; |
| 16 | using NLog; |
| 17 | using NLog.Web; |
| 18 | using System; |
| 19 | using System.IO; |
| 20 | using System.Linq; |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame] | 21 | using System.Reflection; |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 22 | using System.Threading; |
| 23 | using System.Threading.Tasks; |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame] | 24 | using static BaSyx.Utils.Settings.Types.ServerSettings; |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 25 | |
| 26 | namespace BaSyx.Components.Common |
| 27 | { |
| 28 | public abstract class ServerApplication : IServerApplicationLifetime |
| 29 | { |
| 30 | private static readonly Logger logger = NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger(); |
| 31 | |
| 32 | public ServerSettings Settings { get; protected set; } |
| 33 | public IWebHostBuilder WebHostBuilder { get; protected set; } |
| 34 | |
| 35 | public Action ApplicationStarted { get; set; } |
| 36 | |
| 37 | public Action ApplicationStopping { get; set; } |
| 38 | |
| 39 | public Action ApplicationStopped { get; set; } |
| 40 | |
| 41 | protected ServerApplication(Type startupType) : this(startupType, ServerSettings.LoadSettings() ?? throw new NullReferenceException("ServerSettings.xml not found"), null) |
| 42 | { } |
| 43 | |
| 44 | protected ServerApplication(Type startupType, ServerSettings settings) : this(startupType, settings, null) |
| 45 | { } |
| 46 | |
| 47 | protected ServerApplication(Type startupType, ServerSettings settings, string[] webHostBuilderArgs) |
| 48 | { |
| 49 | Settings = settings ?? throw new ArgumentNullException(nameof(settings)); |
| 50 | WebHostBuilder = DefaultWebHostBuilder.CreateWebHostBuilder(webHostBuilderArgs, Settings, startupType); |
| 51 | WebHostBuilder.ConfigureServices(services => |
| 52 | { |
| 53 | services.AddSingleton(typeof(ServerSettings), Settings); |
| 54 | services.AddSingleton<IServerApplicationLifetime>(this); |
| 55 | }) |
| 56 | .UseNLog(); |
| 57 | ConfigureLogging(logger.GetLogLevel()); |
| 58 | } |
| 59 | |
| 60 | public virtual void Run() |
| 61 | { |
| 62 | logger.Debug("Starting Server..."); |
| 63 | |
| 64 | WebHostBuilder.Build().Run(); |
| 65 | } |
| 66 | |
| 67 | public virtual async Task RunAsync(CancellationToken cancellationToken = default) |
| 68 | { |
| 69 | logger.Debug("Starting Server Async..."); |
| 70 | |
| 71 | await WebHostBuilder.Build().RunAsync(cancellationToken); |
| 72 | } |
| 73 | |
| 74 | public virtual void ConfigureLogging(Microsoft.Extensions.Logging.LogLevel logLevel) |
| 75 | { |
| 76 | WebHostBuilder.ConfigureLogging(logging => |
| 77 | { |
| 78 | logging.ClearProviders(); |
| 79 | logging.SetMinimumLevel(logLevel); |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | public virtual void ConfigureApplication(Action<IApplicationBuilder> appConfiguration) => WebHostBuilder.Configure(appConfiguration); |
| 84 | public virtual void ConfigureServices(Action<IServiceCollection> configureServices) => WebHostBuilder.ConfigureServices(configureServices); |
| 85 | public virtual void UseContentRoot(string contentRoot) => WebHostBuilder.UseContentRoot(contentRoot); |
| 86 | public virtual void UseWebRoot(string webRoot) => WebHostBuilder.UseWebRoot(webRoot); |
| 87 | public virtual void UseUrls(params string[] urls) |
| 88 | { |
| 89 | WebHostBuilder.UseUrls(urls); |
| 90 | if (Settings?.ServerConfig?.Hosting != null) |
| 91 | Settings.ServerConfig.Hosting.Urls = urls?.ToList(); |
| 92 | } |
| 93 | |
| 94 | public virtual void ProvideContent(Uri relativeUri, Stream content) |
| 95 | { |
| 96 | try |
| 97 | { |
| 98 | using (Stream stream = content) |
| 99 | { |
| 100 | string fileName = Path.GetFileName(relativeUri.ToString()); |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame^] | 101 | logger.Info("FileName: " + fileName); |
| 102 | string directory = Path.GetDirectoryName(relativeUri.ToString()).TrimStart('\\'); |
| 103 | logger.Info("Directory: " + directory); |
| 104 | |
| 105 | logger.Info("Content Path from Settings: " + Settings.ServerConfig.Hosting.ContentPath); |
| 106 | logger.Info("Current Working Directory: " + Directory.GetCurrentDirectory()); |
| 107 | string hostingDirectory = Path.Join(Directory.GetCurrentDirectory(), Settings.ServerConfig.Hosting.ContentPath, directory); |
| 108 | |
| 109 | logger.Info($"Try creating hosting directory if not existing: {hostingDirectory}"); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 110 | Directory.CreateDirectory(hostingDirectory); |
| 111 | |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame^] | 112 | string filePath = Path.Join(hostingDirectory, fileName); |
| 113 | logger.Info($"Try writing file: {filePath}"); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 114 | |
| 115 | using (FileStream fileStream = File.OpenWrite(filePath)) |
| 116 | { |
| 117 | stream.CopyTo(fileStream); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | catch (Exception e) |
| 122 | { |
| 123 | logger.Error(e, $"Error providing content {relativeUri}"); |
| 124 | } |
| 125 | } |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame] | 126 | |
| 127 | public virtual void MapControllers(ControllerConfiguration controllerConfig) |
| 128 | { |
| 129 | this.ConfigureServices(services => |
| 130 | { |
| 131 | if (controllerConfig?.Controllers?.Count > 0) |
| 132 | { |
| 133 | var mvcBuilder = services.AddMvc(); |
| 134 | foreach (var controllerAssemblyName in controllerConfig.Controllers) |
| 135 | { |
| 136 | Assembly controllerAssembly = Assembly.Load(controllerAssemblyName); |
| 137 | mvcBuilder.AddApplicationPart(controllerAssembly); |
| 138 | } |
| 139 | mvcBuilder.AddControllersAsServices(); |
| 140 | } |
| 141 | }); |
| 142 | } |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 143 | } |
| 144 | } |