Discover the secrets of building high-performing and scalable web applications with clean code using Elixir's metaprogramming
Key Features
Explore the various web servers available to build robust web applications
Leverage Elixir's powerful Plug module to build a request-response pipeline
Explore advanced techniques of Elixir to create Domain-Specific languages and build scalable, maintainable web products
Book Description
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.
What you will learn
Get a comprehensive understanding of the Phoenix framework built on Elixir
Use metaprogramming to optimize your Elixir code and create high-performance web applications
Explore web development fundamentals including the principles of HTTP and HTML
Employ Elixir's templating engine to dispatch requests to a controller and respond with dynamically generated HTML
Improve the scalability and responsiveness of your web server by using OTP constructs
Streamline your routing logic using error handling
Who this book is for
This book is for web developers seeking to deepen their understanding of Elixir's role in the Phoenix framework. Basic familiarity with Elixir is a must.
Author(s): Aditya Iyengar
Publisher: Packt Publishing Pvt Ltd
Year: 2023
Language: English
Pages: 538
Preface
Part 1: Web Server Fundamentals
1
Introducing the Cowboy Web Server
Technical requirements
What is a web server?
Exploring the client-server architecture
Understanding HTTP
Understanding Cowboy’s architecture
Building a web application using Cowboy
Creating a new Mix project
Adding a handler and router to Cowboy
Supervising the web server
Adding routes with bindings
Validating HTTP methods
Responding with HTML files
Testing the web server with ExUnit
Summary
Exercises
2
Building an HTTP Server in Elixir
Technical requirements
Listening over a TCP socket using :gen_tcp
Responding over a TCP socket using :gen_tcp
Writing an HTTP server package
Defining an HTTP response struct
Validating whether a responder is configured
Creating responder behavior
Testing the HTTP server
Running the HTTP server with a sample application
Adding concurrency to the server
Bonus – starting a detached HTTP server
Summary
Exercise
Part 2: Router, Controller, and View
3
Defining Web Application Specifications Using Plug
Technical requirements
What is Plug?
Representing the state of the connection with the Plug.Conn struct
Defining a plug
Understanding Plug.Router
Plugifying our HTTP server
Summary
4
Working with Controllers
Technical requirements
What are controllers?
Controllers in Phoenix
Building a controller interface
Integration with web servers
Understanding redirects
Testing the controller
Summary
Exercises
5
Adding Controller Plugs and Action Fallback
Technical requirements
Plug pipeline in controllers
Understanding the Plug.Builder module
Building the controller pipeline
Implementing action fallback
Summary
6
Working with HTML and Embedded Elixir
Technical requirements
Understanding and exploring embedded elixir
Defining functions dynamically in a module
Compiling a template with a module
Working with EEx options
Using EEx Smart Engine
Understanding custom markers
Building a custom EEx engine
Responding with html.eex files
Testing our templates
Testing with Floki
Summary
7
Working with Views
Technical requirements
What is a view?
Views in Phoenix
Building a view interface
Passing helper functions to templates
Taking Goldcrest for a spin
Defining a store
The index action
The create action
The delete action
Summary
Exercises
Part 3: DSL Design
8
Metaprogramming – Code That Writes Code
Technical requirements
The pros and cons of metaprogramming
When to use metaprogramming
Metaprogramming in Elixir
Understanding quoted literals
Evaluating quoted literals
The hygienic evaluation of quotes
Adding dynamic behavior to quotes
Code injection using evaluation
Code injection using macros
The using macro
Compile-time callbacks
Before compile
After compile
On-definition
Building a music DSL in Elixir
Writing the implementation
Building the interface layer
Testing the interface
Writing integration tests
Testing sequence
Testing note and embed_notes
Testing restrictions
Adding DSL test helpers
Summary
Exercises
9
Controller and View DSL
Technical requirements
Why use metaprogramming here?
DSL design
Building the controller DSL
Adding reflections
Reflective plugs
Extracting response helpers
Building the View DSL
Building a web app using the DSLs
Creating test helpers for ease of testing and debugging
Summary
Exercises
10
Building the Router DSL
Technical requirements
Why use metaprogramming here?
DSL design
Building the router DSL
Adding reflections
Reflective routes
Building router helpers
Creating test helpers
Summary
Exercises
Index