[391] | 1 | using System.IO;
|
---|
[402] | 2 | using Webserver.FileCache;
|
---|
[391] | 3 | using Webserver.UrlHandlers;
|
---|
| 4 |
|
---|
| 5 | namespace Webserver {
|
---|
| 6 | public class WebMod {
|
---|
| 7 | private const string modsBaseUrl = "/webmods/";
|
---|
| 8 | private const string reactBundleName = "bundle.js";
|
---|
| 9 | private const string stylingFileName = "styling.css";
|
---|
| 10 |
|
---|
| 11 | public readonly Mod ParentMod;
|
---|
| 12 | public readonly string ReactBundle; // Absolute web path to the React bundle if the mod has one, e.g. "/webmods/myMod/bundle.js"
|
---|
| 13 | public readonly string CssPath; // Absolute web path to a CSS if the mod has one, e.g. "/webmods/myMod/styling.css";
|
---|
| 14 |
|
---|
| 15 | public WebMod (Web _parentWeb, Mod _parentMod, bool _useStaticCache) {
|
---|
[402] | 16 | string folder = $"{_parentMod.Path}/WebMod";
|
---|
[391] | 17 | if (!Directory.Exists (folder)) {
|
---|
| 18 | throw new InvalidDataException("No WebMod folder in mod");
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | string urlWebModBase = $"{modsBaseUrl}{_parentMod.FolderName}/";
|
---|
| 22 |
|
---|
[402] | 23 | ReactBundle = $"{folder}/{reactBundleName}";
|
---|
| 24 | ReactBundle = File.Exists (ReactBundle) ? $"{urlWebModBase}{reactBundleName}" : null;
|
---|
[391] | 25 |
|
---|
[402] | 26 | CssPath = $"{folder}/{stylingFileName}";
|
---|
| 27 | CssPath = File.Exists (CssPath) ? $"{urlWebModBase}{stylingFileName}" : null;
|
---|
[391] | 28 |
|
---|
| 29 | if (ReactBundle == null && CssPath == null) {
|
---|
| 30 | throw new InvalidDataException($"WebMod folder has neither a {reactBundleName} nor a {stylingFileName}");
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | ParentMod = _parentMod;
|
---|
| 34 |
|
---|
| 35 | _parentWeb.RegisterPathHandler (urlWebModBase, new StaticHandler (
|
---|
| 36 | folder,
|
---|
[402] | 37 | _useStaticCache ? new SimpleCache () : new DirectAccess (),
|
---|
[391] | 38 | false)
|
---|
| 39 | );
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | }
|
---|