-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProgram.cs
More file actions
84 lines (71 loc) · 3.19 KB
/
Program.cs
File metadata and controls
84 lines (71 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
---- Copyright Start ----
This file is part of the OpenVectorFormatTools collection. This collection provides tools to facilitate the usage of the OpenVectorFormat.
Copyright (C) 2024 Digital-Production-Aachen
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
---- Copyright End ----
*/
using System;
using System.IO;
using System.Net;
using System.Text.Json;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace OpenVectorFormat.FileHandlerFactoryGRPCWrapper
{
class Program
{
public static void Main(string[] args)
{
ParseConfig();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(options =>
{
IPAddress.TryParse(Config.IP, out IPAddress ip);
options.Listen(ip, Config.Port);
});
webBuilder.UseStartup<Startup>();
});
private static void ParseConfig()
{
string path = System.Reflection.Assembly.GetEntryAssembly().Location;
path = Path.GetDirectoryName(path);
string configFile = path + "/grpc_server_config.json";
if (File.Exists(configFile))
{
// read values from config file if the file exists
JsonElement? json = JsonSerializer.Deserialize<object>(File.ReadAllText(configFile)) as JsonElement?;
Config.IP = json?.GetProperty("ip").ToString();
Config.Port = int.Parse(json?.GetProperty("port").ToString());
Config.AutomatedCachingThresholdBytes = long.Parse(json?.GetProperty("AutomatedCachingThresholdBytes").ToString());
}
else
{
// read values from environment variables (primary for Docker container)
Config.IP = Environment.GetEnvironmentVariable("GRPC_SERVER_IP");
Config.Port = int.Parse(Environment.GetEnvironmentVariable("GRPC_SERVER_PORT"));
Config.AutomatedCachingThresholdBytes = long.Parse(Environment.GetEnvironmentVariable("GRPC_SERVER_AUTOMATED_CACHING_THRESHOLD_BYTES"));
}
if (Config.IP == string.Empty | Config.Port == -1 | Config.AutomatedCachingThresholdBytes == -1)
{
throw new InvalidDataException("Did not get all config values.");
}
}
}
}