Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Build Your Own Web Framework in Elixir

You're reading from   Build Your Own Web Framework in Elixir Develop lightning-fast web applications using Phoenix and metaprogramming

Arrow left icon
Product type Paperback
Published in Jun 2023
Publisher Packt
ISBN-13 9781801812542
Length 274 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Aditya Iyengar Aditya Iyengar
Author Profile Icon Aditya Iyengar
Aditya Iyengar
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Part 1: Web Server Fundamentals
2. Chapter 1: Introducing the Cowboy Web Server FREE CHAPTER 3. Chapter 2: Building an HTTP Server in Elixir 4. Part 2: Router, Controller, and View
5. Chapter 3: Defining Web Application Specifications Using Plug 6. Chapter 4: Working with Controllers 7. Chapter 5: Adding Controller Plugs and Action Fallback 8. Chapter 6: Working with HTML and Embedded Elixir 9. Chapter 7: Working with Views 10. Part 3: DSL Design
11. Chapter 8: Metaprogramming – Code That Writes Code 12. Chapter 9: Controller and View DSL 13. Chapter 10: Building the Router DSL 14. Index

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime