add setup and dev instructions

This commit is contained in:
Ben Harris 2021-08-03 16:23:50 -04:00
parent 6f23e77cd4
commit 437566c82a
6 changed files with 103 additions and 60 deletions

View File

@ -1,2 +1,22 @@
# quotes
[Quote database for the tildeverse network.](https://quotes.tilde.chat/)
[Quote database for the tilde.chat](https://quotes.tilde.chat/)
## setup
sudo apt install sqlite3 python3-flask
sqlite3 quotes.db < schema.sql
sudo cp qdb.service /etc/systemd/system/
sudo cp qdb.nginx.conf /etc/nginx/sites-available/qdb.example.com
sudo ln -s ../sites-available/qdb.example.com /etc/nginx/sites-enabled/
sudo systemctl enable --now qdb
## running locally
* make sure you have python3-flask installed
* create the schema per the setup instructions
* run the flask app:
FLASK_APP=app.py FLASK_DEBUG=1 flask run -h 0.0.0.0 -p 3333
* visit the local version in your browser: http://localhost:3333

View File

@ -1,59 +0,0 @@
#!/usr/bin/env python3
import sys, sqlite3, app
if len(sys.argv) < 2:
print("python3 dbsetup.py [seed|create]")
exit()
db = sqlite3.connect(app.DATABASE)
cur = db.cursor()
cur.execute(
"""
CREATE TABLE IF NOT EXISTS quotes
(ID INT PRIMARY_KEY, CONTENT TEXT, SCORE INT)
"""
)
cur.execute(
"""
CREATE VIRTUAL TABLE IF NOT EXISTS quotes_idx
USING fts5(content, content='quotes', content_rowid='id')
"""
)
cur.execute(
"""
CREATE TRIGGER IF NOT EXISTS quotes_ai AFTER INSERT ON quotes
BEGIN
INSERT INTO quotes_idx (rowid, content)
VALUES (new.id, new.content);
END
"""
)
cur.execute(
"""
CREATE TRIGGER IF NOT EXISTS quotes_ad AFTER DELETE ON quotes
BEGIN
INSERT INTO quotes_idx (quotes_idx, rowid, content)
VALUES('delete', old.id, old.content);
END
"""
)
cur.execute(
"""
CREATE TRIGGER IF NOT EXISTS quotes_au AFTER UPDATE ON quotes
BEGIN
INSERT INTO quotes_idx (quotes_idx, rowid, content)
VALUES ('delete', old.id, old.content);
INSERT INTO quotes_idx (rowid, content)
VALUES (new.id, new.content);
END
"""
)
if sys.argv[1] == "seed":
cur.execute("INSERT INTO QUOTES VALUES (?, ?, ?)", (1, "welp, this is empty.", 0))
db.commit()
db.close()

29
qdb.nginx.conf Normal file
View File

@ -0,0 +1,29 @@
server {
listen 80;
listen [::]:80;
server_name qdb.example.com;
return 307 https://$server_name$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl ipv6only=on;
server_name qdb.example.com;
ssl_certificate /etc/letsencrypt/live/qdb.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/qdb.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
include uwsgi_params;
uwsgi_pass unix:/opt/quotes/quotes.sock;
}
location /static {
root /opt/quotes;
try_files $uri $uri/ =404;
}
}

24
qdb.service Normal file
View File

@ -0,0 +1,24 @@
[Unit]
Description=quotes.tilde.chat
[Service]
Type=simple
WorkingDirectory=/opt/quotes
ExecStart=/usr/bin/uwsgi \
--plugin=python3 \
--uwsgi-socket=quotes.sock \
--chmod-socket=660 \
--wsgi-file=app.py \
--callable=app \
--processes=1 \
--threads=2
User=qdb
Group=www-data
Restart=always
RestartSec=5
StartLimitInterval=60s
StartLimitBurst=3
[Install]
WantedBy=default.target

26
schema.sql Normal file
View File

@ -0,0 +1,26 @@
CREATE TABLE IF NOT EXISTS quotes
(ID INT PRIMARY_KEY, CONTENT TEXT, SCORE INT);
CREATE VIRTUAL TABLE IF NOT EXISTS quotes_idx
USING fts5(content, content='quotes', content_rowid='id');
CREATE TRIGGER IF NOT EXISTS quotes_ai AFTER INSERT ON quotes
BEGIN
INSERT INTO quotes_idx (rowid, content)
VALUES (new.id, new.content);
END;
CREATE TRIGGER IF NOT EXISTS quotes_ad AFTER DELETE ON quotes
BEGIN
INSERT INTO quotes_idx (quotes_idx, rowid, content)
VALUES('delete', old.id, old.content);
END;
CREATE TRIGGER IF NOT EXISTS quotes_au AFTER UPDATE ON quotes
BEGIN
INSERT INTO quotes_idx (quotes_idx, rowid, content)
VALUES ('delete', old.id, old.content);
INSERT INTO quotes_idx (rowid, content)
VALUES (new.id, new.content);
END;

View File

@ -46,6 +46,9 @@
<p>this is an irc quote database, in the style of <a href="http://bash.org">bash.org</a>, that aims to catalog moments on tilde.chat.</p>
{% endblock %}
<footer class="text-center">
<a href="https://tilde.chat/">return to tilde.chat</a> ~ <a href="https://tildegit.org/tildeverse/quotes">qdb source code</a>
</footer>
</div>
<script src="{{ url_for('static', filename='jquery.min.js') }}"></script>