I’m working on a project called Naja. It’s a full-stack app using a PostgreSQL database. Each time I work on it, I cd to the frontend directory, npm run dev, then cd into the api directory, run the tests and start the server. I want to start automating this…or at least streamline it.
I asked GPT to tell me what some of the pros are doing and it returned a handful of practices. I noticed that one of the practices was using make. ok. I thought make was for c++ projects. This is a python FastAPI backend. As you can tell from that statement, I didn’t really have an understanding of what a Makefile does. It runs shell scripts.
Well that’s great! That’s I’m already doing.
venv/scripts/activate
pip install
pytest
unvicorn
npm install dev
The Makefile just runs them in sequence! This might be really novice, but I haven’t really heard of anyone talking about it other than DevOps friends. I had many school projects that ran backend servers. One Prof did use a script in one of the projects now that I think about it, but he didn’t spend any time on it. Basically said, “just run this”.
Streamlining Install and Updates
Each time I wanted to run the server, I would do the following:
1. activate the virtual environment by typing venv/scripts/activate
2. update the dependencies: pip install -r requirements.txt
Now I just run: make install-api
install-api:
@echo Setting up Backend API environment...
@if not exist $(API_VENV) (python -m venv $(API_VENV)) else (echo "Virtual environment already exists.")
@echo Installing dependencies...
@call $(API_ACTIVATE) && pip install -r $(API_REQUIREMENTS)
@echo Backend API setup complete.
This checks to see if there’s a venv already install. If there is not, it creates it. If so, move on. Next it activates the venv and installs or updates the dependencies found in a requirements.txt file. Now if I want someone to take a look at the project, I’m not having to walk them through the setup and install process. I can simple say run “make install-api”
This morning, I worked up the backend. Now I need to I need to streamline the frontend startup. That doesn’t sound bad, but I’m starting to fret over how to create scripts for database creation and migration. This could be rabbit hole.
Makefile (as it is currently)
# Makefile for naja_admin
######################################################################
# Config
######################################################################
# Backend API Variables
API_DIR := api
API_VENV := $(API_DIR)/venv
API_REQUIREMENTS := $(API_DIR)/requirements.txt
API_ACTIVATE := $(API_VENV)/Scripts/activate.bat
API_TESTS := $(API_DIR)/tests
API_APP := main:app
.DEFAULT_GOAL := help
######################################################################
# Help
######################################################################
.PHONY: help
help:
@echo "Available commands:"
@echo " make install-api - Set up the dev environment for api/ and install dependencies"
@echo " make run-api - Run the API server"
@echo " make test-api - Run API tests"
@echo " make clean-api - Clean up the Backend API environment (remove venv)"
######################################################################
# API Setup and Run
######################################################################
.PHONY: install-api run-api
# Set up the dev environment for api/ if it doesn't exist
# Install dependencies
install-api:
@echo Setting up Backend API environment...
@if not exist $(API_VENV) (python -m venv $(API_VENV)) else (echo "Virtual environment already exists.")
@echo Installing dependencies...
@call $(API_ACTIVATE) && pip install -r $(API_REQUIREMENTS)
@echo Backend API setup complete.
# Run the API server
run-api:
@echo Starting the API server...
@call $(API_ACTIVATE) && uvicorn --app-dir $(API_DIR) $(API_APP) --reload
######################################################################
# API Testing
######################################################################
.PHONY: test-api
test-api:
@echo Running API tests...
@call $(API_ACTIVATE) && pytest $(API_TESTS)
@echo API tests completed.
######################################################################
# Cleanup
# Removes the venv.
# Used to test the setup process, but also in case venv is corrupted.
######################################################################
.PHONY: clean-api
clean-api:
@echo Cleaning up Backend API environment...
@if exist "$(API_VENV)" (rmdir /s /q "$(API_VENV)") else (echo "Virtual environment does not exist.")
@echo Backend API environment cleaned up.