CN116346609A - Method for realizing route configuration and parameter binding based on go language - Google Patents
Method for realizing route configuration and parameter binding based on go language Download PDFInfo
- Publication number
- CN116346609A CN116346609A CN202310419311.0A CN202310419311A CN116346609A CN 116346609 A CN116346609 A CN 116346609A CN 202310419311 A CN202310419311 A CN 202310419311A CN 116346609 A CN116346609 A CN 116346609A
- Authority
- CN
- China
- Prior art keywords
- request
- response
- method body
- user
- http
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 112
- 230000004044 response Effects 0.000 claims abstract description 61
- 238000012545 processing Methods 0.000 claims description 43
- 238000004458 analytical method Methods 0.000 claims description 5
- 230000003068 static effect Effects 0.000 claims description 4
- 238000011161 development Methods 0.000 abstract description 17
- 238000012423 maintenance Methods 0.000 abstract description 2
- 238000004064 recycling Methods 0.000 abstract description 2
- 230000006870 function Effects 0.000 description 13
- 230000008569 process Effects 0.000 description 10
- 238000012795 verification Methods 0.000 description 7
- 230000009286 beneficial effect Effects 0.000 description 2
- 230000007547 defect Effects 0.000 description 1
- 238000010586 diagram Methods 0.000 description 1
- 239000012634 fragment Substances 0.000 description 1
- 239000000463 material Substances 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 238000003672 processing method Methods 0.000 description 1
Images
Classifications
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L41/00—Arrangements for maintenance, administration or management of data switching networks, e.g. of packet switching networks
- H04L41/08—Configuration management of networks or network elements
- H04L41/0803—Configuration setting
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/31—Programming languages or programming paradigms
- G06F8/315—Object-oriented languages
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/40—Transformation of program code
- G06F8/41—Compilation
- G06F8/42—Syntactic analysis
- G06F8/427—Parsing
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/70—Software maintenance or management
- G06F8/73—Program documentation
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L41/00—Arrangements for maintenance, administration or management of data switching networks, e.g. of packet switching networks
- H04L41/08—Configuration management of networks or network elements
- H04L41/0803—Configuration setting
- H04L41/0806—Configuration setting for initial configuration or provisioning, e.g. plug-and-play
-
- Y—GENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
- Y02—TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
- Y02D—CLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
- Y02D10/00—Energy efficient computing, e.g. low power processors, power management or thermal management
Abstract
The application provides a method for realizing route configuration and parameter binding based on go language, which belongs to the technical field of http route configuration, and the method configures route request parameters through annotation in a method body; utilizing an AST grammar tree to analyze and assemble a method body, creating a request object and a response object, registering the method body to a corresponding http route, and generating a rule file; and acquiring a user request, determining a method body from the rule file according to the user request, performing parameter checksum binding based on the method body, executing the method body to acquire a response, and returning the response to the user. The method and the system greatly improve the development efficiency of web services, register the method body to the corresponding http route to generate the rule file by creating the request object and the response object in advance, facilitate the development and maintenance of codes, and reduce the CPU loss of repeatedly creating the object to allocate memory for recycling.
Description
Technical Field
The application belongs to the technical field of http routing configuration, and particularly relates to a method for realizing routing configuration and parameter binding based on a go language and an AST grammar tree.
Background
With the development of cloud computing, go language has become the first productivity development language of cloud native development, and performance and development efficiency can be considered. In the field of web application development, easy-to-use web development frameworks such as gin, echo, iris, beego are also promoted, and more internet companies take go language as the first choice for web application development.
The go language shows better advantages in performance and memory than the traditional java language, but is not humanized in web development. Since there is no annotation in the go language, the request method, the request path, and the binding of the request parameters cannot be described using several simple annotations like spring mvc. Therefore, the web framework developed based on go language at present cannot bypass the process of declaring a request method and a request path through codes and acquiring request parameters from a request context and then binding the request parameters manually, the web service development efficiency is low, and a large number of redundant complicated codes with unfriendly reading exist in the link of manually processing a response body. Meanwhile, in the development process, the object is required to be continuously created for binding and then destroyed, so that the CPU loss is increased.
Disclosure of Invention
Therefore, the method for realizing routing configuration and parameter binding based on go language is beneficial to solving the problems of low web service development efficiency, poor code maintainability and high CPU loss caused by complicated http routing configuration and parameter binding flow in the existing web project development process.
In order to achieve the above purpose, the present application adopts the following technical scheme:
the application provides a method for realizing route configuration and parameter binding based on go language, which comprises the following steps:
configuring route request parameters in a method body through annotation;
analyzing and assembling the method body by utilizing an AST grammar tree, creating a request object and a response object, registering the method body to a corresponding http route, and generating a rule file;
and acquiring a user request, determining Fang Fati from the rule file according to the user request, performing parameter checksum binding based on the method body, executing the method body to acquire a response, and returning the response to the user.
Further, the configuring the routing request parameter through annotation in the method body specifically includes:
editing http request processing logic by a user through comments in a method body, and adding configuration routing request parameters in the http request processing logic; the route request parameters include route annotations, request paths, and request types.
Further, the parsing and assembling the method body by using an AST syntax tree, creating a request object and a response object, and registering the method body to a corresponding http route, and generating a rule file specifically includes:
analyzing and assembling the Fang Fati by utilizing an AST grammar tree, acquiring all http request processing logic and http routes, and compiling the http request processing logic and the http routes;
the method comprises the steps of identifying and creating a request object, a request type, a response object and a response type required in http request processing logic in advance through reflection;
registering the method body to a corresponding http route, and writing the http request processing logic, the request object, the request type, the response object, the response type, the route annotation and the request path into a static file of a memory based on a go language template engine to generate a corresponding rule file.
Further, the obtaining the user request, and determining the method body from the rule file according to the user request specifically includes:
and acquiring and analyzing a request submitted by a user, determining a request path according to an analysis result, searching a corresponding method body from the rule file according to the request path, and determining http request processing logic corresponding to the request according to the method body.
Further, the performing parameter checksum binding based on the method body, executing the method body to obtain a response, and returning the response to the user specifically includes:
verifying and verifying request parameters in the user request according to predefined rules of the user;
after the request parameters are verified to be correct, binding the request parameters to http request processing logic corresponding to the method body;
and executing http request processing logic through reflection calling, and returning the response to the user after obtaining the corresponding response.
The application adopts the technical scheme, possesses following beneficial effect at least:
according to the method for realizing route configuration and parameter binding based on the go language, route request parameters are configured in a method body through annotation; then analyzing and assembling the method body by utilizing an AST grammar tree, creating a request object and a response object, registering the method body to a corresponding http route, and generating a rule file; finally, a user request is obtained, the Fang Fati is determined from the rule file according to the user request, parameter checksum binding is performed based on the method body, the method body is executed to obtain a response, and the response is returned to the user. According to the method, based on the tag or the annotation in the AST grammar tree analysis method body, the routing annotation, the request object, the request type, the response object and the response type of the http request processing logic are declared in advance, the problem that manual operation is needed to process the response body in the request processing logic function is avoided, and the web service development efficiency is greatly improved. Meanwhile, by creating the request object and the response object in advance, the method body is registered in the corresponding http route to generate the rule file, so that development and maintenance of codes are facilitated, and CPU loss caused by repeatedly creating the object to allocate memory for recycling is reduced.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the application.
Drawings
In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below.
FIG. 1 is a flowchart illustrating a method for implementing routing configuration and parameter binding based on a go language in accordance with an exemplary embodiment;
FIG. 2 is a flowchart illustrating a method performed after a program is started, according to an exemplary embodiment;
FIG. 3 is a logic flow diagram that illustrates routing a configuration request to a background execution in accordance with an exemplary embodiment.
Detailed Description
For the purpose of making the objects, technical solutions and advantages of the present application more apparent, the technical solutions of the present application will be described in detail below.
In the application, a go web frame is transversely compared with a java web frame, and due to the lack of annotated language characteristics and the lack of dynamic proxy capability, the go language cannot describe http routing, request methods and request parameter binding through simple annotation keywords, request information needs to be acquired from the request up and down in a method body, then binding is carried out with expected request parameters or some verification is carried out, and the content which is weakly related to service codes cannot be avoided, and the request parameters must be manually acquired by a user and then the object is created for binding.
In order to more deeply understand the defects existing in the prior art, the processing procedures of http routing and parameter binding of the annotation-based java spring mvc frame with the highest awareness and the gin frame of go language are compared, and the disadvantages of gin can be clearly compared:
1. the request path is unhooked from the request type and the request parameters, and only the verification of the request can be displayed in the function body.
2. There must be a process of manually binding the request parameters with the response parameters.
Therefore, the application provides a method for realizing route configuration and parameter binding based on go language, which can realize the registration of an http processing logic handler into an http route through a simple line and even default annotation, and automatically bind request parameters and responses to a method body.
Referring to fig. 1, fig. 1 is a flowchart illustrating a method for implementing routing configuration and parameter binding based on go language according to an exemplary embodiment, and the method includes:
s1: configuring route request parameters in a method body through annotation;
s2: analyzing and assembling the method body by utilizing an AST grammar tree, creating a request object and a response object, registering the method body to a corresponding http route, and generating a rule file;
s3: and acquiring a user request, determining Fang Fati from the rule file according to the user request, performing parameter checksum binding based on the method body, executing the method body to acquire a response, and returning the response to the user.
Further, in one embodiment, the configuring the routing request parameter by annotation in the method body specifically includes:
editing http request processing logic by a user through comments in a method body, and adding configuration routing request parameters in the http request processing logic; the route request parameters include route annotations, request paths, and request types.
The http request processing logic is specifically an http request processing logic function, and a user edits codes of the http request processing logic function mainly through comments, and adds routing comments, a request path and a request type in the codes.
Further, in one embodiment, the parsing and assembling the method body by using an AST syntax tree, creating a request object and a response object, and registering the method body to a corresponding http route, and generating a rule file specifically includes:
analyzing and assembling the Fang Fati by utilizing an AST grammar tree, acquiring all http request processing logic and http routes, and compiling the http request processing logic and the http routes;
the method comprises the steps of identifying and creating a request object, a request type, a response object and a response type required in http request processing logic in advance through reflection;
registering the method body to a corresponding http route, and writing the http request processing logic, the request object, the request type, the response object, the response type, the route annotation and the request path into a static file of a memory based on a go language template engine to generate a corresponding rule file.
In a specific practical process, referring to fig. 2, the application scheme is applied to an actual application program, and after the program is started, the execution content specifically includes:
firstly, acquiring code fragments and comments written in a method body by a user based on an AST grammar tree, analyzing tags or comments of http request processing logic function codes in the method body, then compiling and analyzing all http routes and logic functions based on the AST grammar tree, and presetting route comments, request parameters (namely request objects), request types, response parameters (namely response objects) and response types of the http request processing logic function, thereby avoiding the need of manually displaying the processing request parameters in the logic function, and the operations of request type binding, response parameter binding, response type binding and the like. Wherein, the request parameter and the response parameter are both declared outside the http processing logic function. AST syntax tree (abstract syntax tree) is a way for the golang language to obtain the code and comments written by the user at compile time, and the abstract syntax tree is a data structure definition for converting nonsensical byte streams into code structures. By abstracting the syntax tree rules, the byte stream can be identified layer by layer as meaningful Go language code.
And then, reflecting the request objects needed in the http logical function which are created in advance to exchange space for time, so that the CPU loss caused by repeatedly creating the object to allocate the memory and recycle is reduced. The reflection creation process is mainly implemented with a reflection package, which implements runtime reflection, allowing a program to operate on any type of object.
And finally registering the route annotation, the request parameter and the type of the method body into a static file of the memory, and generating a rule file. The process is mainly a method for generating http registration based on a go/template package and a preset template and a request object code. The rule file is essentially an http routing and processing routing table, and after a client request (i.e. a user request) enters a server (a program background), before entering an http processing method, the routing table generated based on http registration automatically performs method verification, request parameter verification and binding.
The Go/template Go standard library provides a template, in which text/template provides a function of outputting text contents based on a template. It may be convenient to dynamically generate stub codes from metadata based on templates. Therefore, the method and the device adopt the go language template engine to generate the rule file, and can register in the http service route in the publishing and just-in-time compiling processes.
Further, in one embodiment, the obtaining the user request, and determining the method body from the rule file according to the user request specifically includes:
and acquiring and analyzing a request submitted by a user, determining a request path according to an analysis result, searching a corresponding method body from the rule file according to the request path, and determining http request processing logic corresponding to the request according to the method body.
Further, in one embodiment, the performing the parameter checksum binding based on the method body, executing the method body to obtain the response, and returning the response to the user specifically includes:
verifying and verifying request parameters in the user request according to predefined rules of the user;
after the request parameters are verified to be correct, binding the request parameters to http request processing logic corresponding to the method body;
and executing http request processing logic through reflection calling, and returning the response to the user after obtaining the corresponding response.
In the existing go web framework, browser http requests are matched to specific routes through routes, and method body content is executed, wherein the method body content is the position where a user mainly writes codes. The user obtains the request information from the request context, performs checksum verification, and then binds to the desired structure. And then executing the business logic, after the business logic is processed, converting the content responded to the browser into JSON or a specific format.
Referring to fig. 3, the execution logic after the browser request made by the user makes a background to the program specifically includes:
firstly, obtaining a request of a user, analyzing the request to obtain a request path, and searching a finger method (namely http request processing logic) from a routing table (namely a rule file) according to the request path.
And then carrying out checksum verification on the request parameters in the user request according to the predefined rules of the user, and carrying out parameter binding after the checksum verification is completed.
Finally, the service logic method edited by the user (namely http request processing logic) is called through reflection, and logic in the execution function returns a response to the browser (namely the user).
According to the method, based on the tag or the annotation in the AST grammar tree analysis method body, the routing annotation, the request object, the request type, the response object and the response type of the http request processing logic are declared in advance, the problem that manual operation is needed to process the response body in the request processing logic function is avoided, and the web service development efficiency is greatly improved. Meanwhile, by creating the request object and the response object in advance, registering the method body in the corresponding http route to generate a rule file, the code is convenient to develop and maintain, the CPU loss of repeatedly creating the object to allocate memory and recycle is reduced, and the parameter binding and the graceful declaration of the request path can be automatically realized.
It is to be understood that the same or similar parts in the above embodiments may be referred to each other, and that in some embodiments, the same or similar parts in other embodiments may be referred to.
In the description of the present specification, a description referring to terms "one embodiment," "some embodiments," "examples," "specific examples," or "some examples," etc., means that a particular feature, structure, material, or characteristic described in connection with the embodiment or example is included in at least one embodiment or example of the present application. Although embodiments of the present application have been shown and described above, it will be understood that the above embodiments are illustrative and not to be construed as limiting the application, and that variations, modifications, alternatives, and variations may be made to the above embodiments by one of ordinary skill in the art within the scope of the application.
Claims (5)
1. A method for implementing routing configuration and parameter binding based on go language, comprising:
configuring route request parameters in a method body through annotation;
analyzing and assembling the method body by utilizing an AST grammar tree, creating a request object and a response object, registering the method body to a corresponding http route, and generating a rule file;
and acquiring a user request, determining Fang Fati from the rule file according to the user request, performing parameter checksum binding based on the method body, executing the method body to acquire a response, and returning the response to the user.
2. The method for implementing routing configuration and parameter binding based on go language according to claim 1, wherein said configuring routing request parameters by annotation in the method body specifically comprises:
editing http request processing logic by a user through comments in a method body, and adding configuration routing request parameters in the http request processing logic; the route request parameters include route annotations, request paths, and request types.
3. The method for implementing routing configuration and parameter binding based on go language according to claim 1, wherein said parsing and assembling the method body using AST syntax tree, creating request objects and response objects and registering the method body to corresponding http routes, generating rule files specifically comprises:
analyzing and assembling the Fang Fati by utilizing an AST grammar tree, acquiring http request processing logic edited by a user and compiling the http request processing logic;
the method comprises the steps of identifying and creating a request object, a request type, a response object and a response type required in http request processing logic in advance through reflection;
registering the method body to a corresponding http route, and writing a request object, a request type, a response object, a response type, a route annotation and a request path into a static file of a memory based on a go language template engine to generate a corresponding rule file.
4. The method for implementing routing configuration and parameter binding based on go language according to claim 1, wherein said obtaining a user request and determining said method body from said rule file according to said user request specifically comprises:
and acquiring and analyzing a request submitted by a user, determining a request path according to an analysis result, searching a corresponding method body from the rule file according to the request path, and determining http request processing logic corresponding to the request according to the method body.
5. The method for implementing routing configuration and parameter binding based on go language according to claim 1, wherein said performing parameter checksum binding based on said method body, executing said method body to obtain a response, and returning the response to the user specifically comprises:
verifying and verifying request parameters in the user request according to predefined rules of the user;
after the request parameters are verified to be correct, binding the request parameters to http request processing logic corresponding to the method body;
and executing http request processing logic through reflection calling, and returning the response to the user after obtaining the corresponding response.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202310419311.0A CN116346609A (en) | 2023-04-19 | 2023-04-19 | Method for realizing route configuration and parameter binding based on go language |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202310419311.0A CN116346609A (en) | 2023-04-19 | 2023-04-19 | Method for realizing route configuration and parameter binding based on go language |
Publications (1)
Publication Number | Publication Date |
---|---|
CN116346609A true CN116346609A (en) | 2023-06-27 |
Family
ID=86895047
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202310419311.0A Pending CN116346609A (en) | 2023-04-19 | 2023-04-19 | Method for realizing route configuration and parameter binding based on go language |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN116346609A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116846752A (en) * | 2023-07-28 | 2023-10-03 | 钛信(上海)信息科技有限公司 | Multi-cloud resource management method and system based on business scene |
-
2023
- 2023-04-19 CN CN202310419311.0A patent/CN116346609A/en active Pending
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116846752A (en) * | 2023-07-28 | 2023-10-03 | 钛信(上海)信息科技有限公司 | Multi-cloud resource management method and system based on business scene |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN109117131B (en) | Code generation method and device, storage medium and electronic terminal | |
US8402427B2 (en) | Web application generator | |
JP5679989B2 (en) | Debug pipeline | |
US11314503B2 (en) | Software development documentation using machine learning | |
US20040044961A1 (en) | Method and system for transformation of an extensible markup language document | |
Hausmann et al. | Model-based discovery of Web Services | |
US20020078105A1 (en) | Method and apparatus for editing web document from plurality of web site information | |
US20150302110A1 (en) | Decoupling front end and back end pages using tags | |
CN110580189A (en) | method and device for generating front-end page, computer equipment and storage medium | |
CN112148356B (en) | Document generation method, interface development method, device, server and storage medium | |
US7401325B2 (en) | Method and apparatus for a configurable Java Server pages processing framework | |
CN112417178B (en) | Document template definition-based on-chain contract processing method and device and electronic equipment | |
CN115639980A (en) | Draggable front-end logic arrangement method and device for low-code platform | |
WO2023109074A1 (en) | Method and apparatus for implementing linkage function, device, storage medium, and program | |
CN111666072A (en) | Software code and document robot method | |
CN116346609A (en) | Method for realizing route configuration and parameter binding based on go language | |
CN111831384A (en) | Language switching method and device, equipment and storage medium | |
CN111984262A (en) | WeChat cascading style sheet file processing method, device, equipment and storage medium | |
US20200097260A1 (en) | Software application developer tools platform | |
Kienle et al. | Evolution of web systems | |
US20230072988A1 (en) | System and a method for automatic generation of smart contracts across blockchain platforms | |
CN110633162A (en) | Remote call implementation method and device, computer equipment and storage medium | |
CN115640279A (en) | Method and device for constructing data blood relationship | |
CN113050987A (en) | Interface document generation method and device, storage medium and electronic equipment | |
CN113419711A (en) | Page guiding method and device, electronic equipment and storage medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |