commit 17e952bdbd938f4a71683e54934985c054a93e3c Author: Andros Fenollosa Date: Thu Apr 1 09:48:35 2021 +0200 First commit diff --git a/Caddyfile b/Caddyfile new file mode 100644 index 0000000..6d6920d --- /dev/null +++ b/Caddyfile @@ -0,0 +1,11 @@ +http://api.localhost + +root * /usr/src/app/ + +@notStatic { + not path /static/* /media/* +} + +reverse_proxy @notStatic django:8000 + +file_server diff --git a/Dockerfiles/django/Dockerfile b/Dockerfiles/django/Dockerfile new file mode 100644 index 0000000..d97949c --- /dev/null +++ b/Dockerfiles/django/Dockerfile @@ -0,0 +1,21 @@ +FROM debian:stable-slim + +ENV PYTHONUNBUFFERED: 1 + +# set work directory +WORKDIR /usr/src/app + +# install software +RUN apt update +RUN apt install -y build-essential cron python3-pip gettext + +# install dependencies +RUN pip3 install --upgrade pip +COPY ./requirements.txt . +RUN pip3 install -r requirements.txt + +# launcher +COPY django-launcher.dev.sh /django-launcher.dev.sh +COPY django-launcher.pro.sh /django-launcher.pro.sh +RUN chmod +x /django-launcher.dev.sh +RUN chmod +x /django-launcher.pro.sh diff --git a/Dockerfiles/gulp/Dockerfile b/Dockerfiles/gulp/Dockerfile new file mode 100644 index 0000000..9a09217 --- /dev/null +++ b/Dockerfiles/gulp/Dockerfile @@ -0,0 +1,19 @@ +FROM debian:unstable-slim + +# set work directory +WORKDIR /usr/src/app + +# install software +RUN apt update +RUN apt -y upgrade +# dependencies +RUN apt install -y build-essential nodejs npm + +# gulp +RUN npm install -g gulp-cli + +# Add package.json +COPY package.json package.json + +# dependencies gulp +RUN npm i diff --git a/README.md b/README.md new file mode 100644 index 0000000..9090040 --- /dev/null +++ b/README.md @@ -0,0 +1,80 @@ +# Install + +## Django + +https://programadorwebvalencia.com/django-chat-usando-websockets-con-salas-y-async/ + +## Gulp + +```shell +npm i +``` + +Run. + +```shell +gulp dev +``` + +# Config + +## Change path templates + + +In `settings.py`. + +``` python +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] +``` + +Update `DIRS`. + +``` python +'DIRS': [str(BASE_DIR) + '/app/templates/'], +``` + +# Run development + +``` sh +docker-compose -f docker-compose.dev.yaml up +``` + +Now open: + +`http://api.localhost` + +### Other domains + +- Caddy: `http://api.localhost`. +- Gulp: `http://localhost:3000`. +- Django: `http://localhost:8000`. +- Mailhog: `http://localhost:8025`. +- Postgres: `localhost:5432`. + +### Bash Django + +``` shell +docker exec -it api_django_1 bash +``` + +# Run production + +``` sh +docker-compose up -d +``` + +Open `https://domain.com`. diff --git a/asgi.py b/asgi.py new file mode 100644 index 0000000..872bba4 --- /dev/null +++ b/asgi.py @@ -0,0 +1,11 @@ +import os + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings") +import django + +django.setup() + +from channels.routing import ProtocolTypeRouter + + +application = ProtocolTypeRouter({}) diff --git a/django-launcher.dev.sh b/django-launcher.dev.sh new file mode 100644 index 0000000..d328d64 --- /dev/null +++ b/django-launcher.dev.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +# Collect static files +echo "Collect static files" +python3 manage.py collectstatic --noinput + +# Apply database migrations +echo "Apply database migrations" +python3 manage.py makemigrations +python3 manage.py migrate + +# Start server +echo "Starting server" +uvicorn --host 0.0.0.0 --port 8000 --reload chapps.asgi:application diff --git a/django-launcher.pro.sh b/django-launcher.pro.sh new file mode 100644 index 0000000..2623dff --- /dev/null +++ b/django-launcher.pro.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +# Collect static files +echo "Collect static files" +python3 manage.py collectstatic --noinput + +# Apply database migrations +echo "Apply database migrations" +python3 manage.py makemigrations +python3 manage.py migrate + +# Start server +echo "Starting server" +uvicorn --host 0.0.0.0 --port 8000 chapps.asgi:application diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml new file mode 100644 index 0000000..f26d288 --- /dev/null +++ b/docker-compose.dev.yaml @@ -0,0 +1,66 @@ +version: '3.1' + +services: + + db: + image: postgres + restart: always + volumes: + - ./../postgres_data:/var/lib/postgresql/data + environment: + POSTGRES_DB: api + POSTGRES_PASSWORD: postgres + expose: + - 5432 + + django: + build: + context: . + dockerfile: ./Dockerfiles/django/Dockerfile + restart: always + entrypoint: /django-launcher.dev.sh + volumes: + - .:/usr/src/app/ + environment: + DEBUG: "True" + ALLOWED_HOSTS: "" + SECRET_KEY: "" + DB_HOST: db + DB_NAME: "" + DB_USER: "postgres" + DB_PASSWORD: "postgres" + DB_PORT: "5432" + DOMAIN: "api.localhost" + DOMAIN_URL: "http://api.localhost" + STATIC_URL: "/static/" + MEDIA_URL: "/media/" + EMAIL_HOST: "mailhog" + EMAIL_USE_TLS: "False" + EMAIL_PORT: "1025" + EMAIL_USER: "" + EMAIL_PASSWORD: "" + expose: + - 8000 + depends_on: + - db + + caddy: + image: caddy:alpine + restart: always + ports: + - 80:80 + - 443:443 + volumes: + - ./Caddyfile:/etc/caddy/Caddyfile + - ./../caddy_data:/data + - .:/usr/src/app/ + depends_on: + - django + + mailhog: + image: mailhog/mailhog:latest + restart: always + expose: + - 1025:1025 + ports: + - 8025:8025 diff --git a/docker-compose.pro.yaml b/docker-compose.pro.yaml new file mode 100644 index 0000000..1abe9ef --- /dev/null +++ b/docker-compose.pro.yaml @@ -0,0 +1,66 @@ +version: '3.1' + +services: + + db: + image: postgres + restart: always + volumes: + - ./../postgres_data:/var/lib/postgresql/data + environment: + POSTGRES_DB: guitarlions + POSTGRES_PASSWORD: postgres + expose: + - 5432 + + django: + build: . + restart: always + entrypoint: /django-launcher.pro.sh + volumes: + - .:/usr/src/app/ + environment: + DEBUG: "False" + ALLOWED_HOSTS: "" + SECRET_KEY: "secret" + DB_HOST: db + DB_NAME: "guitarlions" + DB_USER: "postgres" + DB_PASSWORD: "postgres" + DB_PORT: "5432" + DOMAIN: "" + DOMAIN_URL: "https://" + STATIC_URL: "/static/" + MEDIA_URL: "/media/" + EMAIL_USE_TLS: True + EMAIL_HOST: "" + EMAIL_USE_TLS: "True" + EMAIL_PORT: "2525" + EMAIL_USER: "" + EMAIL_PASSWORD: "" + expose: + - 8000 + depends_on: + - db + + caddy: + image: caddy:alpine + restart: always + ports: + - 80:80 + - 443:443 + volumes: + - .:/usr/src/app/ + - ./Caddyfile:/etc/caddy/Caddyfile + - ./../caddy_data:/data + depends_on: + - django + + gulp: + build: Dockerfiles/gulp + restart: always + command: gulp + volumes: + - .:/app/ + depends_on: + - caddy diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..e7e8673 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,97 @@ +//=== +// IMPORTS +//=== +const { src, dest, parallel, series, watch } = require('gulp'); +const browserSync = require('browser-sync').create(); +const clean = require('gulp-clean'); +const sass = require('gulp-sass'); +const sourcemaps = require('gulp-sourcemaps'); +const ts = require('gulp-typescript'); +const iconfont = require('gulp-iconfont'); + +//=== +// VARIABLES +//=== +const runTimestamp = Math.round(Date.now() / 1000); +const SRC_PATH = 'assets'; +const DEST_PATH = 'static'; + +//=== +// TASKS +//=== +function cleanDist() { + return src('static/*') + .pipe(clean()); +} + +// Static server with reload +function initBrowserSync(cb) { + browserSync.init({ + notify: false, + open: false, + proxy: 'http://localhost' + }); + return cb; +} + +// Compile SASS + sourcemaps +function sassCompile() { + return src([SRC_PATH + "/sass/desktop.sass", SRC_PATH + "/sass/mobile.sass"]) + .pipe(sourcemaps.init()) + .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError)) + .pipe(sourcemaps.write('.')) + .pipe(dest(DEST_PATH + '/css/')) + .pipe(browserSync.stream()) ; +} + +// Compile Typescript to JavaScript +function typescript() { + return src(SRC_PATH +'/ts/*.ts') + .pipe(ts({ + noImplicitAny: true, + outFile: 'main.js' + })) + .pipe(dest('static/js')) + .pipe(browserSync.stream()) ; +} + +// Copy images +function images() { + return src(SRC_PATH +'/img/**/*') + .pipe(dest(DEST_PATH + '/img/')) + .pipe(browserSync.stream()) ; +} + +// Build Font icons. *.svg to font-icons.woff2 +function buildFontIcons() { + return src(SRC_PATH + '/fonts/icons/*.svg') + .pipe(iconfont({ + fontName: 'font-icons', + prependUnicode: true, + formats: ['woff2'], + timestamp: runTimestamp + })) + .pipe(dest(DEST_PATH + '/fonts/')) + .pipe(browserSync.stream()) ; +} + +//=== +// Tasks +//=== + +// Default: 'gulp' +const build = series( + cleanDist, + parallel(sassCompile, typescript, images, buildFontIcons) +); +exports.default = build; + +// Dev: 'gulp dev' +exports.dev = function () { + build(); + watch([SRC_PATH + '/sass/*.sass', SRC_PATH + '/sass/**/*.sass'], sassCompile); + watch([SRC_PATH + '/ts/*.ts', SRC_PATH + '/ts/**/*.ts'], typescript); + watch([SRC_PATH + '/img/*', SRC_PATH + '/img/**/*'], images); + watch([SRC_PATH + '/fonts/icons/*.svg'], buildFontIcons); + initBrowserSync(); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..f0cb7f2 --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "web", + "version": "1.0.0", + "description": "", + "main": "gulpfile.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@gitlab.com/sapps-studio/....git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://gitlab.com/sapps-studio/.../issues" + }, + "homepage": "https://gitlab.com/sapps-studio/...#readme", + "dependencies": { + "browser-sync": "^2.26.14", + "gulp": "^4.0.2", + "gulp-clean": "^0.4.0", + "gulp-iconfont": "^11.0.0", + "gulp-sass": "^4.1.0", + "gulp-sourcemaps": "^3.0.0", + "gulp-typescript": "*" + } +} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a21eee7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,10 @@ +# Django +django +# Servidor para Django +uvicorn +# Conector para PostgreSQL +psycopg2-binary +# Channels +channels==2.4.0 +# Conector de Redis para Channels +channels_redis