Skip to content

Commit

Permalink
feat(apps/earth): starts adding in deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
deopea-david committed Feb 14, 2023
1 parent b9adc79 commit aa8a78e
Show file tree
Hide file tree
Showing 16 changed files with 1,481 additions and 272 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ module.exports = {
extends: ["@deopea.os/eslint-config", "turbo"],
parserOptions: {
tsconfigRootDir: __dirname,
project: ["./tsconfig.json"],
project: "./tsconfig.json",
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ lib-types

# turbo
.turbo
out
2 changes: 1 addition & 1 deletion apps/earth/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
server/
server
48 changes: 48 additions & 0 deletions apps/earth/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# * Pruner * #
FROM node:18 AS pruner

WORKDIR /app

COPY ./ ./

# Prune
RUN npx -y turbo prune --scope="@deopea.os/earth" --docker
# * Pruner * #

# * Builder * #
FROM node:18 AS builder

WORKDIR /app

# Install
COPY .gitignore ./
COPY --from=pruner \
# sources
/app/out/json/ \
/app/out/package-lock.json \
# destination
./

# Install
RUN npm ci --omit="dev"

# Build
COPY --from=pruner /app/out/full/ ./
RUN npx turbo build --filter="@deopea.os/earth"
# * Builder * #

# * Runner * #
FROM node:18 AS runner

WORKDIR /app

# User
RUN addgroup --system --gid 1001 app
RUN adduser --system --uid 1001 app
USER app

COPY --from=builder /app/apps/earth/package.json .
COPY --from=builder --chown=app:app /app/apps/earth/dist dist
COPY --from=builder --chown=app:app /app/apps/earth/server server

CMD npm start
10 changes: 10 additions & 0 deletions apps/earth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ The production build will generate client and server modules by running both cli
```shell
npm run build # or `yarn build`
```

## Express Server

This app has a minimal [Express server](https://expressjs.com/) implementation. After running a full build, you can preview the build using the command:

```sh
npm run serve
```

Then visit [http://localhost:8080/](http://localhost:8080/)
15 changes: 15 additions & 0 deletions apps/earth/adapters/express/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expressAdapter } from '@builder.io/qwik-city/adapters/express/vite';
import { extendConfig } from '@builder.io/qwik-city/vite';
import baseConfig from '../../vite.config';

export default extendConfig(baseConfig, () => {
return {
build: {
ssr: true,
rollupOptions: {
input: ['src/entry.express.tsx', '@qwik-city-plan'],
},
},
plugins: [expressAdapter()],
};
});
17 changes: 11 additions & 6 deletions apps/earth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,41 @@
"npm": ">=8.0.0"
},
"scripts": {
".build": "qwik build",
"build": "qwik build",
"build.client": "vite build",
"build.preview": "vite build --ssr src/entry.preview.tsx",
"build.server": "vite build -c adapters/express/vite.config.ts",
"build.types": "tsc --incremental --noEmit",
"deploy": "echo 'Run \"npm run qwik add\" to install a server adaptor'",
"deploy": "node server/entry.express",
"dev": "vite --mode ssr",
"dev.debug": "node --inspect-brk ./node_modules/vite/bin/vite.js --mode ssr --force",
"dotenv": "npx dotenv-vault",
"fmt": "prettier --write --cache --cache-location \".turbo/.prettier-cache\" .",
"fmt.check": "prettier --check .",
"lint": "TIMING=1 eslint --cache --cache-location \".turbo/.eslintcache\"",
"preview": "qwik build preview && vite preview --open",
"start": "vite --open --mode ssr",
"start": "node server/entry.express.js",
"qwik": "qwik"
},
"devDependencies": {
"@builder.io/qwik": "^0.16.2",
"@builder.io/qwik-city": "^0.1.0-beta9",
"@builder.io/qwik": "^0.18.1",
"@builder.io/qwik-city": "^0.2.1",
"@deopea.os/eslint-config": "^1.0.0",
"@deopea.os/tsconfig": "^1.0.0",
"@deopea.os/ui": "^1.0.0-alpha.1",
"@tailwindcss/typography": "^0.5.9",
"@types/compression": "^1.7.2",
"@types/express": "4.17.13",
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"autoprefixer": "^10.4.11",
"aws-cdk-lib": "^2.64.0",
"compression": "^1.7.4",
"daisyui": "^2.50.0",
"eslint": "^8.31.0",
"eslint-plugin-qwik": "^0.16.2",
"espree": "^9.4.1",
"express": "4.17.3",
"postcss": "^8.4.16",
"prettier": "^2.8.2",
"tailwindcss": "^3.1.8",
Expand Down
50 changes: 50 additions & 0 deletions apps/earth/src/entry.express.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* WHAT IS THIS FILE?
*
* It's the entry point for the express server when building for production.
*
* Learn more about the cloudflare integration here:
* - https://qwik.builder.io/integrations/deployments/node/
*
*/
import { createQwikCity } from '@builder.io/qwik-city/middleware/node';
import qwikCityPlan from '@qwik-city-plan';
import render from './entry.ssr';
import express from 'express';
import { fileURLToPath } from 'node:url';
import { join } from 'node:path';
// import compression from 'compression';

// Directories where the static assets are located
const distDir = join(fileURLToPath(import.meta.url), '..', '..', 'dist');
const buildDir = join(distDir, 'build');

// Allow for dynamic port
const PORT = process.env.PORT ?? 3000;

// Create the Qwik City express middleware
const { router, notFound } = createQwikCity({ render, qwikCityPlan });

// Create the express server
// https://expressjs.com/
const app = express();

// Enable gzip compression
// app.use(compression());

// Static asset handlers
// https://expressjs.com/en/starter/static-files.html
app.use(`/build`, express.static(buildDir, { immutable: true, maxAge: '1y' }));
app.use(express.static(distDir, { redirect: false }));

// Use Qwik City's page and endpoint request handler
app.use(router);

// Use Qwik City's 404 handler
app.use(notFound);

// Start the express server
app.listen(PORT, () => {
/* eslint-disable */
console.log(`Server starter: http://localhost:${PORT}/`);
});
14 changes: 7 additions & 7 deletions apps/earth/src/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* the styles in here will be applied to the Document, without any sort of CSS scoping.
*
*/
@tailwind base;
@tailwind components;
@tailwind utilities;
html {

@tailwind base;
@tailwind components;
@tailwind utilities;

html {
height: 100%;
}
}
5 changes: 1 addition & 4 deletions apps/earth/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { component$, useStyles$ } from "@builder.io/qwik";
import { component$ } from "@builder.io/qwik";
import type { DocumentHead } from "@builder.io/qwik-city";
import { AnimatedBlobs } from "components/blob";
import styles from "./index.css?inline";

export default component$(() => {
useStyles$(styles);

const expansionClasses = [
"w-0",
"max-w-fit",
Expand Down
24 changes: 24 additions & 0 deletions apps/earth/stack/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as apigateway from "aws-cdk-lib/aws-apigateway";
import * as lambda from "aws-cdk-lib/aws-lambda";
import { Construct } from "constructs";

export class APIStack extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);

const api = new apigateway.RestApi(this, "earth-api", {
restApiName: "Earth API",
description: "API for Deopea/Earth",
});

const ssrHandler = new lambda.DockerImageFunction(this, "ClientHandler", {
code: lambda.DockerImageCode.fromImageAsset(__dirname),
});

const ssrRoot = api.root.addResource("/ssr");
ssrRoot.addMethod(
"GET",
new apigateway.LambdaIntegration(ssrHandler, { proxy: true }),
);
}
}
8 changes: 8 additions & 0 deletions apps/earth/stack/cdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env node

import * as cdk from "aws-cdk-lib";
import { APIStack } from "./api";

const app = new cdk.App();

new APIStack(app, "APIStack");
10 changes: 10 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3"

services:
earth:
container_name: earth
build:
context: .
dockerfile: apps/earth/Dockerfile
ports:
- 8080:4173
Loading

0 comments on commit aa8a78e

Please sign in to comment.