Skip to content

Commit

Permalink
Basic server root
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Oct 21, 2024
1 parent 1bb9c06 commit c511a60
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/server/routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Type } from '@sinclair/typebox'
import * as fs from 'node:fs'
import { kProjectReplicate } from '../mapeo-project.js'
import { wsCoreReplicator } from './ws-core-replicator.js'
import timingSafeEqual from '../lib/timing-safe-equal.js'
Expand All @@ -12,6 +13,8 @@ const HEX_STRING_32_BYTES = Type.String({ pattern: HEX_REGEX_32_BYTES })
const BASE32_REGEX_32_BYTES = '^[0-9A-Za-z]{52}$'
const BASE32_STRING_32_BYTES = Type.String({ pattern: BASE32_REGEX_32_BYTES })

const INDEX_HTML_PATH = new URL('./static/index.html', import.meta.url)

/**
* @typedef {object} RouteOptions
* @prop {string} serverBearerToken
Expand All @@ -37,6 +40,12 @@ export default async function routes(
}
}

fastify.get('/', (_req, reply) => {
const stream = fs.createReadStream(INDEX_HTML_PATH)
reply.header('Content-Type', 'text/html')
reply.send(stream)
})

fastify.get(
'/info',
{
Expand Down Expand Up @@ -283,7 +292,7 @@ export default async function routes(
url: new URL(
`projects/${projectPublicId}/attachments/${attachment.driveDiscoveryId}/${attachment.type}/${attachment.name}`,
req.baseUrl
),
).href,
})),
tags: obs.tags,
})
Expand Down
43 changes: 43 additions & 0 deletions src/server/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>CoMapeo</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
* {
box-sizing: border-box;
}

html {
display: flex;
min-height: 100vh;
font-family: sans-serif;
font-size: 28pt;
text-align: center;
color: black;
background: white;
}

body {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

@media (prefers-color-scheme: dark) {
html {
color: white;
background: black;
}
}
</style>
</head>
<body>
<p>¡Hola desde CoMapeo!</p>
<p>Olá da CoMapeo!</p>
<p>Hello from CoMapeo!</p>
</body>
</html>
20 changes: 20 additions & 0 deletions src/server/test/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { createTestServer } from './test-helpers.js'

test('server root', async (t) => {
const server = createTestServer(t)

const response = await server.inject({
method: 'GET',
url: '/',
})

assert.equal(response.statusCode, 200)
const contentType = response.headers['content-type']
assert(
typeof contentType === 'string' && contentType.startsWith('text/html'),
'response is HTML'
)
assert(response.body.includes('<html'), 'response body looks like HTML')
})

0 comments on commit c511a60

Please sign in to comment.