selene-bananas

module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2021 License: MIT

README

selene-bananas

Docker Image CI Go Report Card GoDoc

A Banagrams clone

A tile-based word-forming game based on the popular Banagrams game. https://bananagrams.com/games/bananagrams

With WebSockets, users can play a word game together over a network.

Uses WebAssembly to manage browser logic.

Dependencies

New dependencies are automatically added to go.mod when the project is built.

  • pq provides the Postgres driver for storing user passwords and points
  • Gorilla WebSocket are used for bidirectional communication between users and the server
  • jwt-go is used for stateless web sessions
  • crypto is used to encrypt passwords with bcrypt
  • Font-Awesome provides the "copyright", "github," "linkedin", and "gavel" icons on the about page; they were copied from version 5.13.0 to resources/template/fa.

Build

Go 1.16 is used to build the application.

Make is used to by Makefile to build and runs the application. Run make without any arguments to build the server with the client and other resources embedded in it. This will likely need to be done before using an IDE in order to generate some files and populate the embedded filesystem used by the the server.

Aspell is used to generate the en_US dictionary to validate words on player boards.

  • Note: An integration test depends on aspell-en 2020.12.07-0. This version is used by Docker. Follow the steps below to install the version on your computer:
    1. Download https://ftp.gnu.org/gnu/aspell/dict/en/aspell6-en-2020.12.07-0.tar.bz2
    2. unzip the archive with tar -xf aspell6-en-2020.12.07-0.tar.bz2
    3. configure and install it:
    cd aspell6-en-2020.12.07-0
    ./configure
    make
    sudo make install
    

Node is needed to run WebAssembly tests.

Run make serve to build and run the application.

Docker

Launching the application with Docker requires minimal configuration.

  1. Install docker-compose
  2. Set environment variables in the .env file in project root (next to Dockerfile).
    PORT=8000
    NO_TLS_REDIRECT=true
    HTTP_PORT=8001
    HTTPS_PORT=8000
    
    DATABASE_URL=postgres://selene:selene123@127.0.0.1:5432/selene_bananas_db
    POSTGRES_DB=selene_bananas_db
    POSTGRES_USER=selene
    POSTGRES_PASSWORD=selene123
    POSTGRES_PORT=54320
    
  3. Run docker-compose up --build to launch the application, rebuilding parts of it that are stale.
  4. Access application by opening http://127.0.0.1:8000. TLS certificates will be copied to Docker. Environment variables are used from the .env file.
Environment Configuration

Environment variables in the .env file are needed to customize the server.

Minimal config:

DATABASE_URL=postgres://selene:selene123@127.0.0.1:54320/selene_bananas_db?sslmode=disable
PORT=8000
NO_TLS_REDIRECT=true

For development, set CACHE_SECONDS to 0 to not cache static and template resources.

Database

The app stores user information in a Postgresql database. When the app starts, files in the resources/sql folder are run to ensure database objects functions are fresh.

localhost

A Postgresql database can be created with the command below. Change the PGUSER and PGPASSWORD variables. The command requires administrator access.

PGDATABASE="selene_bananas_db" \
PGUSER="selene" \
PGPASSWORD="selene123" \
PGHOSTADDR="127.0.0.1" \
PGPORT="5432" \
sh -c ' \
sudo -u postgres psql \
-c "CREATE DATABASE $PGDATABASE" \
-c "CREATE USER $PGUSER WITH ENCRYPTED PASSWORD '"'"'$PGPASSWORD'"'"'" \
-c "GRANT ALL PRIVILEGES ON DATABASE $PGDATABASE TO $PGUSER" \
&& echo DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@$PGHOSTADDR:$PGPORT/$PGDATABASE'
HTTPS

The app can be run on HTTP over TLS (HTTPS). If running on TLS, most HTTP requests are redirected to HTTPS.

If the server handles HTTPS by providing its own certificate, use the PORT variable to specify the HTTPS port. When POST is defined, no HTTP server will be started from HTTP_PORT and certificates are not read. Use this in combination weth NO_TLS_REDIRECT=true to prevent the server trying to check the TLS headers on requests.

Mkcert

Use mkcert to configure a development machine to accept local certificates.

go get github.com/FiloSottile/mkcert
mkcert -install

Generate certificates for localhost at 127.0.0.1

mkcert 127.0.0.1

