Flask “Micro-framework” and SQL Alchemy

I’ve not posted in a while, but I’ve still been busy. Lately I’ve been toying with the Flask framework. It claims to be a “micro-framework” that doesn’t prescribe too much and accomodates a more open-ended approach for developers. It does a bit of heavy lifting with routes and templating, but I’ve not had much of a steep learning curve in terms of having to learn the “Flask way of doing things”. Compared to other widely used webDev frameworks like Ruby on Rails or Django, Flask is, at the moment, fairly straightforward to get your head around.

For example, it doesn’t force us to use abstracted data models. You are welcome to do that via extensions such as the awesome SQL Alchemy, but if you want to tie in directly to your database you can install a bit of middleware or, in the case of SQLite, just start writing CRUD methods with your own SQL. SQL Alchemy is well worth checking out though if you’re into freedom of choice and flexibility.

JS Vanilla Fetch API & Bulma CSS

Today I ran through a quick tutorial by Brad Traversy to explore some new Javascript functionality that does something I could only previously do with libraries such as jQuery. We used a JS Vanilla fetch method to request zip code information from a really easy to use open API called Zippopotam.us.

I also got some practice in with Emmet in Visual Studio Code which was a major speed enhancement. I need to explore this further as it made life so much easier creating a DOM and functionality.

Instead of using Bootstrap, we used Bulma CSS framework with Font Awesome for icons which was a breeze.

Monitorix System Monitor

I’ve been managing a CentOS 7 server on a VPS since July. There’s an overwhelming amount of information and recommendation for how to configure a server out there. My build is based around Vesta CP and I’m pretty happy with it.

I have been running into issues with PHP7 and PHP5 processes maxing out the 8GB available memory. To solve this problem, my first mission was to explore network monitoring solutions. There are a plethora of things on offer but most were just overkill for my needs. They were also very complex to set up. Fortunately I came across Monitorix which was a simple install and configuration. It gives me all the visuals I need to make sure my VPS is running smoothly. Here’s a graph of my current memory usage over 24 hours.

It may look a bit odd because I run a cron that kills all PHP7 and PHP5 processes every hour as you can see in peaks and valleys above.

killall php70-cgi
killall php-cgi
systemctl restart mariadb

This is clearly more of a workaround than a solution. To purge the PHP processes, I’d have to investigate all the memory leaks which are created by content management systems such as this WordPress install you’re looking at now. An old version of Drupal was the worst memory hog out of all of them.

Because this is a low traffic server I can get away with cludges such as the one above. It’s not a long term solution; more of a band-aid, but it keeps my system from maxing out memory and using swap.

To install Monitorix on CentOS 7 & you can use the yum package manager:

sudo yum install epel-release
sudo yum install monitorix

It will install a lot of Perl dependencies. When it’s finished you can start the service:

service monitorix start

It runs, by default, on port 8080 but it can easily be changed in the config file to whatever port you’re happy with.

vim /etc/monitorix/monitorix.conf

Line 30: port = __________

To see the graphs visit: http://yourdomain.com:8080/monitorix   (or whatever port you’ve set it to).

Portfolio: Tian Ran

Today I grabbed the domain: specialed.space as the old one (eddie.cf) was a free domain with an uncertain future.

I’m gradually starting to move projects into a portfolio of work I’ve done over the last 20 years. At first they’ll just be posts on this blog but I’ll eventually put them into some sort of presentation.

This is a website I made for a vegan Chinese restaurant. It’s a WordPress install with a custom theme based off of a free theme I found while digging around the WordPress theme directory.



MariaDB + Ruby on Rails

Today we completed the Ruby on Rails official getting started guide blog example to demonstrate CRUD using Maria DB instead of the default sqlite3.

It ran smoothly and gave me the ability to use phpmyadmin to poke around my database. Some issues encountered were that, despite declaring relationships between tables on the model level, by default the database does not show any cascading or auto-delete.  It may well be something that was beyond the scope of a quick start tutorial, or the functionality may not exist at all.

Switching from Sqlite to MariaDB involved two steps:

In the Gemfile, comment out gem ‘sqlite3’ and add a line gem ‘mysql2’

# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
gem 'mysql2'

In config/database.yml comment out the existing database connections and add this block (replacing it with your MariaDB credentials).

  adapter: mysql2
  encoding: utf8
  database: yourDatabaseName
  pool: 5
  username: yourDatabaseUsername
  password: yourDatabasePassword

Of course this is only working in the dev environment but you’d only need to define it for production in the same file by replacing development: with production:

Exploring Ruby on Rails

Ruby on Rails is my latest adventure. At this point, after doing a basic blog tutorial to demonstrate CRUD, it seems like a solid framework. It preempts developer error by not allowing much freedom in how you go about things.

The error messages are full of information and allow a full stack trace or segmented. There are lots of command line shortcuts for instantiating MVCs for any component.

What really impresses me the most is how they’ve abstracted the database layer. It seems completely agnostic in terms of data storage. If you want sqlite3 (default) then leave it alone, MySQL setup involved changing one line in the Gemfile (config) and replacing the database.yml with the credentials for MySQL.

I’ve heard recently that the buzz for RoR is starting to fade but I feel that it is a great thing to have in my toolkit. The speed of development, once you get your head around the MVC, is blazingly fast.