Skip to content

Commit

Permalink
Next.js service (#53)
Browse files Browse the repository at this point in the history
* moved jackson-next to this repo

* fixed working-directory

* updated package-lock

* fixed docker build

* fixed dockerfile

* cleanup

* save npm version for use in the build step

* switching the order

* fixed env secret

* update saml-jackson to the current version before building the next.js service

* build from typescript and change main and types before publishing npm

* copy README.md from root before publishing npm

* update README only for prod versions

* read version from root package.json file

* fixed artifact

* updated package-lock
  • Loading branch information
deepakprabhakara authored Jan 5, 2022
1 parent 40706fd commit 3754f2b
Show file tree
Hide file tree
Showing 77 changed files with 31,078 additions and 11,528 deletions.
12 changes: 0 additions & 12 deletions nodemon.json

This file was deleted.

4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
node_modules
npm-debug.log
.git
.github
_dev
.vscode
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ module.exports = {
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'next/core-web-vitals',
],
};
44 changes: 40 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,44 @@ jobs:
scope: '@boxyhq'
cache: 'npm'
- run: npm install
working-directory: ./npm
- run: npm run test
working-directory: ./npm
- run: |
publishTag="latest"
npm install -g json
JACKSON_VERSION=$(echo $(cat ../package.json) | json version)
echo ver=$JACKSON_VERSION
json -I -f package.json -e "this.main=\"dist/index.js\""
json -I -f package.json -e "this.types=\"dist/index.d.ts\""
json -I -f package.json -e "this.version=\"${JACKSON_VERSION}\""
if [[ "$GITHUB_REF" == *\/release ]]
then
echo "Release branch"
cp ../README.md .
else
echo "Dev branch"
publishTag="beta"
versionSuffixTag="-beta.${GITHUB_RUN_NUMBER}"
sed "s/\(^[ ]*\"version\"\:[ ]*\".*\)\",/\1${versionSuffixTag}\",/" < package.json > package.json.new
mv package.json.new package.json
JACKSON_VERSION="${JACKSON_VERSION}-beta.${GITHUB_RUN_NUMBER}"
json -I -f package.json -e "this.version=\"${JACKSON_VERSION}\""
fi
npm publish --tag $publishTag --access public
echo ${JACKSON_VERSION} > npmversion.txt
echo $(cat npmversion.txt)
working-directory: ./npm
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Upload saml-jackson npm version
uses: actions/upload-artifact@v2
with:
name: npmversion
path: ./npm/npmversion.txt

build:
needs: publish
runs-on: ubuntu-latest
Expand All @@ -100,6 +122,20 @@ jobs:
id: slug
run: echo "::set-output name=sha7::$(echo ${GITHUB_SHA} | cut -c1-7)"

- name: Download saml-jackson npm version
uses: actions/download-artifact@v2
with:
name: npmversion

- name: Get saml-jackson npm version
id: npmversion
run: echo "::set-output name=npmversion::$(cat npmversion.txt)"

- run: echo ${{ steps.npmversion.outputs.npmversion }}

# - name: Update @boxyhq/saml-jackson
# run: npm install @boxyhq/saml-jackson@${{ steps.npmversion.outputs.npmversion }}

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
Expand All @@ -117,7 +153,7 @@ jobs:
context: ./
file: ./Dockerfile
push: true
tags: ${{ github.repository }}:latest,${{ github.repository }}:${{ steps.slug.outputs.sha7 }}
tags: ${{ github.repository }}:latest,${{ github.repository }}:${{ steps.slug.outputs.sha7 }},${{ github.repository }}:${{ steps.npmversion.outputs.npmversion }}

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
43 changes: 38 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
.vscode
node_modules/**
.nyc_output
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

_config
dist
.DS_Store

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel

# typescript
*.tsbuildinfo
15 changes: 15 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
node_modules
.next
public
**/**/node_modules
**/**/.next
**/**/public

*.lock
*.log

.gitignore
.npmignore
.prettierignore
.DS_Store
.eslintignore
11 changes: 11 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
bracketSpacing: true,
bracketSameLine: true,
singleQuote: true,
jsxSingleQuote: true,
trailingComma: "es5",
semi: true,
printWidth: 110,
arrowParens: "always",
importOrderSeparation: true,
};
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
38 changes: 25 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# Install dependencies only when needed
FROM node:16.13.1-alpine3.14 AS build
FROM node:16.13.1-alpine3.14 AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY src/ src/
COPY package.json package-lock.json tsconfig*.json ./
COPY package.json package-lock.json ./
COPY npm npm
RUN npm install
RUN npm run build

