1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using JetBrains.Annotations;
|
---|
4 | using Webserver.FileCache;
|
---|
5 | using Webserver.UrlHandlers;
|
---|
6 |
|
---|
7 | namespace AllocsFixes.Web {
|
---|
8 | [UsedImplicitly]
|
---|
9 | public class API : IModApi {
|
---|
10 | private static Mod modInstance;
|
---|
11 |
|
---|
12 | public void InitMod (Mod _modInstance) {
|
---|
13 | modInstance = _modInstance;
|
---|
14 |
|
---|
15 | Webserver.Web.ServerInitialized += OnWebServerInitialized;
|
---|
16 | }
|
---|
17 |
|
---|
18 | private static void OnWebServerInitialized (Webserver.Web _web) {
|
---|
19 | try {
|
---|
20 | const string legacyModUrl = "/legacymap";
|
---|
21 | const string legacyFilesFoldername = "webserver_legacy";
|
---|
22 | string legacyFilePath = $"{modInstance.Path}/{legacyFilesFoldername}";
|
---|
23 |
|
---|
24 | if (!Directory.Exists (legacyFilePath)) {
|
---|
25 | Log.Out ($"Legacy webmod feature not started (folder \"{legacyFilesFoldername}\" not found in Allocs_WebAndMapRendering mod folder)");
|
---|
26 | return;
|
---|
27 | }
|
---|
28 |
|
---|
29 | // TODO: Read from config
|
---|
30 | bool useStaticCache = false;
|
---|
31 |
|
---|
32 | _web.RegisterPathHandler ($"{legacyModUrl}", new SimpleRedirectHandler ($"{legacyModUrl}/index.html"));
|
---|
33 | _web.RegisterPathHandler ($"{legacyModUrl}/", new StaticHandler (legacyFilePath, useStaticCache ? new SimpleCache () : new DirectAccess (), false));
|
---|
34 |
|
---|
35 | int webPort = GamePrefs.GetInt (EnumUtils.Parse<EnumGamePrefs> (nameof(EnumGamePrefs.WebDashboardPort)));
|
---|
36 | Log.Out ($"Started legacy webmod feature on port {webPort}, local adress {legacyModUrl}");
|
---|
37 | } catch (Exception e) {
|
---|
38 | Log.Out ($"Error in Web.ctor: {e}");
|
---|
39 | }
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|