Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: redirect to webapp by displayName #1822

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/develop/webapps.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ You can also include the following section in `package.json` to control how your

where:
- `appIcon` is the path (relative to the `public` directory) to an image within the package to display in the webapp list. The image should be at least 72x72 pixels in size.
- `displayName` is the text you want to appear as the name in the webapp list. _(By default the _name_ attribute in the `package.json` is used.)_
- `displayName` is the text you want to appear as the name in the webapp list. _(By default the _name_ attribute in the `package.json` is used.)_. Displayname is also used in an automatic redirect from the root of the server: if you have a webapp with displayName `foo` and you access it using for example the url http://foo.bar.org:3000 the first part of the hostname matches the webapp's displayName and you will be redirected to it instead of the default landingPage, the Admin webapp. With this mechanism you can add easy to access DNS names to each webapp, including .local names.

See also [Working Offline](./developer_notes.md#offline-use).

Expand Down
19 changes: 18 additions & 1 deletion src/serverroutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,24 @@ module.exports = function (
)

app.get('/', (req: Request, res: Response) => {
res.redirect(app.config.settings.landingPage || '/admin/')
let landingPage = '/admin/'

// if accessed with hostname that starts with a webapp's displayName redirect there
//strip possible port number
const firstHostName = (req.headers?.host || '')
.split(':')[0]
.split('.')[0]
.toLowerCase()
const targetWebapp = app.webapps.find(
(webapp) =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(webapp as any).signalk?.displayName.toLowerCase() === firstHostName
)
if (targetWebapp) {
landingPage = `/${targetWebapp.name}/`
}

res.redirect(app.config.settings.landingPage || landingPage)
})

app.get('/@signalk/server-admin-ui', (req: Request, res: Response) => {
Expand Down