1. General flow of API server

There are two steps:

  1. Start the API server
  2. The API server processes HTTP requests

2.API server startup process

  1. Parses the configuration file to initialize the server
  2. Initialize Logger to enable logging
  3. Establish a connection to the database
  4. Setting up HTTP connections (e.g., setting up response headers, registering routes, registering middleware)

3.HTTP request processing flow

  1. DNS Domain name Resolution

DNS works like this: In this way, the local machine will send the URL to the CONFIGURED DNS server. If it can find the corresponding URL, it will return its IP address. Otherwise, the DNS will continue to send the resolution request to the upper-level DNS. The request is sent to the root until the result is obtained. Now that we have the destination IP and port number, we can open the socket connection.

  1. Establish a connection

When we enter such a request, we first establish a socket connection. Since the socket is established by IP and port (TCP connection), there is also a DNS resolution process before changing the domain name to IP. If the URL does not contain a port number, the default port number of the protocol is used.

  1. Send the request

Once the connection is successfully established, a request is sent to the Web server, typically a GET or POST command (POST is used to pass FORM parameters). The format of the GET command is GET path/file name HTTP/1.0 (the file name indicates the file being accessed, and HTTP/1.0 indicates the HTTP version used by the Web browser)

Now you can send the GET command: GET /mydir/index.html HTTP/1.0

  1. Receiving a request

The API server receives the request and processes it, first parsing to the HTTP method and path based on the HTTP request line information, and then finding the specific handler function based on the route information registered by the API server.

  1. Handle the request

After receiving the request, THE API usually parses the HTTP request packet to obtain the request header and message body, and then performs corresponding service processing based on the information. Generally, THE HTTP framework has its own parsing function. You only need to enter the HTTP request packet to parse the required request header and message body.

For example, for the get request, the header and body of the request are parsed, and the API server searches its document space for the file index.html in the subdirectory mydir based on the parsed content. If the file is found, the Web server sends the file contents to the appropriate Web browser.

Business processing can be divided into:

  1. Including operations on the database: need to access the database (add, delete, change and check), and then obtain the specified data, build the specified response structure after processing the data, and return the response package.
  2. Not including operations on the database: After business logic processing, the specified response structure is constructed and the response package is returned.

4. Select REST Web framework

To write a RESTful API server, you first need a RESTful Web framework and choose Gin with the most GitHub stars. The lightweight Gin framework has the following advantages:

  • fast

Routing based on Radix tree, small memory footprint. No reflection. Predictable API performance.

  • Middleware support

Incoming HTTP requests can be handled by a series of middleware and end operations. For example, Logger, Authorization, GZIP, and finally DB.

  • Crash handling

Gin can catch a panic that occurs in an HTTP request and recover it. That way, your server will always be available. For example, you can report this panic! To Sentry.

  • JSON validation

Gin can parse and validate the requested JSON, for example by checking for the presence of the required value.

  • Routing group

Organize routing better. Whether authorization is required, different API versions… In addition, these groups can be nested indefinitely without degrading performance.

  • Error management

Gin provides a convenient way to collect all errors that occur during HTTP requests. Finally, middleware can write them to log files, databases and send them over the network.

  • Built-in rendering

Gin provides an easy-to-use API for JSON, XML, and HTML rendering.

  • scalability

Creating a new middleware is very simple

5. Configure the solution

Viper is a complete configuration solution for Go applications. It is designed to work in applications and can handle all types of configuration requirements and formats with the following features:

  • Setting defaults
  • Read configuration information from configuration files in JSON, TOML, YAML, HCL, ENvFile, and Java Properties formats
  • Real-time monitoring and re-reading of configuration files (optional)
  • Read from an environment variable
  • Read and monitor configuration changes from a remote configuration system (ETCD or Consul)
  • Read configuration from command line arguments
  • Read configuration from buffer
  • Explicitly configured values

From the above features, Viper is undoubtedly very powerful, and Viper is also very easy to use, after the initial configuration file, To read the configuration, you simply call functions such as per.getString (), per.getint (), and per.getbool ().

Viper can perform the following operations:

  1. Find, load, and deserialize configuration files in JSON, TOML, YAML, HCL, INI, ENvFile, and Java Properties formats;
  2. Provide a mechanism to set default values for your different configuration options;
  3. Provides a mechanism to override the value of a specified option with a command line argument;
  4. Provide an alias system to easily rename parameters without breaking existing code;
  5. When a user provides the same command line or configuration file as the default, it is easy to tell the difference;