Browse Source

Now defaulting to serve backend from /backend when using docker

If serving behind a reverse proxy you no longer need to proxy the backend, just the frontend.
Owen Diffey 3 years ago
parent
commit
1d0d7faa9e
7 changed files with 62 additions and 53 deletions
  1. 1 0
      .gitignore
  2. 5 6
      README.md
  3. 0 2
      backend/Dockerfile
  4. 26 32
      backend/config/template.json
  5. 21 8
      frontend/dev.nginx.conf
  6. 3 3
      frontend/dist/config/template.json
  7. 6 2
      musare.sh

+ 1 - 0
.gitignore

@@ -12,6 +12,7 @@ startMongo.cmd
 .db
 .redis
 *.rdb
+backups/
 
 npm-debug.log
 lerna-debug.log

+ 5 - 6
README.md

@@ -43,15 +43,14 @@ We currently only utilize 1 backend, 1 MongoDB server and 1 Redis server running
     | `mode` | Should be either `development` or `production`. No more explanation needed. |
     | `migration` | Should be set to true if you need to update DB documents to a newer version after an update. Should be false at all other times. |
     | `secret` | Whatever you want - used by express's session module. |
-    | `domain` | Should be the url where the site will be accessible from,usually `http://localhost` for non-Docker. |
-    | `serverDomain` | Should be the url where the backend will be accessible from, usually `http://localhost:8080` for non-Docker. |
-    | `serverPort` | Should be the port where the backend will listen on, should always be `8080` for Docker, and is recommended for non-Docker. |
+    | `domain` | Should be the url where the site will be accessible from, usually `http://localhost` for non-Docker. |
+    | `serverDomain` | Should be the url where the backend will be accessible from, usually `http://localhost/backend` for docker or `http://localhost:8080` for non-Docker. |
     | `serverPort` | Should be the port where the backend will listen on, should always be `8080` for Docker, and is recommended for non-Docker. |
     | `registrationDisabled` | If set to true, users can't register accounts. |
     | `apis.youtube.key`            | Can be obtained by setting up a [YouTube API Key](https://developers.google.com/youtube/v3/getting-started). You need to use the YouTube Data API v3, and create an API key. |
     | `apis.recaptcha.secret`       | Can be obtained by setting up a [ReCaptcha Site (v3)](https://www.google.com/recaptcha/admin). |
     | `apis.recaptcha.enabled`       | Keep at false to keep disabled. |
-    | `apis.github` | Can be obtained by setting up a [GitHub OAuth Application](https://github.com/settings/developers). You need to fill in some values to create the OAuth application. The homepage is the homepage of frontend. The authorization callback url is the backend url with `/auth/github/authorize/callback` added at the end. For example `http://localhost:8080/auth/github/authorize/callback`. |
+    | `apis.github` | Can be obtained by setting up a [GitHub OAuth Application](https://github.com/settings/developers). You need to fill in some values to create the OAuth application. The homepage is the homepage of frontend. The authorization callback url is the backend url with `/auth/github/authorize/callback` added at the end. For example `http://localhost/backend/auth/github/authorize/callback`. |
     | `apis.discogs` | Can be obtained by setting up a [Discogs application](https://www.discogs.com/settings/developers), or you can disable it. |
     | `smtp` | Can be obtained by setting up an SMTP server, using a provider such as [Mailgun](http://www.mailgun.com/), or you can disable it. |
     | `redis.url` | Should be left alone for Docker, and changed to `redis://localhost:6379/0` for non-Docker. |
@@ -67,8 +66,8 @@ We currently only utilize 1 backend, 1 MongoDB server and 1 Redis server running
 
     | Property | Description |
     | - | - |
-    | `apiDomain` | Should be the url where the backend will be accessible from, usually `http://localhost:8080` for non-Docker. |
-    | `websocketsDomain` | Should be the same as the `serverDomain`, except using the `ws://` protocol instead of `http://` and with `/ws` at the end. |
+    | `apiDomain` | Should be the url where the backend will be accessible from, usually `http://localhost/backend` for docker or `http://localhost:8080` for non-Docker. |
+    | `websocketsDomain` | Should be the same as the `apiDomain`, except using the `ws://` protocol instead of `http://` and with `/ws` at the end. |
     | `frontendDomain` | Should be the url where the frontend will be accessible from, usually `http://localhost` for docker or `http://localhost:80` for non-Docker. |
     | `frontendPort` | Should be the port where the frontend will be accessible from, should always be port `81` for Docker, and is recommended to be port `80` for non-Docker. |
     | `recaptcha.key` | Can be obtained by setting up a [ReCaptcha Site (v3)](https://www.google.com/recaptcha/admin). |

+ 0 - 2
backend/Dockerfile

@@ -10,6 +10,4 @@ ADD package.json /opt/package.json
 
 RUN npm install
 
-EXPOSE 80
-
 CMD npm run docker:dev

+ 26 - 32
backend/config/template.json

@@ -1,10 +1,10 @@
 {
 	"mode": "development",
-    "migration": false,
+	"migration": false,
 	"secret": "default",
 	"domain": "http://localhost",
 	"frontendPort": 80,
-	"serverDomain": "http://localhost:8080",
+	"serverDomain": "http://localhost/backend",
 	"serverPort": 8080,
 	"registrationDisabled": true,
 	"fancyConsole": true,
@@ -12,7 +12,7 @@
 		"youtube": {
 			"key": "",
 			"rateLimit": 500,
-            "requestTimeout": 5000
+			"requestTimeout": 5000
 		},
 		"recaptcha": {
 			"secret": "",
@@ -31,15 +31,13 @@
 	},
 	"cors": {
 		"origin": [
-			"http://localhost",
-			"http://192.168.99.100",
-			"http://dev.musare.com"
+			"http://localhost"
 		]
 	},
 	"smtp": {
 		"host": "smtp.mailgun.org",
 		"port": 587,
-		"auth" : {
+		"auth": {
 			"user": "",
 			"pass": ""
 		},
@@ -66,33 +64,29 @@
 		"captureJobs": []
 	},
 	"defaultLogging": {
-        "hideType": [
-            "INFO"
-        ],
-        "blacklistedTerms": [
-        ]
-    },
-    "customLoggingPerModule": {
-        // "cache": {
-        //     "hideType": [
-
-        //     ],
-        //     "blacklistedTerms": []
+		"hideType": [
+			"INFO"
+		],
+		"blacklistedTerms": []
+	},
+	"customLoggingPerModule": {
+		// "cache": {
+		//     "hideType": [
+		//     ],
+		//     "blacklistedTerms": []
 		// },
 		"migration": {
-            "hideType": [
-
-            ],
-            "blacklistedTerms": [
-                "Ran job",
-                "Running job",
-                "Queuing job",
-                "Pausing job",
-                "is queued",
-                "is re-queued",
-                "Requeing"
-            ]
-        }
+			"hideType": [],
+			"blacklistedTerms": [
+				"Ran job",
+				"Running job",
+				"Queuing job",
+				"Pausing job",
+				"is queued",
+				"is re-queued",
+				"Requeing"
+			]
+		}
 	},
 	"configVersion": 2
 }

+ 21 - 8
frontend/dev.nginx.conf

@@ -5,29 +5,42 @@ events {
 }
 
 http {
-    include       /etc/nginx/mime.types;
-    default_type  application/octet-stream;
+    include /etc/nginx/mime.types;
+    default_type application/octet-stream;
 
-    sendfile        off;
+    sendfile off;
 
     keepalive_timeout  65;
 
     server {
-        listen       80;
-        server_name  localhost;
+        listen 80;
+        server_name localhost;
 
         location / {
-            proxy_set_header X-Real-IP  $remote_addr;
+            proxy_set_header X-Real-IP $remote_addr;
 			proxy_set_header X-Forwarded-For $remote_addr;
 			proxy_set_header Host $host;
 
+            proxy_http_version 1.1;
+			proxy_set_header Upgrade $http_upgrade;
+			proxy_set_header Connection "upgrade";
+            proxy_redirect off;
+
 			proxy_pass http://localhost:81; 
+        }
 
-			proxy_redirect off;
+        location /backend {
+            proxy_set_header X-Real-IP  $remote_addr;
+			proxy_set_header X-Forwarded-For $remote_addr;
+			proxy_set_header Host $host;
 
-			proxy_http_version 1.1;
+            proxy_http_version 1.1;
 			proxy_set_header Upgrade $http_upgrade;
 			proxy_set_header Connection "upgrade";
+            proxy_redirect off;
+
+            rewrite ^/backend/?(.*) /$1 break;
+			proxy_pass http://backend:8080; 
         }
     }
 }

+ 3 - 3
frontend/dist/config/template.json

@@ -3,11 +3,11 @@
 		"key": "",
 		"enabled": false
 	},
-	"apiDomain": "http://localhost:8080",
-	"websocketsDomain": "ws://localhost:8080/ws",
+	"apiDomain": "http://localhost/backend",
+	"websocketsDomain": "ws://localhost/backend/ws",
 	"frontendDomain": "http://localhost",
 	"frontendPort": "81",
-  	"cookie": {
+	"cookie": {
 		"domain": "localhost",
 		"secure": false,
 		"SIDname": "SID"

+ 6 - 2
musare.sh

@@ -185,8 +185,12 @@ if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
         echo -e "${CYAN}Musare | Backup${NC}"
         if [[ -f .env ]]; then
             source .env
-            echo -e "${YELLOW}Creating backup at ${PWD}/musare-$(date +"%Y-%m-%d-%s").dump${NC}"
-            docker-compose exec -T mongo sh -c "mongodump --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} -d musare --archive" > musare-$(date +"%Y-%m-%d-%s").dump
+            if [[ ! -d "${scriptLocation%x}/backups" ]]; then
+                echo -e "${YELLOW}Creating backup directory at ${scriptLocation%x}/backups${NC}"
+                mkdir "${scriptLocation%x}/backups"
+            fi
+            echo -e "${YELLOW}Creating backup at ${scriptLocation%x}/backups/musare-$(date +"%Y-%m-%d-%s").dump${NC}"
+            docker-compose exec -T mongo sh -c "mongodump --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} -d musare --archive" > "${scriptLocation%x}/backups/musare-$(date +"%Y-%m-%d-%s").dump"
         else
             echo -e "${RED}Error: .env does not exist${NC}"
         fi