Then, replace the resources/tls-cert.pem and resources/tls-key.pem files with the certificates. Update the .env file with the parameters below. Make sure to remove the PORT variable, if present.

HTTP_PORT=8001
HTTPS_PORT=8000
ACME

The server can verify its identity over HTTP to pass a Automatic Certificate Management Environment (ACME) HTTP-01 challenge. Using a local certificate generated in the previous step by mkcert, add the ACME environment parameters listed below with necessary values to the .env file. This makes the HTTP server respond to challenge requests correctly. After the certificates are created, remove the ACME_* parameter and replace the resources/tls-cert.pem and resources/tls-key.pem files with the certificates. See letsencrypt.org for more information about challenges.

ACME_CHALLENGE_TOKEN=token123
ACME_CHALLENGE_KEY=s3cr3t_key
Serve on Default TCP HTTP Ports

Run make serve-tcp to run on port 80 for HTTP and port 443 for HTTPS (default TCP ports). Using these ports requires sudo (root) access.

Directories

Path Synopsis
cmd
Package cmd contains executable packages for the server and client ui.
Package cmd contains executable packages for the server and client ui.
server
Package main starts the server after configuring it from supplied or standard arguments
Package main starts the server after configuring it from supplied or standard arguments
ui
Package main initializes interactive frontend elements and runs as long as the webpage is open.
Package main initializes interactive frontend elements and runs as long as the webpage is open.
db
Package db implements a SQL database.
Package db implements a SQL database.
user
Package user contains handles the state of users.
Package user contains handles the state of users.
user/bcrypt
Package bcrypt contains password hashing and checking logic for stored passwords.
Package bcrypt contains password hashing and checking logic for stored passwords.
Package game contains communication structures for the game controller, lobby, and socket to use.
Package game contains communication structures for the game controller, lobby, and socket to use.
board
Package board stores the tiles for a game and handles queries to read and update tiles
Package board stores the tiles for a game and handles queries to read and update tiles
message
Package message contains structures to pass between the ui and server.
Package message contains structures to pass between the ui and server.
player
Package player identifies game players.
Package player identifies game players.
tile
Package tile contains structures that players interact with on game boards.
Package tile contains structures that players interact with on game boards.
word
Package word handles checking words in the game.
Package word handles checking words in the game.
go module
Package server runs the http server with allows users to open websockets to play the game
Package server runs the http server with allows users to open websockets to play the game
auth
Package auth contains code to ensure users are authorized to use the server after they have logged in.
Package auth contains code to ensure users are authorized to use the server after they have logged in.
game
Package game controls the logic to run the game.
Package game controls the logic to run the game.
game/lobby
Package lobby handles players connecting to games and communication between games and players
Package lobby handles players connecting to games and communication between games and players
game/player
Package player controls the game for each player
Package player controls the game for each player
game/socket
Package socket handles communication with a player using a websocket connection
Package socket handles communication with a player using a websocket connection
game/socket/gorilla
Package gorilla implements a websocket connection by wrapping gorilla/websocket.
Package gorilla implements a websocket connection by wrapping gorilla/websocket.
log
Package log provides an abstraction over log.Logger.
Package log provides an abstraction over log.Logger.
log/logtest
Package logtest implements support for testing Loggers.
Package logtest implements support for testing Loggers.
ui
Package ui contains the client fror the game.
Package ui contains the client fror the game.
dom
Package dom contains the javascript bindings for the site
Package dom contains the javascript bindings for the site
dom/url
Package url replaces the standard net/url package for basic url operations
Package url replaces the standard net/url package for basic url operations
game
Package game has the ui game logic.
Package game has the ui game logic.
game/canvas
Package canvas contains the logic to draw the game.
Package canvas contains the logic to draw the game.
game/lobby
Package lobby contains code to view available games and to close the websocket.
Package lobby contains code to view available games and to close the websocket.
game/socket
Package socket contains the logic to communicate with the server for the game via websocket communication
Package socket contains the logic to communicate with the server for the game via websocket communication
http
Package http makes XML HTTP Requests using native browser code.
Package http makes XML HTTP Requests using native browser code.
log
Package log contains shared logging code
Package log contains shared logging code
user
Package user contains code to create and edit users that can play games in the lobby.
Package user contains code to create and edit users that can play games in the lobby.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL