diff --git a/package.json b/package.json index cb502c5c..8770aa26 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,18 @@ { - "name": "team08-boilerplate", + "name": "dadok", "version": "0.1.0", "private": true, "scripts": { - "dev": "next dev -H dadok.app.local", + "dev": "next dev -H local.dev.dadok.app", + "ssl-dev": "node scripts/server.js local.dev.dadok.app", "build": "next build", - "start": "next start -H dadok.app.local", + "start": "next start -H local.dev.dadok.app", "lint": "next lint", "prepare": "husky install", "storybook": "storybook dev -p 6006", "build-storybook": "storybook build", - "update-host": "node ./updateDevHost.js dadok.app.local" + "update-host": "node scripts/updateDevHost.js local.dev.dadok.app", + "init-https": "sh scripts/init-mkcert.sh local.dev.dadok.app" }, "dependencies": { "@chakra-ui/icons": "^2.0.17", diff --git a/scripts/init-mkcert.sh b/scripts/init-mkcert.sh new file mode 100644 index 00000000..2e725bca --- /dev/null +++ b/scripts/init-mkcert.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +readonly DEFAULT_HOSTS="localhost 127.0.0.1 ::1" +readonly hostname=$1 + +readonly mkcert_intalled=$(which mkcert) + +readonly cert_dir="$PWD/.certificates" +readonly key_path="$cert_dir/localhost-key.pem" +readonly cert_path="$cert_dir/localhost.pem" + +if [ -z $mkcert_intalled ];then + echo "💬 Downloading mkcert package...\n" + brew install mkcert +fi + +mkdir $cert_dir + +echo "💬 Attempting to generate self-signed certificate...\n" + +if [ "$hostname" != "" ];then + mkcert -install -key-file $key_path -cert-file $cert_path $DEFAULT_HOSTS $hostname +else + mkcert -install -key-file $key_path -cert-file $cert_path $DEFAULT_HOSTS +fi + +echo "🚀 CA Root certificate created!" +echo "🚀 Certificates created in ${cert_dir}" diff --git a/scripts/server.js b/scripts/server.js new file mode 100644 index 00000000..109eccce --- /dev/null +++ b/scripts/server.js @@ -0,0 +1,43 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +const fs = require('fs'); +const https = require('https'); +const { parse } = require('url'); +const next = require('next'); + +const dev = process.env.NODE_ENV !== 'production'; +const hostname = process.argv[2]; +const port = 3000; + +// when using middleware `hostname` and `port` must be provided below +const app = next({ dev, hostname, port }); +const handle = app.getRequestHandler(); + +const SELF_CERTIFICATES_PATH = { + key: './.certificates/localhost-key.pem', + cert: './.certificates/localhost.pem', +}; + +const option = { + key: fs.readFileSync(SELF_CERTIFICATES_PATH.key), + cert: fs.readFileSync(SELF_CERTIFICATES_PATH.cert), +}; + +app.prepare().then(() => { + https + .createServer(option, async (req, res) => { + try { + // Be sure to pass `true` as the second argument to `url.parse`. + // This tells it to parse the query portion of the URL. + const parsedUrl = parse(req.url, true); + handle(req, res, parsedUrl); + } catch (err) { + console.error('Error occurred handling', req.url, err); + res.statusCode = 500; + res.end('internal server error'); + } + }) + .listen(port, error => { + if (error) throw error; + console.log(`> Ready on https://${hostname}:${port}`); + }); +}); diff --git a/updateDevHost.js b/scripts/updateDevHost.js similarity index 100% rename from updateDevHost.js rename to scripts/updateDevHost.js