6

Interfaces 101 : Extensible API Handlers Ep. 10

 1 year ago
source link: https://www.ardanlabs.com/blog/2023/04/interfaces-101-extensible-api-handlers-ep-10.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Interfaces 101 : Extensible API Handlers Ep. 10

Author image

Ardan Labs

April 3, 2023
interfaces-10.jpg?v=1

Introduction

In episode 9, Miki discussed how a command flag can be decoded into a user defined type with the Value interface. As a recap, the Value interface consists of two methods: one for serializing the underlying concrete type and one for deserializing a string into an object. To put this interface to the test, Miki wrote a command that had a flag to specify a network address. While implementing the Set method of the Value interface, Miki demonstrated how to add validation for your program’s flags.

In this video, Miki will build an HTTP server with one endpoint to display the health of his server. The first step he took was to define a HTTP handler to illustrate how a Go handler becomes a HandlerFunc. Next, Miki will design a function signature for middleware with the goal of abstracting input and output as much as possible. Miki will then define a logging middleware that measures the time of a request. Watch and learn how you can write a HTTP handler that is extendable with middleware.

Things you will learn in this video

  • Define a route with Go’s http package.
  • The ideal design for a middleware function.
  • How to implement an HTTP server with composability in mind.
  • Define logging middleware with Go’s STD library.

Video


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK