Skip to content

Commit

Permalink
fix: Site does not load when hosted in a subdirectory of the web host…
Browse files Browse the repository at this point in the history
… root (#43)

Fixes #32
  • Loading branch information
bourquep authored Nov 15, 2024
1 parent 0e77912 commit 845da98
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ There's not much else needed to get started. You can have a look at the `skin.co
liking. The most likely customization you'll want to make is to edit the `observations_current` and `observations_summary`
variables to list which observations you want on your Me.teo dashboard and in what order.

### Configuring `HTML_ROOT` and `HTML_SUBDIR`

If you are installing this skin in a subdirectory of your web server, you need to set `HTML_SUBDIR` to the name of the subdirectory.
For example, if your web server is serving files from `/var/www/html`, and you have set `HTML_ROOT` for this skin to `/var/www/html/meteo`,
you must set `HTML_SUBDIR` to `meteo`.

```ini
[StdReport]
[[Me.teo]]
skin = me.teo
enable = true

HTML_ROOT = public_html/foo/bar

# If web server is configured to serve files from public_html:
HTML_SUBDIR = foo/bar
```

### Google Analytics

If you have a Google Analytics account and want to track visits to your site, you can add your tracking ID to the skin
Expand Down
2 changes: 1 addition & 1 deletion next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const withNextIntl = createNextIntlPlugin();

const nextConfig: NextConfig = {
output: 'export',
basePath: `/${process.env.METEO_BUILD_LOCALE || 'en'}`,
basePath: `/##METEO_BASE_PATH##/${process.env.METEO_BUILD_LOCALE || 'en'}`,
distDir: `out/${process.env.METEO_BUILD_LOCALE || 'en'}`
};

Expand Down
2 changes: 1 addition & 1 deletion src/libs/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const fetcher = async (url: string | URL | Request) => {
return res.json();
};

const baseUrl = process.env.NODE_ENV === 'production' ? '/data' : '/sample_data';
const baseUrl = process.env.NODE_ENV === 'production' ? '/##METEO_BASE_PATH##/data' : '/sample_data';

export function useGlobalData() {
return useSWR<GlobalData>(`${baseUrl}/global.json`, fetcher);
Expand Down
34 changes: 34 additions & 0 deletions weewx/bin/user/meteo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from weewx.reportengine import ReportGenerator
import os.path
import logging

log = logging.getLogger(__name__)

class NextJsBasePathGenerator(ReportGenerator):
def run(self):
html_root = self.skin_dict.get('HTML_ROOT', self.config_dict['StdReport']['HTML_ROOT'])
html_dest_dir = os.path.join(self.config_dict['WEEWX_ROOT'], html_root)
html_subdir = self.skin_dict.get('HTML_SUBDIR', '')

log.info("Replacing ##METEO_BASE_PATH## with '%s' in all html, js, css and txt files in '%s'...", html_subdir, html_dest_dir)

for root, dirs, files in os.walk(html_dest_dir):
for file in files:
if file.endswith('.html') or file.endswith('.js') or file.endswith('.css') or file.endswith('.txt'):
filepath = os.path.join(root, file)
log.debug("Processing '%s'...", filepath)

try:
with open(filepath, 'r') as f:
content = f.read()

if html_subdir == '':
content = content.replace('/##METEO_BASE_PATH##', '')
else:
content = content.replace('##METEO_BASE_PATH##', html_subdir)

with open(filepath, 'w') as f:
f.write(content)
except Exception as e:
log.error("Unable to process '%s'. Got exception '%s': %s", filepath, type(e), e)
pass
7 changes: 5 additions & 2 deletions weewx/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import configobj
from setup import ExtensionInstaller
from weecfg.extension import ExtensionInstaller
from io import StringIO
import os

Expand All @@ -41,14 +41,17 @@ def get_files():

class MeteoInstaller(ExtensionInstaller):
def __init__(self):
files = get_files()
files.append(('bin/user', ['bin/user/meteo.py']))

super(MeteoInstaller, self).__init__(
version=VERSION,
name=NAME,
description=DESCRIPTION,
author=AUTHOR,
author_email=AUTHOR_EMAIL,
config=config_dict,
files=get_files()
files=files
)

config_string = """
Expand Down
7 changes: 6 additions & 1 deletion weewx/skins/me.teo/skin.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
SKIN_NAME = Me.teo
SKIN_VERSION = ##SKIN_VERSION##

# If you are installing this skin in a subdirectory of your web server, you need to set HTML_SUBDIR to the name of the subdirectory.
# For example, if your web server is serving files from "/var/www/html", and you have set HTML_ROOT for this skin to "/var/www/html/meteo",
# set HTML_SUBDIR to "meteo".
# HTML_SUBDIR = meteo

[Extras]
# If you have a Google Analytics GA4 tag, uncomment and edit the next line, and
# the analytics code will be included in the generated pages.
Expand Down Expand Up @@ -69,4 +74,4 @@ SKIN_VERSION = ##SKIN_VERSION##
copy_once = en, fr

[Generators]
generator_list = weewx.cheetahgenerator.CheetahGenerator, weewx.reportengine.CopyGenerator
generator_list = weewx.cheetahgenerator.CheetahGenerator, weewx.reportengine.CopyGenerator, user.meteo.NextJsBasePathGenerator

0 comments on commit 845da98

Please sign in to comment.