Creating a Static HTTP Server with Rust – Part 2

In this series, we are creating a basic static HTTP 1.0 server with Rust. If you haven’t seen Part 1 yet, go do that first. At the end of Part 2, our server will do the following:

  • Read and serve files from a predefined directory on the host server
  • Generate appropriate HTTP responses to incoming requests
  • Log information about the response to standard output
Continue reading Creating a Static HTTP Server with Rust – Part 2

Creating a Static HTTP Server with Rust – Part 1

In this series, we will create a basic static HTTP 1.0 server with Rust. At the end of Part 1 of this tutorial, our server will do the following:

  • Listen for and handle TCP connections on a specific port
  • Accept HTTP 1.0 GET requests
  • Parse and validate requests for further use
  • Log incoming requests

We will avoid using libraries that make this trivial (i.e. the http crate) and focus on the fundamentals of how a server works. Let’s get started.

Continue reading Creating a Static HTTP Server with Rust – Part 1

Avoid Memory Issues with Django’s bulk_create

When inserting a large number of objects into the database with Django, your first thought should be to use bulk_create. It is much more efficient than calling create for each object, and it generally only results in a single query. However, when dealing with tens of thousands, hundreds of thousands, or even more objects, you may run into out of memory errors.

Continue reading Avoid Memory Issues with Django’s bulk_create

Using JavaScript’s Fetch with a REST API

If you are at all familiar with modern JavaScript, you have probably heard of, or used, Fetch; a function that allows you to make asynchronous HTTP requests. It leverages ES6 promises to make it easy to define asynchronous behavior. Fetch and promises can be tricky to work with and understand, especially if you are new to them. In this post, we will get a basic understanding of each of them, then discover how we can create a collection of wrappers to make sending requests to a traditional REST API a breeze.

Continue reading Using JavaScript’s Fetch with a REST API

Automate Docker Image Versioning with Jenkins

Versioning your Docker images is a good practice that will make them easier to deal with, whether they are used only privately or are offered to the public. With each iteration of an image, regression is always a possibility. To prevent the disruption of services for yourself and others, it is always a good idea to maintain a stable versioning system. By using semantic versioning, we are able to categorize our iterations clearly and allow the subscriber to choose which they want.

Continue reading Automate Docker Image Versioning with Jenkins

How to Dockerize Node.js

Dockerizing your application makes things like development, deployment, distribution, and continuous integration much simpler. Today, we will look at how to run your Node.js application in a Docker container, using Docker Compose to further facilitate the process. See last week’s post, How to Dockerize Django and Postgres, if you are interested in doing the same for your Django application.

Continue reading How to Dockerize Node.js

How to Dockerize Django and Postgres

Docker is an invaluable tool that makes things like setting up your local development environment, continuous integration, and deployment a breeze. Docker is good to set up early on for any application, but it is never too late. With the help of Docker Compose, we are going to set up a Django application to install and deploy itself on any machine with only one command.

Continue reading How to Dockerize Django and Postgres

How to Optimize Your Django REST Viewsets

The combination of Django and the Django REST framework is powerful. With just three classes (a model, a viewset, and a serializer), you can have a basic CRUD endpoint up and running. Although it is easy to get set up, it is also easy to end up with a view that makes hundreds of unnecessary database queries. As database queries are relatively slow, we want to avoid them as much as possible. In order to do this, we will follow Tip #4 from my Django ORM Optimization Tips post:

4. Use select_related() and prefetch_related() when you will need foreign-key/reverse related objects.

Continue reading How to Optimize Your Django REST Viewsets

Normalize Your Django REST Serializers

When dealing with models with nested relationships, it may initially make sense to serialize them in a nested format. However, you may soon discover that this has a couple of potential issues.

  1. This structure can result in a lot of duplication in the serialized data, especially for many-to-many relationships.
  2. Since those objects are nested, you don’t have them all in one place for easy referencing or updating. Working around this requires tedious iteration and transformation.

These issues are especially pronounced in Javascript applications that use state methodologies or technologies like Redux. Redux applications work much better when your data is normalized. Normalization facilitates cleaner code, makes updating state simpler, and ensures that the fewest possible UI components are forced to re-render due to such updates.

Continue reading Normalize Your Django REST Serializers

Case-Insensitive Fields in Django Models

If you are trying to figure out how to create a case-insensitive field for your Django models, there are several ways to go about it. If you don’t care about preserving the actual mixed-case version, you can simply convert your strings to lowercase before they get saved to the model. If you are using PostgreSQL and don’t mind a database-specific solution, you can use CITextField or similar. Otherwise, read on.

Some solutions online involve adding or customizing manager or queryset functions. While this may work okay at a basic level, it isn’t a complete solution, and doesn’t actually prevent you from inserting a duplicate case-insensitive value.

I have discovered a solution that is clean, enforces case-insensitive uniqueness, and works for all Django lookups that have case-insensitive versions.

Continue reading Case-Insensitive Fields in Django Models