Table of Contents

Preface

Part 1:Getting Started with Sanic

Chapter 1: Introduction to Sanic and Async Frameworks

Technical requirements

What is Sanic?

Leveling up

Framework versus server

Web server

Web framework

Why we use Sanic—build fast, run fast

Simple and lightweight

Unopinionated and flexible

Performant and scalable

Production-ready

Trusted by millions

Community-driven

What drives code decisions?

Summary

Chapter 2: Organizing a Project

Technical requirements

Setting up an environment and directory

Environment

Sanic CLI

Directory structure

Using blueprints effectively

Blueprint registration

Blueprint versioning

Grouping blueprints

Wiring it all up

Controlled imports

Factory pattern

Autodiscovery

Running our application

Summary

Part 2:Hands-On Sanic

Chapter 3: Routing and Intaking HTTP Requests

Technical requirements

Understanding HTTP methods

Using HTTP methods on route handlers

Advanced method routing

Method safety and request body

RESTful API design

Simplifying your endpoints with CBVs

Blanket support for OPTIONS and HEAD

Paths, slashes, and why they matter

Strict slashes

Extracting information from the path

Advanced path parameters

Custom parameter matching

Modifying matched parameter values

API versioning

Should all of my routes bump versions?

Version prefixing

Virtual hosts

Serving static content

Serving static content from Sanic

Serving static content with Nginx

Streaming static content

Summary

Chapter 4: Ingesting HTTP Data

Technical requirements

Reading cookies and headers

Headers are flexible

Authentication headers

Context headers

Sanic extracts header data for us

Headers as multi-dict

Getting information from cookies (yum!)

Reading forms, query arguments, files, JSON, and more

Query arguments

Forms and files

Consuming JSON data

Getting streaming data

Validating data

Step 1—getting started and making a decorator

Step 2—reading the handler signature

Step 3—modeling

Step 4—model hydration

Step 5—performing validations

Taking it to the next level with third-party packages

Summary

Chapter 5: Building Response Handlers

Technical requirements

Examining the HTTP response structure

The HTTP response status

Response groupings

A response through exceptions

Custom status

Headers

The response body

Rendering HTML content

Delivering HTML files

Basic templating

Using a templating engine

Serializing JSON content

Choosing a serializer

Serializing custom objects

Best practices

Streaming data

File streaming

Server-sent events for push communication

Starting with the basics

Building some SSE objects

Websockets for two-way communication

Setting response headers and cookies

Responding with a request ID

Setting response cookies

Summary

Chapter 6: Operating Outside the Response Handler

Technical requirements

Making use of ctx

Altering requests and responses with middleware

Request middleware

Response middleware

Responding early (or late) with middleware

Middleware and streaming responses

Leveraging signals for intra-worker communication

Signal definitions

Using built-in signals

Custom signals

Waiting on events

Mastering HTTP connections

Keep-Alive within Sanic

Caching data per connection

Handling exceptions like a pro

Implementing proper exception handling

Bad exception messages

Misusing statuses

Responses through raising an exception

Fallback handling

Catching exceptions

Background task processing

Adding tasks to the loop

Integrating with an outside service

Designing an in-process task queue

Summary

Chapter 7: Dealing with Security Concerns

Technical requirements

Setting up an effective CORS policy

What is the security issue with ineffective CORS?

Protecting applications from CSRF

Solutions that do not work

Solutions that do work

Samesite cookies

Protecting your Sanic app with authentication

Using API keys

Understanding session-based versus non-session-based authentication

Using sessions

JSON Web Tokens (JWTs)

Summary

Chapter 8: Running a Sanic Server

Technical requirements

Handling the server life cycle

Server listeners

Configuring an application

What is the Sanic configuration object?

How can an application's configuration object be accessed?

How can the configuration object be set?

Some general rules about configuration

Running Sanic locally

How does running Sanic locally differ from production?

Deploying to production

Choosing the right server option

How to choose a deployment strategy?

Securing your application with TLS

Setting up TLS in Sanic

Getting and renewing a certificate from Let's Encrypt

Deployment examples

PaaS

Kubernetes (as-a-service)

Summary

Part 3:Putting It All together

Chapter 9: Best Practices to Improve Your Web Applications

Technical requirements

Implementing practical real-world exception handlers

Catching errors with middleware

Catching errors with signals

Catching the error and responding manually

Modifying ErrorHandler

Setting up a testable application

Getting started with sanic-testing

A more practical test client implementation

Using ReusableClient for testing

Gaining insight from logging and tracing

Types of Sanic loggers

Creating your own loggers, my first step in application development

Configuring logging

Adding color context

Adding some basic tracing with request IDs

Using X-Request-ID

Managing database connections

To ORM or not to ORM, that is the question

Creating a custom data access layer in Sanic

Connecting Sanic to Redis

Summary

Chapter 10: Implementing Common Use Cases with Sanic

Technical requirements

Synchronizing and scaling websocket feeds

Powering a progressive web application

Dealing with subdomains and CORS

Running a development server

Designing a GraphQL API

Why would I want to use GraphQL?

Adding GraphQL to Sanic

Building a Discord bot: running Sanic from another service

Building a simple Discord bot

Running the Discord bot from Sanic

Creating an HTTP to HTTPS proxy: nesting Sanic inside Sanic

Summary

Chapter 11: A Complete Real-World Example

Technical requirements

The process of building a web application

Step 1—Define the functionality and workflow

Step 2—Decide on the technology stack

Step 3—Architect the data structures

Step 4—Plan and build the user interface

Step 5—Build the application infrastructure

Step 6—Prototype the minimally viable backend features

Step 7—Create continuous integration, deployment, and automation tools

Step 8—Iterate, iterate, iterate

Highlighting select features of the Booktracker

Development environment

Creating a better factory pattern with setup functions

The data access layer

Authentication flow

Summary

Putting it all together

Why subscribe?

Other Books You May Enjoy

Packt is searching for authors like you

Share Your Thoughts

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181