Go to file
Brandon Thomas cb5a135f68 trying to figure out websocket stuff again 2022-01-09 02:57:20 -05:00
.github/workflows
…
crates trying to figure out websocket stuff again 2022-01-09 02:57:20 -05:00
db silent k slur also works. ugh :( 2021-09-12 12:24:20 -04:00
debug test category assignments 2021-12-29 01:12:03 -05:00
localdev/nginx-http-config proper setup for local dev + optional ssl 2022-01-07 16:56:49 -05:00
migrations note about migrations in prod 2022-01-06 03:09:07 -05:00
migrations_squashed/tts_tables Update tts_models schema to track TalkNet-related details 2021-09-10 04:21:47 -04:00
migrations_wip/2021-09-29-042235_twitch_oauth_tokens move unused OBS migrations out of the way (for now) 2021-12-20 17:56:17 -05:00
models Repair model/ files that were overwritten for testing 2021-11-15 00:13:25 -05:00
scripts
…
seed
…
static trying to figure out websocket stuff again 2022-01-09 02:57:20 -05:00
vendor/newrelic-telemetry-sdk-rust Add NewRelic tracing to tts-inference-job using vendored lib 2021-09-13 05:44:47 -04:00
.dockerignore
…
.env Add NewRelic tracing to tts-inference-job using vendored lib 2021-09-13 05:44:47 -04:00
.gitignore Twitch secrets handling, AnyhowResult in common crate, etc. 2021-11-07 23:32:21 -05:00
Cargo.lock natural+lexographic sort of category dropdown names 2022-01-05 02:04:55 -05:00
Cargo.toml Refactor crate organization. Both server binaries build 2021-11-12 23:51:25 -05:00
Dockerfile Attempt to fix dockerfile 2021-11-15 00:33:57 -05:00
README.md simplify readme 2022-01-07 02:37:25 -05:00
codegen_database.sh Fix sqlx issue preventing builds 2022-01-03 02:27:24 -05:00
diesel.toml
…
loc.sh fixup loc.sh 2021-11-15 00:32:24 -05:00
rustfmt.toml better rustfmt rules? still not ready to apply these 2021-12-22 03:36:26 -05:00
sqlx-data.json Filter out deleted category assignments 2022-01-07 02:59:29 -05:00

README.md

storyteller-web

This is the main user account monolith that we'll also bake other pieces into.

Local development

Debugging and Fixing CUDA/PyTorch

See this spreadsheet for previous battles with version incompatibility: https://docs.google.com/spreadsheets/d/1BEdLmwOzo3r83-iJn9sj6co1VT92t3lIeJ-lNH25bdQ/edit#gid=0

Ubuntu might upgrade the driver by accident, and it might need reinstallation. Who knows. This stuff is a nightmare.

Database Setup

Install the following libraries, and see the notes further below about MySQL on Ubuntu 20.04.

mysql-server
imagemagick

To manage the database and perform migrations, install the Rust tools diesel and sqlx.

We'll be using diesel to manage the migrations, but sqlx within the app to actually perform queries. Diesel is an ORM, which is dumb, so we use sqlx as at-compile-time typesafe SQL.

sudo apt-get install libmysqlclient-dev
cargo install sqlx-cli --no-default-features --features mysql
cargo install diesel_cli --no-default-features --features mysql

Linux database notes

If MySql in local dev can't be connected to, reset the accounts:

https://linuxconfig.org/how-to-reset-root-mysql-mariadb-password-on-ubuntu-20-04-focal-fossa-linux

Fixing dev MySql on Ubuntu 20.04

For some reason, the MySql default install on 20.04 gave me a bunch of trouble.

In retrospect, I think this is because 'root@localhost' requires sudo to access, but if this gives any trouble in the future, here's how I got around it (two full hours of distraction!)

sudo apt-get install mysql-server

# But for some reason the default password doesn't work and diesel/sqlx can't connect?

# Kill everything

sudo apt purge mysql-server mysql-client mysql-common
sudo apt autoremove
sudo mv -iv /var/lib/mysql /var/tmp/mysql-backup
sudo rm -rf /var/lib/mysql*

sudo /usr/bin/mysql_secure_installation

# still no work...

sudo systemctl stop mysql.service

# ugh, it wasn't chowned or created (both states observed in different installs)…
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld

sudo mysqld_safe --skip-grant-tables --skip-networking &

mysql -u root

flush privileges;
USE mysql;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';

sudo killall -u mysql
sudo systemctl restart mysql.service

# now it works
sudo mysql -u root -p

# but now I have to use "sudo" !?!?

use mysql;

CREATE USER 'storyteller'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON storyteller.* TO 'storyteller'@'localhost';

Install Diesel (migrations only)

We use Diesel to manage migrations, but we don't use it in server code. Actual server code uses SQLx. To install the CLI tool, run the following:

cargo install diesel_cli

Mac has issues and requires a few dependencies:

brew install libpq

Database migrations

To reset the entire database (drop, migrate), run:

diesel database reset

To migrate at the current step and beyond:

diesel migration run

To undo migrations at the current step:

diesel migration redo

Server Query Codegen

We use SQLx instead of Diesel in the production server. It's typesafe SQL instead of an ORM like Diesel.

SQLx connects to a database to derive type information, but obviously cannot do this for builds in CI. In order to cache the types, we build and check in a cache file (necessary for builds):

SQLX_OFFLINE=true cargo sqlx prepare

Now that we have multiple binaries, it's required to include all the queries in the main binary so we can generate the cached queries as a single target. That's then executed with:

SQLX_OFFLINE=true cargo sqlx prepare -- --bin storyteller-web

Setting Up a Local Nginx Proxy

Set up a local nginx to proxy to the frontend and backend so cookie issues aren't annoying

Configure Nginx per the checked in Nginx configs (and instructions) in localdev/nginx-http-config.


And in /etc/hosts,

127.0.0.1 jungle.horse 127.0.0.1 api.jungle.horse


### Python 3.6 on Apple M1 Mac

Python3.6 isn't supported on Apple silicon, and it's not in homebrew. It can be installed with 
[nix using Rosetta](https://stackoverflow.com/a/65980989):

Download: https://nixos.org/download.html#nix-quick-install

nix run nixpkgs.python36 -c python


Install venv:

nix run nixpkgs.python36 -c python -m venv python


Install other packages on Mac that aren't used in venv:

python3 -m pip install --user requests gdown youtube_dl


Production
----------

These instructions assume running on GCP.

### Database migrations

1. Set `DATABASE_URL` in `.env` to the production secrets (DO NOT COMMIT!)
2. Run `diesel migration run`

### Setting up public buckets without list permission

Public buckets that deny the `list` action should use the following Role:

`roles/storage.legacyObjectReader`

See:

* https://stackoverflow.com/a/56354633
* https://cloud.google.com/storage/docs/access-control/making-data-public#buckets

### Generating Bucket Access Key and Secret Key

https://cloud.telestream.net/tutorials/how-to-setting-up-google-cloud-storage/

1. Go to the GCS page
2. Click "settings"
3. Click "interoperability" tab
4. (enable interoperable access if not already set)
5. Click "create new key"

Actix notes
-----------

json request

async fn handler(request: web::Json) -> impl Responder { "whatever" }


form-multipart request

async fn handler(request: web::Form) -> impl Responder { "whatever" }


route parameters

#[get("/{name}")] async fn hello(name: web::Path) -> impl Responder { format!("Hi {}", name) }


TODO
----

* Revise this README to be more useful.

Notes / TODOs:

* Examples for good Actix+Sqlx Tests:
  https://stackoverflow.com/questions/65370752/how-do-i-create-an-actix-web-server-that-accepts-both-sqlx-database-pools-and-tr

* Actix/sqlx runtime compat:
  https://github.com/launchbadge/sqlx/issues/1117#issuecomment-801237734
  
* Redis caching

* Jobs for analytics queries


Docker builds
-------------

The repository needs to be given read access to the base docker image:

https://github.com/orgs/storytold/packages/container/docker-base-images-rust-ssl/settings