blob: 055b9753ac8510344a21c8af0a70b018eff52254 [file] [log] [blame]
Constantin Zieschefa612082020-04-03 09:54:56 +02001/*******************************************************************************
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*******************************************************************************/
11using BaSyx.Utils.Settings.Types;
12using Microsoft.AspNetCore.Builder;
13using Microsoft.AspNetCore.Hosting;
14using Microsoft.Extensions.DependencyInjection;
15using Microsoft.Extensions.Logging;
16using NLog;
17using NLog.Web;
18using System;
19using System.IO;
20using System.Linq;
Constantin Ziesche02817f12020-08-04 21:40:43 +020021using System.Reflection;
Constantin Zieschefa612082020-04-03 09:54:56 +020022using System.Threading;
23using System.Threading.Tasks;
Constantin Ziesche02817f12020-08-04 21:40:43 +020024using static BaSyx.Utils.Settings.Types.ServerSettings;
Constantin Zieschefa612082020-04-03 09:54:56 +020025
26namespace 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 Zieschee837f992020-08-19 12:04:32 +0200101 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 Zieschefa612082020-04-03 09:54:56 +0200110 Directory.CreateDirectory(hostingDirectory);
111
Constantin Zieschee837f992020-08-19 12:04:32 +0200112 string filePath = Path.Join(hostingDirectory, fileName);
113 logger.Info($"Try writing file: {filePath}");
Constantin Zieschefa612082020-04-03 09:54:56 +0200114
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 Ziesche02817f12020-08-04 21:40:43 +0200126
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 Zieschefa612082020-04-03 09:54:56 +0200143 }
144}