Home  >  Article  >  Backend Development  >  Elegant implementation of Golang REST API architecture

Elegant implementation of Golang REST API architecture

Guanhui
Guanhuiforward
2020-06-17 17:48:334147browse

In this series, we will go through how to build a product-level REST API TODO list, a sustainable extension architecture, starting with routing and code interfaces, then adding a mongo database and a badger data layer, and then Is the authorization protocol layer (OAuth 2.0)Elegant implementation of Golang REST API architecture

In this series we will use chi routing.

Why choose to use Chi instead of the standard library or Gin or router-x for routing?

Well, actually it doesn’t matter what you choose to use. No matter what you use for routing, the concepts discussed in this series will be useful. But there are the following advantages that make me think Chi-router is superior to most alternatives: Elegant implementation of Golang REST API architecture

is 100% compatible with the

net/http

standard library-- - Any net/http compatible http or middleware pkg
  • can be used in the Go ecosystem Designed for modular/composable APIs - middleware, inline middleware, Route group and sub-router installation

  • No external dependencies ---Purely just Go 1.7 stdlib net / http

  • Powerful --- There are many Companies are using, such as: Pressly, CloudFlare, Heroku, 99Designs

  • Lightweight --- cloc'd in ~1000 LOC for the chi router

  • It's fast

  • What I like most is that the old http handlers and middleware you wrote for other net/http compatible routers will still work fine.

    Let’s get started
First, we create a main.go. The foundation (or central element?) of our program

The code above is the focus of some best practices

Elegant implementation of Golang REST API architectureUse a single package To implement routing logic, group them, and then mount them:

r.Mount("/api/todo", todo.Routes())
  1. Verify the API so that you can update the api without breaking old clients:

router.Route("/v1", .... )
  1. Use middleware as an extension. Code that uses a lot of routes is very cumbersome. In fact, it can be turned into link middleware, such as: authorization, setting response headers, compression, request logs, rate limiting, etc.

The author (based on the issues mentioned by Ajinkya in the comments, I will elaborate more on the walk method):
  1. chi routing has a method called walk. Parameters received by this method:

A router

  • A callback.

  • Every The defined routes will all be called back and receive 4 parameters:

Route definition method

  • The string of the actual route

  • Processor (function), handles requests for a given route

  • A list of middleware defined in a given route (middleware is a relatively simple function, which will be called before the handler is called, so they will be used before request processing, authorization, etc.)

  • In my case, I will simply poll Routes and prints all defined routes. This gives me an overview of all available routes.

    Next we build a todo package, which actually saves our todo logic.

Notes

Elegant implementation of Golang REST API architecture

The todo package has a method that returns all routes. These routes are written in the main.go Elegant implementation of Golang REST API architecture. In fact, I usually write these routes in a Elegant implementation of Golang REST API architecture called

routes.go,

so that it will be easy to find
    .
  • The handler has the function signature of func (w http.ResponseWriter,r *http.Request)

    , which means that this handler is no different from the net/http writing method you use the standard library.
  • Use render.JSON, an encoding/json wrapper, which will automatically escape all html in your JSON response and set the content-type to application/json

  • Are you intimidated by how easy it is? You can view this project on GitHub https://github.com/tonyalaribe/todoapi/tre....

    In our next article in this series, we will continue to support configuration and shared state. Most projects usually require additional configuration, such as database connections, etc. We will discuss this in the next article.

    Recommended tutorial: "Go Tutorial"

The above is the detailed content of Elegant implementation of Golang REST API architecture. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete