Book Image

Build Your Own Web Framework in Elixir

By : Aditya Iyengar
Book Image

Build Your Own Web Framework in Elixir

By: Aditya Iyengar

Overview of this book

Elixir's functional nature and metaprogramming capabilities make it an ideal language for building web frameworks, with Phoenix being the most ubiquitous framework in the Elixir ecosystem and a popular choice for companies seeking scalable web-based products. With an ever-increasing demand for Elixir engineers, developers can accelerate their careers by learning Elixir and the Phoenix web framework. With Build Your Own Web Framework in Elixir, you’ll start by exploring the fundamental concepts of web development using Elixir. You'll learn how to build a robust web server and create a router to direct incoming requests to the correct controller. Then, you'll learn to dispatch requests to controllers to respond with clean, semantic HTML, and explore the power of Domain-Specific Languages (DSL) and metaprogramming in Elixir. You'll develop a deep understanding of Elixir's unique syntax and semantics, allowing you to optimize your code for performance and maintainability. Finally, you'll discover how to effectively test each component of your application for accuracy and performance. By the end of this book, you'll have a thorough understanding of how Elixir components are implemented within Phoenix, and how to leverage its powerful features to build robust web applications.
Table of Contents (15 chapters)
1
Part 1: Web Server Fundamentals
4
Part 2: Router, Controller, and View
10
Part 3: DSL Design

What this book covers

Chapter 1, Introducing the Cowboy Web Server, covers how an HTTP server is designed and the details of Cowboy, the most used web server in the Elixir ecosystem. It also covers how to use Cowboy to build a simple web application that serves HTML. Finally, it details how to test the web application built using Cowboy.

Chapter 2, Building an HTTP Server in Elixir, uses the conclusions from Chapter 1 and extends them to build a brand new HTTP server in Elixir. It covers setting up a TCP socket using :gen_tcp, and wrapping it in an idiomatic interface. This chapter also covers how to test a web application built using the new web server, as well as how to test the web server itself. Finally, it covers how to add concurrency to the new web server. This HTTP server will finally be used in the web framework that’s being built in this book, Goldcrest.

Chapter 3, Defining Web Application Specifications Using Plug, covers the Plug package and the Plug.Conn construct, which together act as building blocks of a Phoenix HTTP request. This chapter covers the components of the Plug package and the philosophy of using it. It takes a deeper dive into Plug.Router and covers how to use it to build a routing layer to a web application. It then covers how to build a Plug adapter for a web server by taking a look at Cowboy’s Plug adapter. Finally, it uses all this knowledge to build a new Plug adapter for the web server built in Chapter 2. This will allow us to use Plug with the new web server.

Chapter 4, Working with Controllers, leverages what we learned in Chapter 3 about Plug and uses it to build a controller interface for the web framework, Goldcrest. It covers the basics of a controller in Phoenix and building a controller interface that follows a similar pattern. It also details how redirection works in Phoenix and how to add that functionality to the new controller interface. Finally, it uses the Plug.Test module to test the newly built controller.

Chapter 5, Adding Controller Plugs and Action Fallback, extends the controller interface built in Chapter 4 by adding the ability to intercept a request at the controller level before letting the controller handlers handle it. This chapter covers how Phoenix handles such use cases and how we can simply use Plug to handle this. It details the Plug.Builder module that comes with the Plug package and the different approaches to test it. Finally, this chapter covers adding the ability to provide a fallback option to the controller, which handles any failed responses from all the handlers.

Chapter 6, Working with HTML and Embedded Elixir, explains how HTML is rendered on the server side by Phoenix. It introduces the EEx module, which allows us to embed Elixir between non-Elixir text, and it covers how we can use EEx to respond with dynamic server-side rendered HTML. Finally, it covers how to test the dynamically generated HTML.

Chapter 7, Working with Views, covers how to build the View interface for the web framework, Goldcrest. It goes over some of the key functionalities that can be extracted from the HTML rendering aspect of the controller and leverages the EEx module’s ability to pass helper functions at the time of evaluation. Like all the other chapters, it ends by covering strategies to test the changes explained in the chapter.

Chapter 8, Metaprogramming – Code That Writes Code, covers metaprogramming in Elixir. It covers the constructs such as quote, code injection, abstract syntax trees, and so on that allow Phoenix to have such a simple interface and breaks them down in a digestible manner. It also covers macros and compile-time hooks while using these constructs to build a new domain-specific language (DSL) to produce music in Elixir, as an example. Finally, it covers several ways to test the meta code to make it more deterministic.

Chapter 9, Controller and View DSL, uses the concepts covered in the previous chapter to build a DSL around the controller and view built in Chapter 3 to Chapter 7. This chapter also covers ways of making the interface easier to work with by making it easier to test and more introspective. Finally, it covers ways of testing the new Controller and View interfaces.

sw, Building the Router DSL, uses the metaprogramming concepts from Chapter 8 to build a new DSL. This DSL will be for the router functionality, and this chapter covers ways to mimic Phoenix’s router DSL. Like the previous chapter, it covers ways of making the DSL easier to use and test. Finally, it updates the example app built in the first part of the book to use the Router, Controller, and View interfaces built in the last two chapters.