Adds https support; Fixes prod config

This commit is contained in:
pikiou 2021-11-23 22:55:03 +01:00
parent 02be480cca
commit 3ac9c72a8b
8 changed files with 134 additions and 45 deletions

View File

@ -19,6 +19,8 @@ module.exports = {
},
globals: {
__DEV__: true,
__CLIENT__: true,
__SERVER__: false,
},
maxConcurrency: 50,
maxWorkers: 1,

View File

@ -95,6 +95,7 @@
"lodash": "^4.17.21",
"morgan": "^1.10.0",
"normalize.css": "^8.0.1",
"process": "^0.11.10",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-helmet": "^6.1.0",

View File

@ -1,5 +1,6 @@
const PORT = 4000
const HOST = process.env.INTRANET_HOST || "fo.parisestludique.fr"
const HOST =
__SERVER__ && !__DEV__ ? process.env.INTRANET_HOST || "fo.parisestludique.fr" : "localhost"
export default {
PORT,

15
src/routes/certbot.ts Normal file
View File

@ -0,0 +1,15 @@
/* Copyright Coplay. All Rights Reserved. Use of this source code is governed by an MIT-style license that can be found in the LICENSE file at https://coplay.org/colicense */
import { NextFunction, Request, Response, Router } from "express"
import * as path from "path"
const certbotRouter: Router = Router()
certbotRouter.use((request: Request, response: Response, _next: NextFunction) => {
const filename = request.originalUrl.replace(/.*\//, "")
const resolvedPath: string = path.resolve(`../certbot/.well-known/acme-challenge/${filename}`)
console.log("response", resolvedPath)
response.setHeader("Content-Type", "text/html")
return response.sendFile(resolvedPath)
})
export default certbotRouter

View File

@ -1,8 +1,6 @@
import { Express } from "express"
import chalk from "chalk"
import config from "../config"
export default (app: Express): void => {
const webpack = require("webpack")
const webpackConfig = require("../../webpack/client.config").default
@ -22,7 +20,6 @@ export default (app: Express): void => {
)
instance.waitUntilValid(() => {
const url = `http://${config.HOST}:${config.PORT}`
console.info(chalk.green(`==> 🌎 Listening at ${url}`))
console.info(chalk.green("webpack-dev-middleware activated"))
})
}

View File

@ -6,10 +6,15 @@ import helmet from "helmet"
import hpp from "hpp"
import favicon from "serve-favicon"
import chalk from "chalk"
import * as http from "http"
import * as https from "https"
import * as fs from "fs"
import _ from "lodash"
import devServer from "./devServer"
import ssr from "./ssr"
import certbotRouter from "../routes/certbot"
import { jeuJavListGet } from "../gsheets/jeuJav"
import { envieListGet, envieAdd } from "../gsheets/envies"
import { membreGet, membreSet } from "../gsheets/membres"
@ -24,8 +29,13 @@ app.use(hpp())
// Compress all requests
app.use(compression())
// Https with certbot and Let's Encrypt
if (!__DEV__) {
app.use("/.well-known/acme-challenge", certbotRouter)
}
// Use for http request debug (show errors only)
app.use(logger("dev", { skip: (_, res) => res.statusCode < 400 }))
app.use(logger("dev", { skip: (_req, res) => res.statusCode < 400 }))
app.use(favicon(path.resolve(process.cwd(), "public/favicon.ico")))
app.use(express.static(path.resolve(process.cwd(), "public")))
@ -43,7 +53,62 @@ app.post("/EnvieAdd", envieAdd)
// Use React server-side rendering middleware
app.get("*", ssr)
// @ts-expect-error
app.listen(config.PORT, config.HOST, (error) => {
if (error) console.error(chalk.red(`==> 😭 OMG!!! ${error}`))
/**
* Create HTTP and HTTPS server.
*/
const servers = [{ protocol: "http", server: http.createServer(app) }]
interface Cert {
key: string
cert: string
}
const certPaths: Cert[] = [
{
// Prod
key: "/root/certbot/config/live/fo.parisestludique.fr/privkey.pem",
cert: "/root/certbot/config/live/fo.parisestludique.fr/fullchain.pem",
},
{
// Local
key: "../certbot/key.pem",
cert: "../certbot/cert.pem",
},
]
const validCertPath: Cert | undefined = certPaths.find((certPath: Cert) =>
_.every(certPath, (pemPath: string) => fs.existsSync(pemPath))
)
if (validCertPath) {
const httpsOptions = _.mapValues(validCertPath, (pemPath: string) => fs.readFileSync(pemPath))
servers.push({ protocol: "https", server: https.createServer(httpsOptions, app) })
}
/**
* Listen on provided port, on all network interfaces.
*/
servers.forEach(({ protocol, server }) => {
server.listen(protocol === "http" ? config.PORT : <number>config.PORT + 2)
server.on("error", onError)
server.on("listening", () => onListening(server))
})
/**
* Event listener for HTTP server 'error' event.
*/
function onError(error: any) {
if (error) {
console.error(chalk.red(`==> 😭 OMG!!! ${error}`))
}
}
/**
* Event listener for HTTP server 'listening' event.
*/
function onListening(server: any) {
const addr = server.address()
const bind = typeof addr === "string" ? `pipe ${addr}` : `port ${addr.port}`
console.error(chalk.green(`\nServer listening on ${bind}`))
}

View File

@ -23,6 +23,9 @@ const getPlugins = () => {
...plugins,
new webpack.HotModuleReplacementPlugin(),
new ReactRefreshWebpackPlugin({ overlay: { sockIntegration: "whm" } }),
new webpack.DefinePlugin({
process: "process/browser",
}),
]
if (!isDev)

View File

@ -1647,9 +1647,9 @@
"@types/istanbul-lib-report" "*"
"@types/jest@*":
version "27.0.2"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-27.0.2.tgz#ac383c4d4aaddd29bbf2b916d8d105c304a5fcd7"
integrity sha512-4dRxkS/AFX0c5XW6IPMNOydLn2tEhNhJV7DnYK+0bjoJZ+QTmfucBlihX7aoEsh/ocYtkLC73UbnBXBXIxsULA==
version "27.0.3"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-27.0.3.tgz#0cf9dfe9009e467f70a342f0f94ead19842a783a"
integrity sha512-cmmwv9t7gBYt7hNKH5Spu7Kuu/DotGa+Ff+JGRKZ4db5eh8PnKS4LuebJ3YLUoyOyIHraTGyULn23YtEAm0VSg==
dependencies:
jest-diff "^27.0.0"
pretty-format "^27.0.0"
@ -1737,9 +1737,9 @@
"@types/node" "*"
"@types/node@*":
version "16.11.7"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.7.tgz#36820945061326978c42a01e56b61cd223dfdc42"
integrity sha512-QB5D2sqfSjCmTuWcBWyJ+/44bcjO7VbjSbOE0ucoVbAsSNQc4Lt6QkgkVXkTDwkL4z/beecZNDvVX15D4P8Jbw==
version "16.11.9"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.9.tgz#879be3ad7af29f4c1a5c433421bf99fab7047185"
integrity sha512-MKmdASMf3LtPzwLyRrFjtFFZ48cMf8jmX5VRYrDQiJa8Ybu5VAmkqBWqKU8fdCwD8ysw4mQ9nrEHvzg6gunR7A==
"@types/normalize-package-data@^2.4.0":
version "2.4.1"
@ -1829,9 +1829,9 @@
"@types/react" "*"
"@types/react@*":
version "17.0.35"
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.35.tgz#217164cf830267d56cd1aec09dcf25a541eedd4c"
integrity sha512-r3C8/TJuri/SLZiiwwxQoLAoavaczARfT9up9b4Jr65+ErAUX3MIkU0oMOQnrpfgHme8zIqZLX7O5nnjm5Wayw==
version "17.0.36"
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.36.tgz#0d81e0e2419e6a8e9ba6af5e3a0608e70835d7d1"
integrity sha512-CUFUp01OdfbpN/76v4koqgcpcRGT3sYOq3U3N6q0ZVGcyeP40NUdVU+EWe3hs34RNaTefiYyBzOpxBBidCc5zw==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "*"
@ -2289,9 +2289,9 @@ ajv-keywords@^3.4.1, ajv-keywords@^3.5.2:
integrity sha512-5p6WTN0DdTGVQk6VjcEju19IgaHudalcfabD7yhDGeA6bcQnmL+CpveLJq/3hvfwd1aof6L386Ougkx6RfyMIQ==
ajv-keywords@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-5.0.0.tgz#d01b3b21715b2f63d02aa511b82fc6eb3b30083c"
integrity sha512-ULd1QMjRoH6JDNUQIfDLrlE+OgZlFaxyYCjzt58uNuUQtKXt8/U+vK/8Ql0gyn/C5mqZzUWtKMqr/4YquvTrWA==
version "5.1.0"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-5.1.0.tgz#69d4d385a4733cdbeab44964a1170a88f87f0e16"
integrity sha512-YCS/JNFAUyr5vAuhk1DWm1CBxRHW9LbJ2ozWeemrIqpbsqKjHVxYPyi5GC0rjZIT5JxJ3virVTS8wk4i/Z+krw==
dependencies:
fast-deep-equal "^3.1.3"
@ -2306,9 +2306,9 @@ ajv@^6.10.0, ajv@^6.12.2, ajv@^6.12.3, ajv@^6.12.4, ajv@^6.12.5:
uri-js "^4.2.2"
ajv@^8.0.0, ajv@^8.0.1, ajv@^8.8.0:
version "8.8.1"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.8.1.tgz#e73dd88eeb4b10bbcd82bee136e6fbe801664d18"
integrity sha512-6CiMNDrzv0ZR916u2T+iRunnD60uWmNn8SkdB44/6stVORUg0aAkWO7PkOhpCmjmW8f2I/G/xnowD66fxGyQJg==
version "8.8.2"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.8.2.tgz#01b4fef2007a28bf75f0b7fc009f62679de4abbb"
integrity sha512-x9VuX+R/jcFj1DHo/fCp99esgGDWiHENrKxaCENuCxpoMCmAt/COCGVDwA7kleEpEzJjDnvh3yGoOuLu0Dtllw==
dependencies:
fast-deep-equal "^3.1.1"
json-schema-traverse "^1.0.0"
@ -4040,9 +4040,9 @@ ee-first@1.1.1:
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=
electron-to-chromium@^1.3.896:
version "1.3.901"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.901.tgz#ce2c3157d61bce9f42f1e83225c17358ae9f4918"
integrity sha512-ToJdV2vzwT2jeAsw8zIggTFllJ4Kxvwghk39AhJEHHlIxor10wsFI3wo69p8nFc0s/ATWBqugPv/k3nW4Y9Mww==
version "1.3.904"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.904.tgz#52a353994faeb0f2a9fab3606b4e0614d1af7b58"
integrity sha512-x5uZWXcVNYkTh4JubD7KSC1VMKz0vZwJUqVwY3ihsW0bst1BXDe494Uqbg3Y0fDGVjJqA8vEeGuvO5foyH2+qw==
emittery@^0.8.1:
version "0.8.1"
@ -4322,9 +4322,9 @@ eslint-plugin-react-hooks@^4:
integrity sha512-XslZy0LnMn+84NEG9jSGR6eGqaZB3133L8xewQo3fQagbQuGt7a63gf+P1NGKZavEYEC3UXaWEAA/AqDkuN6xA==
eslint-plugin-react@^7.23.2:
version "7.27.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.27.0.tgz#f952c76517a3915b81c7788b220b2b4c96703124"
integrity sha512-0Ut+CkzpppgFtoIhdzi2LpdpxxBvgFf99eFqWxJnUrO7mMe0eOiNpou6rvNYeVVV6lWZvTah0BFne7k5xHjARg==
version "7.27.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.27.1.tgz#469202442506616f77a854d91babaae1ec174b45"
integrity sha512-meyunDjMMYeWr/4EBLTV1op3iSG3mjT/pz5gti38UzfM4OPpNc2m0t2xvKCOMU5D6FSdd34BIMFOvQbW+i8GAA==
dependencies:
array-includes "^3.1.4"
array.prototype.flatmap "^1.2.5"
@ -6925,9 +6925,9 @@ lilconfig@^2.0.3:
integrity sha512-bfTIN7lEsiooCocSISTWXkiWJkRqtL9wYtYy+8EK3Y41qh3mpwPU0ycTOgjdY9ErwXCc8QyrQp82bdL0Xkm9yA==
lines-and-columns@^1.1.6:
version "1.1.6"
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00"
integrity sha1-HADHQ7QzzQpOgHWPe2SldEDZ/wA=
version "1.2.4"
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632"
integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==
lint-staged@^11.0.0:
version "11.2.6"
@ -8592,6 +8592,11 @@ process-nextick-args@~2.0.0:
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"
integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==
process@^0.11.10:
version "0.11.10"
resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182"
integrity sha1-czIwDoQBYb2j5podHZGn1LwW8YI=
progress@^2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8"
@ -9509,9 +9514,9 @@ side-channel@^1.0.4:
object-inspect "^1.9.0"
signal-exit@^3.0.0, signal-exit@^3.0.2, signal-exit@^3.0.3:
version "3.0.5"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.5.tgz#9e3e8cc0c75a99472b44321033a7702e7738252f"
integrity sha512-KWcOiKeQj6ZyXx7zq4YxSMgHRlod4czeBQZrPb8OKcohcqAXShm7E20kEMle9WBt26hFcAf0qLOcp5zmY7kOqQ==
version "3.0.6"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.6.tgz#24e630c4b0f03fea446a2bd299e62b4a6ca8d0af"
integrity sha512-sDl4qMFpijcGw22U5w63KmD3cZJfBuFlVNbVMKje2keoKML7X2UzWbc4XrmEbDwg0NXJc3yv4/ox7b+JWb57kQ==
sirv@^1.0.7:
version "1.0.18"
@ -9590,9 +9595,9 @@ source-map-resolve@^0.6.0:
decode-uri-component "^0.2.0"
source-map-support@^0.5.19, source-map-support@^0.5.6, source-map-support@~0.5.20:
version "0.5.20"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.20.tgz#12166089f8f5e5e8c56926b377633392dd2cb6c9"
integrity sha512-n1lZZ8Ve4ksRqizaBQgxXDgKwttHDhyfQjA6YZZn8+AroHbsIz+JjwxQDxbp+7y5OYCI8t1Yk7etjD9CRd2hIw==
version "0.5.21"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f"
integrity sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
@ -10348,9 +10353,9 @@ ts-node@^10.0.0:
yn "3.1.1"
tsconfig-paths@^3.11.0, tsconfig-paths@^3.9.0:
version "3.11.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.11.0.tgz#954c1fe973da6339c78e06b03ce2e48810b65f36"
integrity sha512-7ecdYDnIdmv639mmDwslG6KQg1Z9STTz1j7Gcz0xa+nshh/gKDAHcPxRbWOsA3SPp0tXP2leTcY9Kw+NAkfZzA==
version "3.12.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.12.0.tgz#19769aca6ee8f6a1a341e38c8fa45dd9fb18899b"
integrity sha512-e5adrnOYT6zqVnWqZu7i/BQ3BnhzvGbjEjejFXO20lKIKpwTaupkCPgEfv4GZK1IBciJUEhYs3J3p75FdaTFVg==
dependencies:
"@types/json5" "^0.0.29"
json5 "^1.0.1"
@ -10822,9 +10827,9 @@ webpack-sources@^3.2.2:
integrity sha512-cp5qdmHnu5T8wRg2G3vZZHoJPN14aqQ89SyQ11NpGH5zEMDCclt49rzo+MaRazk7/UeILhAI+/sEtcM+7Fr0nw==
webpack@^5, webpack@^5.37.0, webpack@^5.43.0:
version "5.64.1"
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.64.1.tgz#fd59840c16f04fe315f2b2598a85026f12dfa1bb"
integrity sha512-b4FHmRgaaAjP+aVOVz41a9Qa5SmkUPQ+u8FntTQ1roPHahSComB6rXnLwc976VhUY4CqTaLu5mCswuHiNhOfVw==
version "5.64.2"
resolved "https://registry.yarnpkg.com/webpack/-/webpack-5.64.2.tgz#152e28d4712a6223b06c06cba0d3e622a61611a0"
integrity sha512-4KGc0+Ozi0aS3EaLNRvEppfZUer+CaORKqL6OBjDLZOPf9YfN8leagFzwe6/PoBdHFxc/utKArl8LMC0Ivtmdg==
dependencies:
"@types/eslint-scope" "^3.7.0"
"@types/estree" "^0.0.50"