Skip to content

PHX-Devs/bento-reloaded

Repository files navigation

lad

bento-box

A python web developer's box of goodies.

code-sizecode-sizelicense

Table of Contents

Getting Started

Bento is a "template repository." To build something using Bento, use it as a template to create your own repository with all of the same files (but without the git commit history and branches). Click the "use this template" button in github or click here.

Once you have your own repo, clone it locally as you normally would and continue to the setup steps below. Once installed, you'll be ready to build something neat within Bento!

Setup

Set Up the Vagrant box

  1. get the deps:
    • Vagrant
    • Virtualbox
    • optional: winscp, putty, puttygen
  2. open terminal/cmd in this dir
  3. vagrant up

SSH to the machine

2 options:

  • vagrant ssh
  • your ssh client of choice

Putty setup (optional)

TO USE PUTTY, convert the autogenerated private key to ppk

  • autogenerated: ./.vagrant/machines/default/virtualbox/private_key (pem)
  • putty: ./.vagrant/machines/default/virtualbox/private.ppk (putty) To Convert:
  1. run generate_putty_key.bat after 'vagrant up' completes
    • NOTE: you'll need winscp in your path for this step to work
    • ALTERNATIVELY: convert the key manually using puttygen
  2. configure putty session:
    • user: vagrant
    • host: localhost
    • port: 2222
    • private key: ./.vagrant/machines/default/virtualbox/private.ppk

Install on AWS, Google, Digital Ocean, etc

  1. make sure your instance is running almalinux 8.x
  2. clone repo in /var/www/modules
  3. run install.sh

Build Something Neat

Once installed, explore the built-in examples and build something of your own!

What you get

vagrant up

Gets you...

  • almalinux
  • all rpms updated
  • python38 installed
  • fastapi installed
  • hypercorn installed
  • postgres 13 and postgis 3 installed
    • also a starter database called 'bento' and a schema called test
  • migra installed
  • empty 'root_app' module listening on the '/' path
  • hello world fastapi app installed and configured in nginx
  • example api module installed (with an actual database schema behind it)

Creating a new module

**for the sake of example, we'll act like we're adding a module named 'carbs'

  1. create a new dir in the root of this repo (/carbs/)
  2. make sure it has the following (probably best to copy one of the examples)
    • __init__.py (empty - if you're reading this in an editor... otherwise known as init.py)
    • carbs.py (main fastapi app here)
    • carbs.toml ( hypercorn config items)
    • carbs.service (systemd service file)
    • carbs_conf_d.conf (nginx config file (conf.d dir) - loaded outside of the server directive)
    • carbs.conf (nginx config file (default.d dir) - loaded inside of the server directive)
    • ./templates/ dir (optional, if your module has a web ui) **If copy/pasting, make sure to review each of the above files
  3. add a line to /provision/modules.sh for your new carbs module
    • will run next time your vagrant box provisions
    • to run the provision step manually (just for your module), comment out accordingly and run modules.sh
    • this step:
      • installs your module as a uwsgi proc running as a service
      • adds the nginx config to the appropriate dir
    • systemctl command will match the filename of your .service file (systemctl start carbs)
  4. restart nginx
    • 'systemctl restart nginx

Creating a new db schema

**for the sake of example, we'll act like we're adding a schema named 'carbs'

  1. add a schema create file in the db dir (/db/carbs_schema.sql)
    • this file should start with 'CREATE SCHEMA carbs;'
  2. add a line to /provision/schemas.sh
    • runuser -l postgres -c "psql -U bento -f /var/www/modules/db/carbs_schema.sql"
    • note: this will install the carbs schema to the bento database - if you've created your own database, use that

Creating a new cron job

**for the sake of example, we'll act like we're adding a cron named 'carbs'

  1. create the new dir in /crons/carbs
  2. make sure it has the following (probably best to copy the example_cron)
    • carbs.logrotate
    • carbs.py (main python cron job)
  3. add a line to /crons/crontab (make sure that there is a newline at the end of the file)
    • in our case */1 * * * * cd /var/www/modules/crons/carbs && /bin/python3 /var/www/modules/crons/carbs/carbs.py >> /var/log/carbs/carbs.log
  4. add a line to /provision/crons.sh
    • crons+=(carbs)

Note on db versioning/migrations

Bento does not solve for db versioning at the moment. But it does include migra, a db diff tool. That's a good start toward a sane db "non-versioning" scheme. Read up on it here: https://djrobstep.com/docs/migra/quickstart

Other Deets

Architecture

The following bash output is the directory structure and organization of bento-box:

tree
.
├── config.py
├── crons
│   ├── crontab
│   └── example_cron
│       ├── example_cron.logrotate
│       ├── example_cron.py
│       └── say_hello.py
├── db
│   ├── create_database.sql
│   └── test_schema.sql
├── example_api
│   ├── example_api.conf
│   ├── example_api_conf_d.conf
│   ├── example_api.py
│   ├── example_api.service
│   ├── example_api.toml
│   └── __init__.py
├── generate_putty_key.bat
├── hello_world
│   ├── hello_world.conf
│   ├── hello_world_conf_d.conf
│   ├── hello_world.py
│   ├── hello_world.service
│   ├── hello_world.toml
│   ├── __init__.py
│   └── templates
│       └── hello_world.html
├── __init__.py
├── install.sh
├── LICENSE
├── provision
│   ├── crons.sh
│   ├── dev_env.sh
│   ├── enable_ssl.sh
│   ├── modules.sh
│   ├── nginx_conf
│   ├── nginx.sh
│   ├── packages.sh
│   ├── postgres.sh
│   ├── schemas.sh
│   └── static.conf
├── README.md
├── root_app
│   ├── __init__.py
│   ├── root_app.conf
│   ├── root_app_conf_d.conf
│   ├── root_app.py
│   ├── root_app.service
│   ├── root_app.toml
│   ├── templates
│   │   ├── base_template.html
│   │   └── index.html
│   └── wsgi.py
├── static
│   ├── css
│   │   └── root.css
│   ├── favicon.ico
│   └── image
│       ├── Bento.png
│       └── title_logo.png
├── utils
│   ├── db.py
│   ├── __init__.py
│   └── __pycache__
│       ├── db.cpython-36.pyc
│       ├── db.cpython-38.pyc
│       ├── __init__.cpython-36.pyc
│       └── __init__.cpython-38.pyc
└── Vagrantfile
14 directories, 55 files

License

MIT © John Zechlin

About

A python web developer's box of goodies.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •