Skip to content

Commit

Permalink
Deploy the example to fly.io
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Mar 25, 2024
1 parent 6456a25 commit 86d0e29
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 91 deletions.
8 changes: 8 additions & 0 deletions nodejs/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
dist
.wrangler
.dev.vars
.hono

# Change them to your taste:
wrangler.toml
49 changes: 49 additions & 0 deletions nodejs/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=19.7.0
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Vite"

# Vite app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"

# Install pnpm
ARG PNPM_VERSION=8.6.3
RUN npm install -g pnpm@$PNPM_VERSION


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3

# Install node modules
COPY --link package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile --prod=false

# Copy application code
COPY --link . .

# Build application
RUN pnpm run build

# Remove development dependencies
RUN pnpm prune --prod


# Final stage for app image
FROM nginx

# Copy built application
# COPY --from=build /app/dist /usr/share/nginx/html

# Start the server by default, this can be overwritten at runtime
EXPOSE 80
CMD [ "/usr/sbin/nginx", "-g", "daemon off;" ]
69 changes: 0 additions & 69 deletions nodejs/app/lib/jwt.ts

This file was deleted.

10 changes: 1 addition & 9 deletions nodejs/app/routes/_renderer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { jsxRenderer } from 'hono/jsx-renderer'
import { HasIslands } from 'honox/server'
import { Script } from 'honox/server'

export default jsxRenderer(({ children, title }) => {
Expand All @@ -11,19 +10,12 @@ export default jsxRenderer(({ children, title }) => {
<title>{title}</title>
<link rel='icon' type='image/x-icon' href='/favicon.ico' />

{import.meta.env.PROD ? (
<HasIslands>
<script type='module' src='/static/client.js'></script>
</HasIslands>
) : (
<script type='module' src='/app/client.ts'></script>
)}

{import.meta.env.PROD ? (
<link href='/static/assets/style.css' rel='stylesheet' />
) : (
<link href='/app/style.css' rel='stylesheet' />
)}
<Script src='/app/client.ts' />
</head>
<body class='bg-gray-50 dark:bg-gray-900 container mx-auto gap-y-4 p-6'>
<main>{children}</main>
Expand Down
3 changes: 0 additions & 3 deletions nodejs/app/server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { showRoutes } from 'hono/dev'
import { createApp } from 'honox/server'

const app = createApp()

showRoutes(app)

export default app
30 changes: 30 additions & 0 deletions nodejs/fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# fly.toml file generated for latitude-embedding-example on 2024-03-25T19:02:06+01:00

app = "latitude-embedding-example"

kill_signal = "SIGINT"
kill_timeout = 5
mounts = []
processes = []

[build]
dockerfile = "Dockerfile"

[[services]]
internal_port = 8080
processes = ["app"]
protocol = "tcp"

[services.concurrency]
hard_limit = 25
soft_limit = 20
type = "connections"

[[services.ports]]
force_https = true
handlers = ["http"]
port = 80

[[services.ports]]
handlers = ["tls", "http"]
port = 443
9 changes: 5 additions & 4 deletions nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"scripts": {
"dev": "vite",
"build": "vite build --mode client && vite build",
"preview": "wrangler pages dev ./dist",
"deploy": "$npm_execpath run build && wrangler pages deploy ./dist"
"deploy": "fly deploy",
"start": "node dist/server.mjs"
},
"private": true,
"dependencies": {
"@latitude-data/jwt": "0.2.0",
"@hono/node-server": "^1.9.0",
"hono": "^4.1.3",
"honox": "^0.1.9",
"@latitude-data/jwt": "0.2.0"
"honox": "^0.1.9"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240208.0",
Expand Down
9 changes: 6 additions & 3 deletions nodejs/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions nodejs/vite-node-server-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { builtinModules } from 'module'
import type { Plugin, UserConfig } from 'vite'

export const nodeServerPlugin = (): Plugin => {
const virtualEntryId = 'virtual:node-server-entry-module'
const resolvedVirtualEntryId = '\0' + virtualEntryId

return {
name: '@hono/vite-node-server',
resolveId(id) {
if (id === virtualEntryId) {
return resolvedVirtualEntryId
}
},
async load(id) {
if (id === resolvedVirtualEntryId) {
return `import { Hono } from 'hono'
import { serveStatic } from '@hono/node-server/serve-static'
import { serve } from '@hono/node-server'
const worker = new Hono()
worker.use('/static/*', serveStatic({ root: './dist' }))
const modules = import.meta.glob(['/app/server.ts'], { import: 'default', eager: true })
for (const [, app] of Object.entries(modules)) {
if (app) {
worker.route('/', app)
worker.notFound(app.notFoundHandler)
}
}
serve({ ...worker, port: 8080 }, info => {
console.log('Listening on http://localhost:'+info.port)
})`
}
},
config: async (): Promise<UserConfig> => {
return {
ssr: {
external: [],
noExternal: true,
},
build: {
outDir: './dist',
emptyOutDir: false,
minify: true,
ssr: true,
rollupOptions: {
external: [...builtinModules, /^node:/],
input: virtualEntryId,
output: {
entryFileNames: 'server.mjs',
},
},
},
}
},
}
}

export default nodeServerPlugin
25 changes: 22 additions & 3 deletions nodejs/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import pages from '@hono/vite-cloudflare-pages'
import path from 'path'
import honox from 'honox/vite'
import { defineConfig } from 'vite'
import nodeServerPlugin from './vite-node-server-plugin'

const root = './'
export default defineConfig(({ mode }) => {
if (mode === 'client') {
return {
build: {
manifest: true,
rollupOptions: {
input: ['/app/style.css'],
input: {
styles: '/app/style.css',
client: '/app/client.ts',
},
output: {
assetFileNames: 'static/assets/[name].[ext]',
},
Expand All @@ -16,7 +22,20 @@ export default defineConfig(({ mode }) => {
}
} else {
return {
plugins: [honox(), pages()],
plugins: [
honox({
islandComponents: {
isIsland: (id) => {
const resolvedPath = path.resolve(root).replace(/\\/g, '\\\\')
const regexp = new RegExp(
`${resolvedPath}[\\\\/]app[^\\\\/]*[\\\\/]islands[\\\\/].+\.tsx?$`,
)
return regexp.test(path.resolve(id))
},
},
}),
nodeServerPlugin(),
],
}
}
})

0 comments on commit 86d0e29

Please sign in to comment.