Skip to content

Commit

Permalink
[#531] 로컬 서버에 https 적용 (#541)
Browse files Browse the repository at this point in the history
* feat: mkcert 패키지 install 스크립트 작성

* feat: next https custom server 작성
  • Loading branch information
gxxrxn committed Jun 17, 2024
1 parent 5d23ab3 commit f2f4a82
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
28 changes: 28 additions & 0 deletions scripts/init-mkcert.sh
Original file line number Diff line number Diff line change
@@ -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}"
43 changes: 43 additions & 0 deletions scripts/server.js
Original file line number Diff line number Diff line change
@@ -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}`);
});
});
File renamed without changes.

0 comments on commit f2f4a82

Please sign in to comment.