Merge pull request #8 from codex-storage/Refactor_nginx

Refactor nginx
This commit is contained in:
Shaun Orssaud 2023-11-02 16:21:31 +09:00 committed by GitHub
commit bab13bf214
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 32 additions and 253 deletions

View File

@ -1,9 +0,0 @@
FROM python:3.9
WORKDIR /api
COPY api/requirements.txt api/api.py ./
RUN pip install -r ./requirements.txt
ENV FLASK_ENV production
EXPOSE 5000
CMD ["gunicorn", "-b", ":5000", "api:app"]

View File

@ -2,7 +2,7 @@
FROM node:18 as build-step FROM node:18 as build-step
WORKDIR /frontend WORKDIR /frontend
ENV PATH /frontend/node_modules/.bin:$PATH ENV PATH /frontend/node_modules/.bin:$PATH
COPY frontend/package.json frontend/yarn.lock frontend/tsconfig.json frontend/.env ./ COPY frontend/package.json frontend/yarn.lock frontend/tsconfig.json ./
COPY frontend/src ./src COPY frontend/src ./src
COPY frontend/public ./public COPY frontend/public ./public
RUN yarn install RUN yarn install
@ -11,4 +11,6 @@ RUN yarn build --production
# Build step #2: build an nginx container # Build step #2: build an nginx container
FROM nginx:stable-alpine FROM nginx:stable-alpine
COPY --from=build-step /frontend/build /usr/share/nginx/html COPY --from=build-step /frontend/build /usr/share/nginx/html
COPY deployment/nginx.default.conf /etc/nginx/conf.d/default.conf COPY deployment/nginx.template /etc/nginx/nginx.template
CMD ["nginx", "-g", "daemon off;"]

160
api/.gitignore vendored
View File

@ -1,160 +0,0 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

View File

@ -1,42 +0,0 @@
from flask import Flask, request, jsonify
from flask_cors import CORS
import requests
app = Flask(__name__)
CORS(app)
@app.route('/')
def root():
return jsonify({'message': 'Welcome to the API!'})
@app.route('/upload', methods=['POST'])
def upload():
print(request.headers.get('Content-Type'))
if request.headers.get('Content-Type') == 'application/octet-stream':
bytes = request.data
base_url = request.headers.get('Base-Url')
auth_string = request.headers.get('Auth-String')
# print(request.data)
if auth_string is not None:
auth = tuple(auth_string.split(':'))
else:
auth = None
response = requests.post(
f'{base_url}/api/codex/v1/upload',
data=bytes,
headers={
'Content-Type': 'application/octet-stream'
},
auth=auth
)
print(response.status_code)
print(response.text)
return jsonify({'cid': response.text.strip()})
else:
return jsonify({'message': 'Error!'})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)

View File

@ -1,4 +0,0 @@
requests
flask
gunicorn
flask-cors

View File

@ -1,22 +0,0 @@
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
error_page 500 502 503 504 /50x.html;
location / {
try_files $uri /index.html;
add_header Cache-Control "no-cache";
}
location /static {
expires 1y;
add_header Cache-Control "public";
}
location /uploads {
proxy_pass http://localhost:5000;
}
}

21
deployment/nginx.template Normal file
View File

@ -0,0 +1,21 @@
server
{
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html;
error_page 500 502 503 504 /50x.html;
location /api {
proxy_pass $codex_url;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
try_files $uri /index.html;
add_header Cache-Control "no-cache";
}
}

View File

@ -1,10 +1,4 @@
services: services:
api:
build:
context: .
dockerfile: Dockerfile.api
image: codex-frontend-api
network_mode: "host"
client: client:
build: build:
context: . context: .
@ -12,3 +6,7 @@ services:
image: codex-frontend-client image: codex-frontend-client
ports: ports:
- "3000:80" - "3000:80"
environment:
- codex_url=http://host.docker.internal:8080
volumes:
- ./deployment/nginx.template:/etc/nginx/templates/10-variables.conf.template:ro

View File

@ -1 +0,0 @@
REACT_APP_CODEX_URL=http://localhost:8080

View File

@ -18,7 +18,6 @@
"@types/react": "^18.2.25", "@types/react": "^18.2.25",
"@types/react-dom": "^18.2.11", "@types/react-dom": "^18.2.11",
"axios": "^1.5.1", "axios": "^1.5.1",
"dotenv": "^16.3.1",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",

View File

@ -15,16 +15,13 @@ function DebugPage() {
const [statusInfo, setStatusInfo] = React.useState< const [statusInfo, setStatusInfo] = React.useState<
DebugNodeInfoModel | undefined DebugNodeInfoModel | undefined
>(); >();
useEffect(() => { useEffect(() => {
axios axios
.get( .get(
`${ `/api/codex/v1/debug/info`,
nodeInfo.nodeToConnectTo || nodeInfo.baseUrl
}/api/codex/v1/debug/info`,
{ {
headers: headers:
(nodeInfo.auth !== null && { (nodeInfo.auth && {
Authorization: Authorization:
(nodeInfo.auth && "Basic " + btoa(nodeInfo.auth)) || "", (nodeInfo.auth && "Basic " + btoa(nodeInfo.auth)) || "",
}) || }) ||

View File

@ -28,7 +28,7 @@ export const useDexyStore = create<DexyState>()(
ftdCid: "", ftdCid: "",
setFtdCid: (cid) => set({ ftdCid: cid }), setFtdCid: (cid) => set({ ftdCid: cid }),
nodeInfo: { nodeInfo: {
baseUrl: process.env.REACT_APP_CODEX_URL || "http://localhost:8080", baseUrl: "http://host.docker.internal:8080",
nodeToConnectTo: null, nodeToConnectTo: null,
id: null, id: null,
// ip: null, // ip: null,