-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from DIG-Network/release/v0.0.1-alpha.20
Release/v0.0.1 alpha.20
- Loading branch information
Showing
4 changed files
with
56 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,56 @@ | ||
import express from "express"; | ||
import { storeRoutes } from "./routes"; | ||
import cookieParser from "cookie-parser"; | ||
import cors from "cors"; | ||
import express from "express"; | ||
import https from "https"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { storeRoutes } from "./routes"; | ||
import { Tls } from "chia-server-coin"; | ||
|
||
const caCertPath = path.join(__dirname, "ssl", "ca", "chia_ca.crt"); | ||
const caKeyPath = path.join(__dirname, "ssl", "ca", "chia_ca.key"); | ||
|
||
const serverCertPath = path.join(__dirname, "ssl", "dig", "server.cert"); | ||
const serverKeyPath = path.join(__dirname, "ssl", "dig", "server.key"); | ||
|
||
if (!fs.existsSync(caCertPath) || !fs.existsSync(caKeyPath)) { | ||
throw new Error("CA certificate or key not found."); | ||
} | ||
|
||
// Ensure the directory for server certificate and key exists | ||
const serverDir = path.dirname(serverCertPath); | ||
if (!fs.existsSync(serverDir)) { | ||
fs.mkdirSync(serverDir, { recursive: true }); | ||
} | ||
|
||
if (!fs.existsSync(serverCertPath) || !fs.existsSync(serverKeyPath)) { | ||
// Ensure that the Tls class will generate certs correctly, signed by your CA. | ||
new Tls(serverCertPath, serverKeyPath); | ||
console.log("Server certificate and key generated successfully."); | ||
} | ||
|
||
const caCert = fs.readFileSync(caCertPath); | ||
const serverCert = fs.readFileSync(serverCertPath); | ||
const serverKey = fs.readFileSync(serverKeyPath); | ||
|
||
const app = express(); | ||
const PORT = process.env.PORT || 3000; | ||
const PORT = Number(process.env.PORT) || 4161; | ||
|
||
// Apply store routes | ||
app.use(cookieParser()); | ||
app.use(cors()); | ||
|
||
app.use("/", storeRoutes); | ||
|
||
app.use((req, res, next) => { | ||
res.setHeader("Referrer-Policy", "same-origin"); | ||
next(); | ||
}); | ||
const serverOptions = { | ||
key: serverKey, | ||
cert: serverCert, | ||
ca: caCert, | ||
requestCert: true, // Require client certificate | ||
rejectUnauthorized: true, // Reject unauthorized clients | ||
}; | ||
|
||
// Create the HTTPS server | ||
const server = https.createServer(serverOptions, app); | ||
|
||
export { app, PORT }; | ||
// Export both the app and the server | ||
export { app, server, PORT }; |