diff --git a/.env b/.env index 5d38e84..b96a9d8 100644 --- a/.env +++ b/.env @@ -1,4 +1,5 @@ -PUBLIC_URL=/spot/oxo2 +PUBLIC_URL=/ +OXO_DEV_BACKEND_PROXY_URL=http://localhost:8008/ REACT_APP_SSSOM_API=http://66.29.128.20:8008 REACT_APP_SSSOM_HOME=https://mapping-commons.github.io/sssom/spec/ diff --git a/Caddyfile b/Caddyfile index c92bceb..34def2b 100644 --- a/Caddyfile +++ b/Caddyfile @@ -4,7 +4,7 @@ :8080 { handle_path {$PUBLIC_URL}* { - root * /opt/oxo2/build + root * /opt/oxo2/dist try_files {path} / file_server } diff --git a/Dockerfile b/Dockerfile index f86eb7c..e747560 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,8 +21,8 @@ RUN mkdir /opt/oxo2 WORKDIR /opt/oxo2 -COPY package.json yarn.lock .yarnrc.yml /opt/oxo2/ -RUN yarn install +COPY package.json package-lock.json /opt/oxo2/ +RUN npm install COPY . /opt/oxo2/ diff --git a/build.mjs b/build.mjs new file mode 100644 index 0000000..fc57e4b --- /dev/null +++ b/build.mjs @@ -0,0 +1,57 @@ +import { exec } from "child_process"; +import { build } from "esbuild"; +import fs from "fs"; + +let define = {}; +for (const k in process.env) { + define[`process.env.${k}`] = JSON.stringify(process.env[k]); +} + +/// +/// Build index.html (simple find and replace) +/// +console.log("### Building index.html"); +fs.writeFileSync( + "dist/index.html", + fs + .readFileSync("index.html.in") + .toString() + .split("%PUBLIC_URL%/") + .join(process.env.PUBLIC_URL || "/") + .split("%PUBLIC_URL%") + .join(process.env.PUBLIC_URL || "/") +); + +/// +/// Build bundle.js (esbuild) +/// +console.log("### Building bundle.js"); +build({ + entryPoints: ["src/index.tsx"], + bundle: true, + platform: "browser", + outfile: "dist/bundle.js", + define, + plugins: [], + logLevel: "info", + sourcemap: "linked", + + ...(process.env.OXO_MINIFY === "true" + ? { + minify: true, + } + : {}), +}); + +/// +/// Build styles.css (tailwind) +/// +console.log("### Building styles.css"); +exec("tailwind -i ./src/index.css -o ./dist/styles.css"); + +/// +/// Copy files +/// +// console.log("### Copying misc files"); +// exec("cp ./src/banner.txt ./dist"); // home page banner text + diff --git a/dev_server.mjs b/dev_server.mjs new file mode 100644 index 0000000..1279e82 --- /dev/null +++ b/dev_server.mjs @@ -0,0 +1,46 @@ + +import express from 'express' +import fetch from 'node-fetch' +import urlJoin from 'url-join' +import nocache from 'nocache' + + +let server = express() + +server.use(nocache()) + +if(process.env.OXO_DEV_BACKEND_PROXY_URL === undefined) { + throw new Error('please set OXO_DEV_BACKEND_PROXY_URL before running dev server') +} +server.use(/^\/api.*/, async (req, res) => { + let backendUrl = urlJoin(process.env.OXO_DEV_BACKEND_PROXY_URL, req.originalUrl) + console.log('forwarding api request to: ' + backendUrl) + try { + let apiResponse = await fetch(backendUrl, { + redirect: 'follow', + method: req.method, + body: req.body + }) + res.header('content-type', apiResponse.headers.get('content-type')) + res.status(apiResponse.status) + apiResponse.body.pipe(res) + } catch(e) { + console.log(e) + } +}) + + +server.use(express.static('dist')) + +server.get(/^(?!\/api).*$/, (req, res) => { + res.sendFile(process.cwd() + '/dist/index.html') +}) + + + + +server.listen(3000) + + + + diff --git a/public/embl-ebi-background.jpg b/dist/embl-ebi-background.jpg similarity index 100% rename from public/embl-ebi-background.jpg rename to dist/embl-ebi-background.jpg diff --git a/public/favicon.ico b/dist/favicon.ico similarity index 100% rename from public/favicon.ico rename to dist/favicon.ico diff --git a/public/logo.png b/dist/logo.png similarity index 100% rename from public/logo.png rename to dist/logo.png diff --git a/public/manifest.json b/dist/manifest.json similarity index 100% rename from public/manifest.json rename to dist/manifest.json diff --git a/public/robots.txt b/dist/robots.txt similarity index 100% rename from public/robots.txt rename to dist/robots.txt diff --git a/entrypoint.dockersh b/entrypoint.dockersh index 9ef7bf3..b77bb61 100644 --- a/entrypoint.dockersh +++ b/entrypoint.dockersh @@ -2,8 +2,10 @@ cd /opt/oxo2 -echo Building frontend. PUBLIC_URL: $PUBLIC_URL +echo Building frontend. REACT_APP_ENV: $REACT_APP_ENV -yarn react-scripts build +npm run build caddy run --config ./Caddyfile + + diff --git a/public/index.html b/index.html.in similarity index 93% rename from public/index.html rename to index.html.in index ee93acf..b076c50 100644 --- a/public/index.html +++ b/index.html.in @@ -3,6 +3,7 @@
+ + +