Skip to content

Commit

Permalink
Add cors-anywhere
Browse files Browse the repository at this point in the history
# Conflicts:
#	docker/Dockerfile.dev
#	examples/frontend/angular/dist/casper/index.html
#	examples/frontend/angular/libs/components/src/lib/header/header.component.ts
  • Loading branch information
gRoussac committed Jan 25, 2025
1 parent ba02cc2 commit 9c1170a
Show file tree
Hide file tree
Showing 31 changed files with 7,942 additions and 7,350 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ docker-start-prod:
$(CICD_DC) build --build-arg BUILD_CONFIGURATION=docker
$(CICD_DC) up --remove-orphans -d

docker-stop-prod:
$(CICD_DC) stop

docker-deploy-prod:
rm webclient.tar
$(CICD_DC) build --build-arg BUILD_CONFIGURATION=production
Expand Down
37 changes: 11 additions & 26 deletions docker/Dockerfile.cicd
Original file line number Diff line number Diff line change
@@ -1,48 +1,33 @@
FROM --platform=linux/amd64 node:22.1.0-alpine3.19 as build
FROM --platform=linux/amd64 node:22.13-alpine as build

ENV NODE_ENV "production"
ENV NODE_ENV "development"
ARG BUILD_CONFIGURATION=production

WORKDIR /app

# Update npm to the latest version
RUN npm install -g npm@latest

COPY ./examples/frontend/angular .
COPY pkg pkg

RUN rm package-lock.json
RUN rm -rf node_modules package-lock.json
# Until pkg is npm copy files and change path to root
RUN sed -i 's|\"casper-sdk\": \"file:\(\.\.\/\)\+pkg\"|\"casper-sdk\": \"file:pkg\"|' package.json
RUN sed -i 's/"input": "\(\.\.\/\)\+pkg"/\"input": "pkg"/g' project.json

# optional
# Build proxy from template
RUN npm run build-proxy-conf
# Proxy localhost to gateway
RUN if [ "$BUILD_CONFIGURATION" = "docker" ]; then \
sed -i 's|http://localhost|http://172.17.0.1|g' proxy.conf.json; \
fi
RUN npm install --ignore-scripts --verbose && npm update

RUN npm install --verbose
RUN npm run build -- --configuration=$BUILD_CONFIGURATION --verbose

# Build for docker env and remove devDependencies
RUN npm run build -- --configuration=$BUILD_CONFIGURATION --verbose \
&& npm prune --production
FROM python:3.13-slim

FROM --platform=linux/amd64 node:22.1.0-alpine3.19
ENV PORT 8080

WORKDIR /app

COPY --from=build --chown=node:node /app/dist/casper ./dist/
COPY --from=build --chown=node:node /app/proxy.conf.json .

# Install express and http-proxy-middleware packages
RUN npm install express http-proxy-middleware

# Copy the custom server script
COPY ./docker/server-prod.js .

EXPOSE 8080

# Exposing another port needs server-prod.jsto get the exposed port
# ENV PORT 8080

CMD ["node", "server-prod.js"]
CMD ["python3", "-m", "http.server", "8080", "--directory", "/app/dist"]
15 changes: 7 additions & 8 deletions docker/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
FROM --platform=linux/amd64 node:22.1.0-alpine3.19
FROM --platform=linux/amd64 node:22.13-alpine as build

ENV NODE_ENV "development"

# /dev/rpc network adress (optional)
ENV RPC_ADDRESS "http://localhost:11101"

WORKDIR /app

# Update npm to the latest version
RUN npm install -g npm@latest

COPY ./examples/frontend/angular .

COPY pkg pkg

# Until pkg is npm copy files and change path to root
RUN rm package-lock.json
RUN rm -rf node_modules package-lock.json
RUN sed -i 's|\"casper-sdk\": \"file:\(\.\.\/\)\+pkg\"|\"casper-sdk\": \"file:pkg\"|' package.json
RUN sed -i 's/"input": "\(\.\.\/\)\+pkg"/\"input": "pkg"/g' project.json

RUN npm install --ignore-scripts --verbose && npm update

# Build proxy from template
RUN npm run build-proxy-conf
# Proxy localhost to gateway
RUN sed -i 's|http://localhost|http://172.17.0.1|g' proxy.conf.json

RUN npm install --verbose

