Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: .env.* support #40

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BIND_ADDRESS=localhost
PORT=8080
GOOGLE_CLOUD_PROJECT=excalidraw-json-dev
BUCKET_NAME=excalidraw-json-dev.appspot.com
ALLOW_ORIGINS=excalidraw.vercel.app,https://dai-shi.github.io,https://excalidraw.com,https://www.excalidraw.com,http://localhost:
API_ENDPOINT=storage.google.com
6 changes: 6 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BIND_ADDRESS=localhost
PORT=8080
GOOGLE_CLOUD_PROJECT=excalidraw-json
BUCKET_NAME=excalidraw-json.appspot.com
ALLOW_ORIGINS=excalidraw.vercel.app,https://dai-shi.github.io,https://excalidraw.com,https://www.excalidraw.com
API_ENDPOINT=storage.google.com
40 changes: 22 additions & 18 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@ import { nanoid } from "nanoid";
import favicon from "serve-favicon";
import * as path from "path";

const PROD = process.env.NODE_ENV === "production";

require("dotenv").config(
PROD
? { path: ".env.production" }
: { path: ".env.development" },
);

const PROJECT_NAME = process.env.GOOGLE_CLOUD_PROJECT || "excalidraw-json-dev";
const PROD = PROJECT_NAME === "excalidraw-json";
const LOCAL = process.env.NODE_ENV !== "production";
const BUCKET_NAME = PROD
? "excalidraw-json.appspot.com"
: "excalidraw-json-dev.appspot.com";
const BUCKET_NAME = process.env.BUCKET_NAME || 'excalidraw-json-dev.appspot.com';
const API_ENDPOINT = process.env.API_ENDPOINT || 'storage.google.com';

const FILE_SIZE_LIMIT = 2 * 1024 * 1024;
const storage = new Storage(
LOCAL
? {
PROD
? undefined : {
apiEndpoint: API_ENDPOINT,
projectId: PROJECT_NAME,
keyFilename: `${__dirname}/keys/${PROJECT_NAME}.json`,
}
: undefined
);

const bucket = storage.bucket(BUCKET_NAME);
const app = express();

let allowOrigins = [
"excalidraw.vercel.app",
"https://dai-shi.github.io",
"https://excalidraw.com",
"https://www.excalidraw.com",
];
if (!PROD) {
allowOrigins.push("http://localhost:");
let allowOrigins = ['http://localhost:'];

if (process.env.ALLOW_ORIGINS !== undefined) {
allowOrigins = process.env.ALLOW_ORIGINS.toString().split(',');
}

const corsGet = cors();
Expand Down Expand Up @@ -80,9 +81,10 @@ app.post("/api/v2/post/", corsPost, (req, res) => {
});

blobStream.on("finish", async () => {

res.status(200).json({
id,
data: `${LOCAL ? "http" : "https"}://${req.get("host")}/api/v2/${id}`,
data: `${req.protocol}://${req.get("host")}/api/v2/${id}`,
});
});

Expand All @@ -109,4 +111,6 @@ app.post("/api/v2/post/", corsPost, (req, res) => {
});

const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`http://localhost:${port}`));
const bind_address = process.env.BIND_ADDRESS || 'localhost';

app.listen(port, () => console.log(`http://${bind_address}:${port}`));
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"@google-cloud/storage": "5.16.1",
"cors": "2.8.5",
"dotenv": "10.0.0",
"express": "4.17.2",
"nanoid": "3.1.30",
"serve-favicon": "2.5.0"
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,11 @@ dot-prop@^5.2.0:
dependencies:
is-obj "^2.0.0"

dotenv@10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81"
integrity sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==

duplexify@^4.0.0, duplexify@^4.1.1:
version "4.1.2"
resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-4.1.2.tgz#18b4f8d28289132fa0b9573c898d9f903f81c7b0"
Expand Down