Skip to content

Commit 4cc9080

Browse files
committed
Fix hardcoded file path
1 parent 2ca53f6 commit 4cc9080

File tree

4 files changed

+94
-98
lines changed

4 files changed

+94
-98
lines changed

source/jaindb/Controllers/HomeController.cs renamed to source/jaindb/Controllers/JainDBController.cs

+54-46
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,34 @@
1515
using Microsoft.Extensions.Caching.Memory;
1616
using System.Text;
1717
using Microsoft.AspNetCore.Authorization;
18-
using System.Collections.Generic;
18+
using Microsoft.AspNetCore.Hosting;
19+
using Microsoft.AspNetCore.Http.Extensions;
1920

2021
namespace jaindb.Controllers
2122
{
2223
[Produces("application/json")]
23-
public class HomeController : Controller
24+
public class JainDBController : Controller
2425
{
2526
private readonly IConfiguration _config;
2627
private readonly ILogger _logger;
2728
private IMemoryCache _cache;
29+
private readonly IHostingEnvironment _env;
2830

29-
public HomeController(IConfiguration config, ILogger<HomeController> logger, IMemoryCache memoryCache)
31+
public JainDBController(IConfiguration config, ILogger<JainDBController> logger, IMemoryCache memoryCache, IHostingEnvironment env)
3032
{
3133
_config = config;
3234
_logger = logger;
3335
_cache = memoryCache;
36+
_env = env;
3437
jDB._cache = memoryCache;
3538
}
3639

37-
[HttpGet]
38-
public ActionResult get()
39-
{
40-
string sVersion = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
41-
return Content("JainDB (c) 2018 by Roger Zander; Version: " + sVersion);
42-
}
40+
//[HttpGet]
41+
//public ActionResult get()
42+
//{
43+
// string sVersion = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
44+
// return Content("JainDB (c) 2018 by Roger Zander; Version: " + sVersion);
45+
//}
4346

4447
[HttpPost]
4548
[Route("upload/{Id}")]
@@ -96,32 +99,18 @@ public string GetPS()
9699
return sResult;
97100
}
98101

99-
if (System.IO.File.Exists("/app/wwwroot/inventory.ps1"))
102+
if (System.IO.File.Exists(Path.Combine(_env.WebRootPath, "inventory.ps1")))
100103
{
101-
string sFile = System.IO.File.ReadAllText("/app/wwwroot/inventory.ps1");
102-
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WebPort")))
103-
sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace(":%WebPort%", "");
104-
else
105-
sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace("%WebPort%", Environment.GetEnvironmentVariable("WebPort"));
104+
string sFile = System.IO.File.ReadAllText(Path.Combine(_env.WebRootPath, "inventory.ps1"));
106105

107-
//Cache result in Memory
108-
if (!string.IsNullOrEmpty(sResult))
109-
{
110-
var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(300)); //cache ID for 5min
111-
_cache.Set("GetPS", sResult, cacheEntryOptions);
112-
}
106+
string sLocalURL = Request.GetEncodedUrl().Replace("/getps", "");
113107

114-
return sResult;
115-
}
108+
//if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WebPort")))
109+
// sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace(":%WebPort%", "");
110+
//else
111+
// sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace("%WebPort%", Environment.GetEnvironmentVariable("WebPort"));
116112

117-
string sCurrDir = System.IO.Directory.GetCurrentDirectory();
118-
if (System.IO.File.Exists(sCurrDir + "/wwwroot/inventory.ps1"))
119-
{
120-
string sFile = System.IO.File.ReadAllText(sCurrDir + "/wwwroot/inventory.ps1");
121-
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WebPort")))
122-
sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace(":%WebPort%", "");
123-
else
124-
sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace("%WebPort%", Environment.GetEnvironmentVariable("WebPort"));
113+
sResult = sFile.Replace("%LocalURL%", sLocalURL).Replace(":%WebPort%", "");
125114

126115
//Cache result in Memory
127116
if (!string.IsNullOrEmpty(sResult))
@@ -133,21 +122,40 @@ public string GetPS()
133122
return sResult;
134123
}
135124

