diff --git a/.gitignore b/.gitignore index 43c6bbc..b9e0919 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ **npm-debug.log **.env* +**.pem* /client/node_modules/ diff --git a/Makefile b/Makefile index 9e9b1ae..5cd6f87 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,24 @@ deploy: make bundle git subtree push --prefix client/public origin gh-pages +# Generate self-signed key and certificate +.PHONY: https +https: + openssl req -x509 -newkey rsa:4096 -keyout config/key.pem -out config/cert.pem -days 365 -nodes + +# Run NGINX container in daemon mode +# KNOWN ISSUE: Docker for Mac/Windows runs in VM --network host will not work as expected +# Replace --network host with -p 80:80 and -p 443:443 when in development +.PHONY: nginx +nginx: + docker run -d --rm \ + --name bumper_nginx \ + -v $(PWD)/config/nginx.conf:/etc/nginx/nginx.conf \ + -v $(PWD)/config/cert.pem:/etc/nginx/ssl/nginx.crt \ + -v $(PWD)/config/key.pem:/etc/nginx/ssl/nginx.key \ + --network host \ + nginx:alpine + # Build and run Bumper in daemon mode .PHONY: bumper DATABASE_URL=https://bumperdevdb.firebaseio.com @@ -28,7 +46,7 @@ bumper: -e DATABASE_URL=$(DATABASE_URL) \ -e PORT=$(SERVER_PORT) \ -v $(PWD)/client/public:/app/build \ - -p 80:$(SERVER_PORT) \ + -p $(SERVER_PORT):$(SERVER_PORT) \ bumper # Starts a file server in the web/ directory diff --git a/config/nginx.conf b/config/nginx.conf new file mode 100644 index 0000000..a7b60c1 --- /dev/null +++ b/config/nginx.conf @@ -0,0 +1,58 @@ +user nginx; + +worker_processes auto; +worker_rlimit_nofile 8192; + +events { + worker_connections 8000; + multi_accept on; + use epoll; +} + +error_log /var/log/nginx/error.log warn; + +pid /var/run/nginx.pid; + +http { + gzip on; + gzip_http_version 1.0; + gzip_comp_level 5; + gzip_min_length 256; + gzip_proxied any; + gzip_vary on; + gzip_types + application/atom+xml + application/javascript + application/json + application/rss+xml + application/vnd.ms-fontobject + application/x-font-ttf + application/x-web-app-manifest+json + application/xhtml+xml + application/xml + font/opentype + image/svg+xml + image/x-icon + text/css + text/plain + text/x-component; + + server { + root /usr/share/nginx/html; + + listen 80; + listen [::]:80; + + listen 443 ssl; + + server_name localhost; + ssl_certificate /etc/nginx/ssl/nginx.crt; + ssl_certificate_key /etc/nginx/ssl/nginx.key; + + # KNOWN ISSUE - Docker for Mac/Windows runs in VM --network host will not work as expected + # Change localhost below to your machine's IP + location / { + proxy_pass http://localhost:9090/; + } + } +} \ No newline at end of file diff --git a/server/main.go b/server/main.go index 11e100e..52d049f 100644 --- a/server/main.go +++ b/server/main.go @@ -29,19 +29,19 @@ func getLobby(w http.ResponseWriter, r *http.Request) { func main() { rand.Seed(time.Now().UTC().UnixNano()) + PORT := os.Getenv("PORT") + if PORT == "" { + PORT = "9090" + } + arena.MessageChannel = make(chan models.Message) game := game.CreateGame() - // database.ConnectDB("service-account.json") - // if database.DBC == nil { - // log.Println("DBClient not initialized correctly") - // } - http.Handle("/", http.FileServer(http.Dir("./build"))) http.HandleFunc("/start", getLobby) http.Handle("/connect", game) game.StartGame() - log.Println("Starting server on localhost:" + os.Getenv("PORT")) - log.Println(http.ListenAndServe(":"+os.Getenv("PORT"), nil)) + log.Println("Starting server on localhost:" + PORT) + log.Println(http.ListenAndServe(":"+PORT, nil)) }