Build Your Own Web Framework in Elixir 9781801812542

Discover the secrets of building high-performing and scalable web applications with clean code using Elixir's metap

1,384 21 2MB

English Pages 538 Year 2023

Report DMCA / Copyright

DOWNLOAD FILE

Build Your Own Web Framework in Elixir
 9781801812542

Table of contents :
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

Polecaj historie