136-
try
137-
{
138-
string sFile2 = System.IO.File.ReadAllText("wwwroot/inventory.ps1");
139-
sResult = sFile2.Replace("%LocalURL%", "http://localhost").Replace("%WebPort%", "5000");
140-
141-
//Cache result in Memory
142-
if (!string.IsNullOrEmpty(sResult))
143-
{
144-
var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(300)); //cache ID for 5min
145-
_cache.Set("GetPS", sResult, cacheEntryOptions);
146-
}
147-
148-
return sResult;
149-
}
150-
catch { }
125+
//string sCurrDir = System.IO.Directory.GetCurrentDirectory();
126+
//if (System.IO.File.Exists(sCurrDir + "/wwwroot/inventory.ps1"))
127+
//{
128+
// string sFile = System.IO.File.ReadAllText(sCurrDir + "/wwwroot/inventory.ps1");
129+
// if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WebPort")))
130+
// sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace(":%WebPort%", "");
131+
// else
132+
// sResult = sFile.Replace("%LocalURL%", Environment.GetEnvironmentVariable("localURL")).Replace("%WebPort%", Environment.GetEnvironmentVariable("WebPort"));
133+
134+
// //Cache result in Memory
135+
// if (!string.IsNullOrEmpty(sResult))
136+
// {
137+
// var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(300)); //cache ID for 5min
138+
// _cache.Set("GetPS", sResult, cacheEntryOptions);
139+
// }
140+
141+
// return sResult;
142+
//}
143+
144+
//try
145+
//{
146+
// string sFile2 = System.IO.File.ReadAllText("wwwroot/inventory.ps1");
147+
// sResult = sFile2.Replace("%LocalURL%", "http://localhost").Replace("%WebPort%", "5000");
148+
149+
// //Cache result in Memory
150+
// if (!string.IsNullOrEmpty(sResult))
151+
// {
152+
// var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(300)); //cache ID for 5min
153+
// _cache.Set("GetPS", sResult, cacheEntryOptions);
154+
// }
155+
156+
// return sResult;
157+
//}
158+
//catch { }
151159

152160
return sResult;
153161

source/jaindb/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ RUN rm -f -r /app/wwwroot/bin
3434
##RUN rm -r /app/wwwroot/redis
3535

3636

37-
ENV localURL "http://localhost"
37+
#ENV localURL "http://localhost"
3838
ENV WebPort 5000
3939
ENV UseRedis 1
4040
ENV UseRethinkDB 0

source/jaindb/Startup.cs

+10-18
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,26 @@
1818
using Moon.AspNetCore.Authentication.Basic;
1919
using System.Threading.Tasks;
2020
using System.Security.Claims;
21-
using jaindb;
2221
using Microsoft.AspNetCore.Mvc.Authorization;
2322

2423
namespace jaindb
2524
{
2625
public class Startup
2726
{
28-
public Startup(IHostingEnvironment env)
27+
public Startup(IHostingEnvironment env, IConfiguration configuration)
2928
{
3029
var builder = new ConfigurationBuilder()
3130
.SetBasePath(env.ContentRootPath)
3231
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
3332
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
3433
.AddEnvironmentVariables();
3534
Configuration = builder.Build();
35+
//Configuration = configuration;
36+
Env = env;
3637
}
3738

38-
public IConfigurationRoot Configuration { get; }
39+
public IConfiguration Configuration { get; }
40+
public IHostingEnvironment Env { get; }
3941

4042
// This method gets called by the runtime. Use this method to add services to the container.
4143
public void ConfigureServices(IServiceCollection services)
@@ -60,16 +62,6 @@ public void ConfigureServices(IServiceCollection services)
6062
};
6163
});
6264

63-
//Custom Authentication Provider
64-
/*services.AddAuthentication(options =>
65-
{
66-
// the scheme name has to match the value we're going to use in AuthenticationBuilder.AddScheme(...)
67-
options.DefaultAuthenticateScheme = "Custom Scheme";
68-
options.DefaultChallengeScheme = "Custom Scheme";
69-
}).AddCustomAuth(o => { });*/
70-
71-
// Add framework services.
72-
//services.AddMvc();
7365
services.AddMvc(options =>
7466
{
7567
options.OutputFormatters.RemoveType<StringOutputFormatter>();
@@ -103,7 +95,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
10395
}
10496
else
10597
{
106-
app.UseExceptionHandler("/Home/Error");
98+
app.UseExceptionHandler("/JainDB/Error");
10799
}
108100

109101
app.UseAuthentication();
@@ -114,7 +106,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
114106
{
115107
routes.MapRoute(
116108
name: "default",
117-
template: "{controller=Home}/{action=Index}/{id?}");
109+
template: "{controller=JainDB}/{action=Index}/{id?}");
118110
});
119111
}
120112

@@ -171,9 +163,9 @@ private void OnStartup()
171163
Console.WriteLine("-------------------------------------------------------------------");
172164
Console.WriteLine(" ");
173165

174-
string sHashType = Environment.GetEnvironmentVariable("HashType");
166+
string sHashType = Environment.GetEnvironmentVariable("HashType") ?? "";
175167
if (string.IsNullOrEmpty(sHashType))
176-
sHashType = Configuration.GetSection("jaindb:HashType").Value ?? Configuration.GetSection("HashType").Value;
168+
sHashType = Configuration.GetSection("jaindb:HashType").Value ?? Configuration.GetSection("HashType").Value ?? "md5";
177169

178170
switch (sHashType.ToLower())
179171
{
@@ -191,7 +183,7 @@ private void OnStartup()
191183
break;
192184
}
193185

194-
if ((int.Parse(Configuration.GetSection("UseRedis").Value ?? Configuration.GetSection("jaindb:UseRedis").Value) == 1) || (Environment.GetEnvironmentVariable("UseRedis")) == "1")
186+
if ((int.Parse(Configuration.GetSection("UseRedis").Value ?? Configuration.GetSection("jaindb:UseRedis").Value) == 1) || (Environment.GetEnvironmentVariable("UseRedis") ?? "0") == "1")
195187
{
196188
try
197189
{

0 commit comments

Comments
 (0)