# Rebuild the source code only when needed
FROM node:16.13.1-alpine3.14 AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/npm ./npm
COPY --from=deps /app/node_modules ./node_modules
RUN npm run build && npm install --production --ignore-scripts --prefer-offline

# Production image, copy all the files and run next
FROM node:16.13.1-alpine3.14 AS runner
Expand All @@ -16,16 +23,21 @@ ENV NODE_OPTIONS="--max-http-header-size=81920"
ENV NODE_ENV production

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nodejs -u 1001
RUN adduser -S nextjs -u 1001

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/npm ./npm
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

COPY --from=build /app/dist ./dist
COPY --from=build /app/package.json ./package.json
COPY --from=build /app/package-lock.json ./package-lock.json
RUN npm ci --only=production
USER nextjs

USER nodejs
EXPOSE 3000

EXPOSE 5000
EXPOSE 6000
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
ENV NEXT_TELEMETRY_DISABLED 1

CMD [ "node", "dist/jackson.js" ]
CMD ["npm", "start"]
32 changes: 32 additions & 0 deletions lib/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DatabaseEngine, DatabaseType } from '@boxyhq/saml-jackson';

const hostUrl = process.env.HOST_URL || 'localhost';
const hostPort = Number(process.env.PORT || '5000');
const externalUrl = process.env.EXTERNAL_URL || 'http://' + hostUrl + ':' + hostPort;
const samlPath = '/api/oauth/saml';

const apiKeys = (process.env.JACKSON_API_KEYS || '').split(',');

const samlAudience = process.env.SAML_AUDIENCE;
const preLoadedConfig = process.env.PRE_LOADED_CONFIG;

const idpEnabled = !!process.env.IDP_ENABLED;
const db = {
engine: process.env.DB_ENGINE ? <DatabaseEngine>process.env.DB_ENGINE : undefined,
url: process.env.DB_URL,
type: process.env.DB_TYPE ? <DatabaseType>process.env.DB_TYPE : undefined,
ttl: process.env.DB_TTL ? Number(process.env.DB_TTL) : undefined,
encryptionKey: process.env.DB_ENCRYPTION_KEY,
};

export default {
hostUrl,
hostPort,
externalUrl,
samlPath,
samlAudience,
preLoadedConfig,
apiKeys,
idpEnabled,
db,
};
25 changes: 25 additions & 0 deletions lib/jackson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import jackson, { IAPIController, IOAuthController } from '@boxyhq/saml-jackson';
import env from '@lib/env';

let apiController: IAPIController;
let oauthController: IOAuthController;

const g = global as any;

export default async function init() {
if (!g.apiController || !g.oauthController) {
const ret = await jackson(env);
apiController = ret.apiController;
oauthController = ret.oauthController;
g.apiController = apiController;
g.oauthController = oauthController;
} else {
apiController = g.apiController;
oauthController = g.oauthController;
}

return {
apiController,
oauthController,
};
}
25 changes: 25 additions & 0 deletions lib/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Cors from 'cors';
import { NextApiRequest, NextApiResponse } from 'next';

// Initializing the cors middleware
const corsFunction = Cors({
methods: ['GET', 'HEAD'],
});

// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function runMiddleware(req: NextApiRequest, res: NextApiResponse, fn: any) {
return new Promise((resolve, reject) => {
fn(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result);
}

return resolve(result);
});
});
}

export async function cors(req: NextApiRequest, res: NextApiResponse) {
return await runMiddleware(req, res, corsFunction);
}
11 changes: 0 additions & 11 deletions map.js

This file was deleted.

5 changes: 5 additions & 0 deletions next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
4 changes: 4 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
module.exports = {
reactStrictMode: true,
}
File renamed without changes.
19 changes: 19 additions & 0 deletions npm/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
env: {
es2021: true,
node: true,
},
parserOptions: {
ecmaVersion: 13,
sourceType: 'module',
},
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'next/core-web-vitals',
],
};
9 changes: 9 additions & 0 deletions npm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

.vscode
.nyc_output
_config
dist
.DS_Store
/node_modules
**/node_modules/**
Loading

0 comments on commit 3754f2b

Please sign in to comment.