CMD npm run serve -- --configuration=docker

EXPOSE 4200
23 changes: 23 additions & 0 deletions docker/cors-anywhere.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM node:14-alpine

# Set environment variables for production
ENV NODE_ENV=production
ENV NODE_PATH=/usr/local/lib/node_modules

# Optional: If you want to manage versions directly, use ARG
ARG version=latest

# Install dependencies globally
RUN npm install -g cors-anywhere@$version

# Copy the server.js file into the container
COPY ./docker/server.js /app/server.js

# Set the working directory for the container
WORKDIR /app

# Expose the port your app will run on
EXPOSE 11100

# Run the server when the container starts
CMD ["node", "server.js"]
36 changes: 32 additions & 4 deletions docker/docker-compose.cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,36 @@ services:
dockerfile: ./docker/Dockerfile.cicd
context: ../
ports:
- "8080:8080"
- '8080:8080'
networks:
- casper-network

# Build and run image with Dockerfile
# docker build -t casper-webclient . --force-rm
# docker container run -t -i --rm -h casper-webclient -p 4200:4200 casper-webclient
# Build and run image with Dockerfile
# docker build -t casper-webclient . --force-rm
# docker container run -t -i --rm -h casper-webclient -p 4200:4200 casper-webclient

cors-anywhere:
image: cors-anywhere:latest
container_name: cors-anywhere
build:
context: ../
dockerfile: ./docker/cors-anywhere.Dockerfile
tty: true
environment:
PORT: 11100

# Set allowed origins here with casper-webclient url
ORIGIN_WHITELIST: |
http://localhost:8080,
http://localhost:4200,
http://127.0.0.1:8080,
http://127.0.0.1:4200
ports:
- 11100:11100
networks:
- casper-network

networks:
casper-network:
external: false
8 changes: 4 additions & 4 deletions docker/docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ services:
dockerfile: ./docker/Dockerfile.dev
context: ../
ports:
- "4200:4200"
- '4200:4200'

# Build and run image with Dockerfile
# docker build -t casper-webclient . --force-rm
# docker container run -t -i --rm -h casper-webclient -p 4200:4200 casper-webclient
# Build and run image with Dockerfile
# docker build -t casper-webclient . --force-rm
# docker container run -t -i --rm -h casper-webclient -p 4200:4200 casper-webclient
31 changes: 0 additions & 31 deletions docker/server-prod.js

This file was deleted.

29 changes: 29 additions & 0 deletions docker/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Config from https://github.com/make-software/casper-nctl-docker/blob/master/cors/server.js

// Listen on a specific host via the HOST environment variable
var host = process.env.HOST || '0.0.0.0';
// Listen on a specific port via the PORT environment variable
var port = process.env.PORT || 11100;

const originWhitelist = (
process.env.ORIGIN_WHITELIST ||
`
http://localhost:8080,
http://localhost:4200,
http://127.0.0.1:8080,
http://127.0.0.1:4200
`
)
.replace(/\s+/g, '')
.split(',');

var cors_proxy = require('cors-anywhere');
cors_proxy
.createServer({
originWhitelist,
requireHeader: ['origin', 'x-requested-with'],
removeHeaders: ['cookie', 'cookie2'],
})
.listen(port, host, function () {
console.log('Running CORS Anywhere on ' + host + ':' + port);
});
2 changes: 1 addition & 1 deletion examples/desktop/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^33.2.0",
"electron": "^34.0.1",
"electron-builder": "^25.1.8",
"electron-updater": "^6.3.9"
},
Expand Down
4 changes: 2 additions & 2 deletions examples/desktop/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"casper-sdk": "file:../../../pkg-nodejs"
},
"devDependencies": {
"@types/node": "^22.9.1",
"typescript": "^5.6.3"
"@types/node": "^22.10.10",
"typescript": "^5.7.3"
}
}
8 changes: 4 additions & 4 deletions examples/frontend/angular/dist/casper/3rdpartylicenses.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
MIT
The MIT License

Copyright (c) 2010-2024 Google LLC. https://angular.dev/license
Copyright (c) 2010-2025 Google LLC. https://angular.dev/license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -27,7 +27,7 @@ THE SOFTWARE.
MIT
The MIT License

Copyright (c) 2010-2024 Google LLC. https://angular.dev/license
Copyright (c) 2010-2025 Google LLC. https://angular.dev/license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -52,7 +52,7 @@ THE SOFTWARE.
MIT
The MIT License

