In this section, we will learn to develop the functional interface to realize the statistical function development of the page of the background management platform. This section will involve a variety of request routing methods.
Function is introduced
Background management platform is not only a functional management platform, but also a data management platform. From the perspective of data management platform, the management platform needs to provide real-time tracking and monitoring of various data on the platform, so as to facilitate the administrator users to see the trend of platform data. In the background function, the main interface provides statistics of users, administrators, commodities and other data. As shown below:
In the above figure, we can see that there are various data types involved in statistics and categories, which can be summarized as follows:
-
Daily growth data (users, orders, administrators)
-
Total statistics (users, orders, administrators)
-
Graph statistics (users, orders, administrators) In the last category of graph statistics, the graph shows the growth of three types of data in the last week, where the date of the last week is constantly changing.
Interface Request analysis
After seeing the above functional effects and performing functional analysis, we categorized the statistical interfaces from the request. First, open the extensions in the upper right corner of your browser, find more tools, select Developer Tools, and open debugging Tools. As shown below:
We can see the details of the interface request through the content of the interface request, for our development and use, mainly focusing on the interface request details of several data:
-
Request URL: identifies a unique request to the background. It can be uniquely identified through routing. In a specific Request interface, the part of the Request URL excluding the host domain name is the Request URL.
-
Request types: In previous lessons, we’ve looked at multiple request types, GET, POST, and so on. In a specific Request interface, the value of Request Method is the Request type.
-
Request carries data format: This field indicates how data is submitted to the server at the time of the request, or specifies what type of data is submitted to the server. In the request interface, the Content-Type field specifies the Type of data to submit to the server. The supported data formats are as follows:
-
Form Form: Application/X-www-form-urlencoded, Application /form-data
-
Json, application/Json
-
Javascript: application/Javascript
-
XML, application/XML
-
-
Request data: The specific content of the request data submitted to the server.
With these things in mind, we can develop our code in the background.
Interface statistics and classification
After understanding the important data and request data types of interface requests, we can sort out and classify these data request interfaces in the statistics module. Convenient for our server to classify processing. After combing through, we can see that there are three types of request interfaces:
-
Daily growth data interface request:
-
Daily user growth data interface request: /statis/user/ nan-nan-nan /count
-
Daily order growth data interface request: /statis/order/ nan-nan-nan /count
-
Administrator of the day add data interface request: /statis/admin/ nan-nan-nan /count
-
-
Total record data interface request:
-
Number of Users Interface request: /v1/users/count
-
Order total interface request: /bos/ Orders /count
-
Total number of administrator interface requests: /admin/count
-
-
Increasing data interface requests every day for the past 7 days:
-
Interface request for daily user growth data in recent 7 days: /statis/user/2019-04-05/count (2019-04-05 value will be changed according to the date of every day in recent 1 week)
-
/statis/order/2019-04-05/count (2019-04-05 value will be changed according to the date of every day in the last week)
-
Interface request for the administrator to add data every day in recent 7 days: /statis/admin/2019-04-05/count (the value of 2019-04-05 will be changed according to the date of every day in recent 1 week)
-
Statistics module to achieve data statistics request processing
From the above analysis, we can already see that two of the three types of data requests use the /statis prefix for data interface requests. According to the principle of project modularization development, we made the following implementation:
-
The newly created statistics module function is used to realize the interface data request of statistics function;
-
Total record data interface request, in their respective functional modules for implementation.
After the above scenario analysis, we are going to create the statistics module.
Statisticmodule Controller (StatisticController)
Since it is a brand new module, we will use MVC to build the implementation of request logic processing and business function implementation. First, we need to define the statistics module controller. The code definition is as follows:
typeStatisController Struct {// Context object Ctx iris.Context // Service implementation interface of statistics. StatisService //session Session *sessions.Session }Copy the code
Business Function Implementation (StatisService)
The above StatisController controller is only responsible for the request business logic processing, and important logical functions need to be realized in the data function provider. Here is the StatisService defined by us. For example, StatisService provides the following interfaces and definitions:
typeStatisService Interface {// Querying the increase in the number of users on a day GetUserDailyCount(Date String) int64 GetOrderDailyCount(Date String) int64 GetAdminDailyCount(date string) int64 }Copy the code
The above StatisService is only a definition of an interface standard. To decouple the code, we implement the interface through additional implementation definitions, as follows:
type statisService struct {
Engine *xorm.Engine
}Copy the code
The statisService is the structure that we define to realize the statisService interface. The structure will realize all three methods of statisService and complete the specific business logic. The specific code development will be explained in the next class.
Routing group parsing and MVC module binding
When we set up StatisController and StatisService, we need to get our controller to work for us, to accept our browser requests, and we need to start our application, Bind our StatisControll and APP objects through an MVC configuration. The following binding operations are performed:
statisService := service.NewStatisService(engine)
statis := mvc.New(app.Party("/statis/{model}/{date}/"))
statis.Register(
statisService,
sessManager.Start,
)
statis.Handle(new(controller.StatisController))Copy the code
Regular expressions recognize requests
When we analyzed and classified the requests above, we mentioned that during the request statistics, there would be URL requests with date changes, as well as requests from different modules. How to resolve URL requests for dynamic variables? This is where we apply our previous knowledge of regular expressions to identify requests.
app.Party("/statis/{model}/{date}/")Copy the code
The route group can identify requests starting with statis, and {model} is used to distinguish presentation modules, including admin, order, and user. {date} is used to indicate a date.
In this section, we have classified and analyzed the statistical functions, and started to establish our overall code logic. In the next section, we will specifically realize the code development of these functional interfaces.