From f9acc172630c39cac221db7aef48e6a3fccbdeea Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sat, 18 Dec 2021 00:03:22 +0100 Subject: [PATCH] Updated Dockerfile to build frontend --- .dockerignore | 2 ++ Dockerfile | 21 +++++++++++++++++ default.conf.template | 25 ++++++++++++++++----- matrix.conf.template | 52 +++++++++++++++++++++++++++++++++++++++++++ nginx.conf | 31 ++++++++++++++++++++++++++ src/pages/index.astro | 6 +++-- 6 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 .dockerignore create mode 100644 matrix.conf.template create mode 100644 nginx.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b947077 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +node_modules/ +dist/ diff --git a/Dockerfile b/Dockerfile index 3c79d8b..9b3830b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,24 @@ +# ======Building the project===== +FROM node:17.2.0 AS builder + +WORKDIR /usr/src/app + +# Install Node dependencies +COPY package.json yarn.lock ./ +RUN yarn install + +# Copy over source code & build project +COPY astro.config.mjs tsconfig.json ./ +COPY src/ ./src +RUN yarn run build + + +# =====Packaging inside an Nginx container===== FROM nginx:1.21.4-alpine +# Copy over the Nginx config files +COPY nginx.conf /etc/nginx/nginx.conf COPY default.conf.template /etc/nginx/templates/default.conf.template + +# Copy over build artifacts +COPY --from=builder /usr/src/app/dist /usr/share/nginx/html diff --git a/default.conf.template b/default.conf.template index f91f8cb..31e2d37 100644 --- a/default.conf.template +++ b/default.conf.template @@ -1,14 +1,15 @@ # vim: ft=nginx server { - listen 80; + listen 80; listen [::]:80; + + # =====MATRIX WELL-KNOWN FILES===== # Used for server federation - location ~^/\.well-known/matrix/server$ { + location = /.well-known/matrix/server { charset utf-8; default_type application/json; - if ($request_method = 'GET') { add_header Access-Control-Allow-Origin '*'; add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS'; @@ -28,11 +29,10 @@ server { return 405; } - location ~^/\.well-known/matrix/client$ { + location = /.well-known/matrix/client { charset utf-8; default_type application/json; - if ($request_method = 'GET') { add_header Access-Control-Allow-Origin '*'; add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS'; @@ -51,4 +51,19 @@ server { return 405; } + + + # =====FRONTEND HOSTING===== + location / { + root /usr/share/nginx/html; + index index.html; + } + + error_page 404 /404.html; + + # redirect server error pages to the static page /50x.html + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } } diff --git a/matrix.conf.template b/matrix.conf.template new file mode 100644 index 0000000..577cc3e --- /dev/null +++ b/matrix.conf.template @@ -0,0 +1,52 @@ +# vim: ft=nginx +server { + listen 80; + listen [::]:80; + + # Used for server federation + location = /.well-known/matrix/server { + charset utf-8; + default_type application/json; + + if ($request_method = 'GET') { + add_header Access-Control-Allow-Origin '*'; + add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS'; + add_header Access-Control-Allow-Headers 'X-Requested-With, Content-Type, Authorization'; + + return 200 '{"m.server":"${MATRIX_SERVER}"}'; + } + + if ($request_method = 'OPTIONS') { + add_header Access-Control-Allow-Origin '*'; + add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS'; + add_header Access-Control-Allow-Headers 'X-Requested-With, Content-Type, Authorization'; + add_header 'Content-Length' 0; + return 204; + } + + return 405; + } + + location = /.well-known/matrix/client { + charset utf-8; + default_type application/json; + + if ($request_method = 'GET') { + add_header Access-Control-Allow-Origin '*'; + add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS'; + add_header Access-Control-Allow-Headers 'X-Requested-With, Content-Type, Authorization'; + + return 200 '{"m.homeserver":{"base_url":"${MATRIX_CLIENT_SERVER}"}}'; + } + + if ($request_method = 'OPTIONS') { + add_header Access-Control-Allow-Origin '*'; + add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS'; + add_header Access-Control-Allow-Headers 'X-Requested-With, Content-Type, Authorization'; + add_header 'Content-Length' 0; + return 204; + } + + return 405; + } +} diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..69a637f --- /dev/null +++ b/nginx.conf @@ -0,0 +1,31 @@ +user nginx; +worker_processes auto; + +error_log /var/log/nginx/error.log notice; +pid /var/run/nginx.pid; + + +events { + worker_connections 1024; +} + + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + + keepalive_timeout 65; + + gzip on; + + # This order is important, as the Matrix matches should be evaluated first + include /etc/nginx/conf.d/default.conf; +} diff --git a/src/pages/index.astro b/src/pages/index.astro index 031be20..76802cb 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -1,6 +1,8 @@ - - + + + +

Hi!