当前位置:网站首页>API process and code structure
API process and code structure
2022-07-24 06:34:00 【Laughter addiction】
One 、HTTP API The server starts the process

1. Starting a API After the command ,API The command will first load the configuration file , Do the following processing according to the configuration .
2. Usually, log related configurations are recorded in the configuration file , After parsing the configuration file , You can load the log package initialization function , To initialize the log instance , For later programs to call .
3. Next , Initialize database instance , Establish a database connection , For later facing the database CRUD Operation and use .
4. After establishing the database connection , Need to set up HTTP, Usually include 3 Aspect settings :
4.1 Set up Header
4.2 Registered routing
4.3 Register middleware
5. And then it will call net/http Bag ListenAndServe() Method start up HTTP The server .
6. Start up HTTP Before port , Program meeting go An agreement , Come on ping HTTP Server's /sd/health Interface , If the program starts successfully ,ping Assist in timeout It will return successfully before , If the program fails to start , be ping The process will eventually timeout, And terminate the whole program .
Two 、HTTP Request processing flow

1. Establishing a connection
1. The client sends HTTP After the request , The server will resolve the domain name according to the domain name , Is to change the website name into IP Address :localhost ->127.0.0.1,Linux hosts file 、DNS Domain name resolution can realize this function .
2. Then launch TCP Three handshakes to establish a connection , After the connection is established, you can send HTTP Request the .
2. Receiving request
1.HTTP Server software process , This means API The server , After receiving the request ,
2. First of all, according to the HTTP Request line information to parse to HTTP Methods and paths , Then according to API The server registers the routing information and finds the specific processing function .( It might be understood as :HTTP Method + Mapping of path and specific processing function )
3. Processing requests
1. After receiving the request ,HTTP Frameworks generally have their own analytic functions , Just input HTTP Request message , You can parse the required request header and message body , And then deal with the business logic .
2. Usually , Business logic processing can be divided into two types : Including the operation on the database and excluding the operation on the data . In large systems, there are usually both :
1. Including operations on the database : Need to access the database ( Additions and deletions ), Then get the specified data , After data processing , Return the response package .
2. Does not include operations on the database : After business logic processing , Return the response package .
4. Record the processing of things
1. In the process of business logic processing , Some key information needs to be recorded , Convenient later stage Debug use .
2. stay Go There are a variety of log packages in the to record this information .
3、 ... and 、HTTP Request message
One HTTP The request message is sent by the request line (request line)、 Request header (header)、 Blank line and request data are composed of four parts .
1、 Composition of request header
1. The first line must be a request line (request line)
Used to describe the request type 、 The resources to be accessed and the HTTP edition
2. Then there is a head (header) Section
Used to describe additional information to be used by the server
3. Then there is an empty line
4. Finally, request data ( Call it the subject :body)
You can add any other data
Be careful :
Request any website , Use F12 You can view the request header and response header information
HTTP The response format is similar to the request format , Also by 4 Component composition : Status line 、 The message header 、 Blank lines and response data .
Four 、 Directory structure
stay Go API In the project , It usually includes many functions .
1、 Common function items
Makefile file ( Compile the file )、 Profile directory 、RESTful API Server's handler Catalog 、model Catalog 、 Tool catalog 、vendor Catalog , And the actual processing business logic function service Catalog .
2、 Specific directory structure
gin Does not provide a standardized directory structure
admin.sh -- One button stop
conf -- Profile directory
config -- Code for reading and parsing configuration files
db.sql -- Create library table
docs -- Document directory
router -- route -- url
hander -- Processing function -- go Code
main.go -- Entrance file
model -- Data model -- ORM
pkg -- Write some general processing code
util
边栏推荐
猜你喜欢
随机推荐
Jenkins自动化无人值守运行(上/下)
Leetcode sword finger offer jz73 flip word sequence
MySQL forgot to exit and close the window directly. How to delete the entire folder now
Leetcode sword finger offer JZ9 dual stack implementation queue
[214] what is an automation framework
Process and planned task management
Leetcode refers to offer jz5 to replace the space string
XML parsing
【226】wireshark的参数使用说明
文件系统与日志分析
Do not rent servers, build your own personal business website (4)
Interview questions for Test Manager / test team leader / Test Supervisor
Leetcode refers to the duplicate number in the offer jz3 array
General paging 2.0
IP notes (8)
Windows下bat脚本备份MySQL数据库
JSP tag 02
IP course (OSPF) comprehensive experiment
【251】常见的测试工具
MySQL batch modifies the data table code and character set to utf8mb4