Copyright (c) 2010-2024 Google LLC. https://angular.dev/license
Copyright (c) 2010-2025 Google LLC. https://angular.dev/license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -77,7 +77,7 @@ THE SOFTWARE.
MIT
The MIT License

Copyright (c) 2010-2024 Google LLC. https://angular.dev/license
Copyright (c) 2010-2025 Google LLC. https://angular.dev/license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Binary file not shown.

Large diffs are not rendered by default.

This file was deleted.

4 changes: 2 additions & 2 deletions examples/frontend/angular/dist/casper/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en" data-critters-container>
<html lang="en" data-beasties-container>
<head>
<meta charset="utf-8">
<title>Casper WebClient</title>
Expand All @@ -11,5 +11,5 @@
<style>html,body{display:grid;height:100%;margin:0;padding:0}</style><link rel="stylesheet" href="styles.bae465c3fcf30099.css" media="print" onload="this.media='all'"><noscript><link rel="stylesheet" href="styles.bae465c3fcf30099.css"></noscript></head>
<body>
<app-root></app-root>
<script src="runtime.a201a20accf2fb4b.js" type="module"></script><script src="polyfills.b7ecd408d58033fa.js" type="module"></script><script src="main.e97e0daf969e59f0.js" type="module"></script></body>
<script src="runtime.5debcb79cff10eb2.js" type="module"></script><script src="polyfills.376204005635514d.js" type="module"></script><script src="main.c36795cc86bac906.js" type="module"></script></body>
</html>

Large diffs are not rendered by default.

This file was deleted.

Large diffs are not rendered by default.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class HeaderComponent implements AfterViewInit {
private window!: (Window & typeof globalThis) | null;
private is_electron!: boolean;
private is_production: boolean = this.env['production'] as unknown as boolean;
private localhost_to_gateway: boolean = this.env['localhost_to_gateway'] as unknown as boolean;
private is_docker: boolean = this.env['is_docker'] as unknown as boolean;

constructor(
@Inject(SDK_TOKEN) private readonly sdk: SDK,
Expand Down Expand Up @@ -145,20 +145,35 @@ export class HeaderComponent implements AfterViewInit {
return typeof this.window !== 'undefined' && window.location?.origin?.startsWith('file://');
}

// TODO Refacto with proxy-everywhere
private setRPCAddress() {
try {
if ((this.is_electron)) {
this.sdk.setRPCAddress(this.rpc_address);
this.sdk.setNodeAddress(this.node_address);
} else {
const network = this.networks.find(x => x.rpc_address == this.rpc_address);
if (this.is_production && !this.localhost_to_gateway && network && ['ntcl', 'node-launcher'].includes(network?.name)) {
this.sdk.setRPCAddress(this.rpc_address);
this.sdk.setNodeAddress(this.node_address);
} else {
network && this.sdk.setRPCAddress([this.window?.location?.href, network?.name].join(''));
}
} else if (!this.is_production && this.is_docker) {
this.sdk.setRPCAddress(this.rpc_address);
this.sdk.setNodeAddress(this.node_address);
}
else if (this.is_production && this.is_docker) {
this.sdk.setRPCAddress([
this.config['default_protocol'],
this.config['docker_gateway'],
':',
this.config['cors_anywhere_port'],
'/',
this.rpc_address.replace(this.config['localhost'] as string, this.config['docker_gateway'] as string)].join(''));

this.sdk.setNodeAddress([
this.config['docker_gateway'],
':',
this.config['cors_anywhere_port'],
'/',
this.node_address.replace(this.config['localhost'] as string, this.config['docker_gateway'] as string)].join(''));
}
else {
const network = this.networks.find(x => x.node_address == this.node_address);
network && this.sdk.setRPCAddress([this.window?.location?.href, network?.name].join(''));
network && this.sdk.setNodeAddress([this.window?.location?.href, network?.name].join(''));
}
}
catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions examples/frontend/angular/libs/util/config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ export const config: EnvironmentConfig = {
app_port: '4200',
default_port: '7777',
default_protocol: 'http://',
docker_gateway: '172.17.0.1',
cors_anywhere_port: '11100',
};
Loading

0 comments on commit 9c1170a

Please sign in to comment.