GIN framework routing registration method, device, electronic device and medium

By automatically registering routes using the reflection mechanism when the gin framework project is started, the code complexity and development difficulty caused by the manual registration interface of the gin framework is solved, and efficient routing registration and development process specifications are achieved.

CN116248571BActive Publication Date: 2024-12-06CHENGDU LUYI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202211647206.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-21
Publication Date
2024-12-06
Estimated Expiration
2042-12-21

AI Technical Summary

Technical Problem

The existing gin framework needs to manually register each interface before it is launched, resulting in bloated code, increasing the development complexity and difficulty of collaborative development, and interspersing the business code and the routing registration code.

Method used

By initializing the controller when the gin framework project is started, obtaining its memory address, using the reflection mechanism to obtain the controller information, automatically registering the route, and automatically registering the routing information.

Benefits of technology

It significantly improves the efficiency of routing registration, saves manpower and time costs, realizes the decoupling of business code and routing registration code, simplifies the development process, and improves development efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116248571B_ABST
    Figure CN116248571B_ABST
Patent Text Reader

Abstract

The present application relates to a routing registration method, device, electronic device and medium of a gin framework, and the method includes: initializing at least one controller of the gin framework and obtaining the memory address of the controller; based on the reflection mechanism, obtaining the routing information contained in each controller according to the memory address, the routing information includes the controller name and at least one method information; performing routing registration on the routing information to realize routing registration of the gin framework. The present application realizes automatic routing registration according to the naming rules of the controller, greatly improves the efficiency of routing registration, and significantly saves the manpower and time cost of routing registration; at the same time, it realizes the decoupling of business code and routing registration code, standardizes the development process, and improves development efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a routing registration method, device, electronic device and medium of a gin framework. Background Art

[0002] Currently, the gin framework needs to manually register each interface before startup and provide a processing method for each route, which makes the code too bloated, and the business code and route registration code are interspersed with each other, increasing the complexity of development and the difficulty of collaborative development. Summary of the invention

[0003] In view of the above situation, the embodiments of the present application propose a routing registration method, device, electronic device and medium of the gin framework to overcome or at least partially overcome the shortcomings of the prior art.

[0004] In a first aspect, an embodiment of the present application provides a routing registration method of a gin framework, characterized in that the method is executed by a registration server, and the method includes:

[0005] Initialize at least one controller of the gin framework and obtain the memory address of the controller;

[0006] Based on the reflection mechanism, according to the memory address, the routing information contained in each of the controllers is obtained, where the routing information includes a controller name and at least one method information;

[0007] The routing information is registered to implement routing registration of the gin framework.

[0008] In a second aspect, the present application also provides a routing registration device for a gin framework in an embodiment, and the device can deploy a registration server to implement the aforementioned routing registration method for the gin framework.

[0009] In a third aspect, an embodiment of the present application further provides an electronic device, comprising: a processor; and a memory arranged to store computer executable instructions, wherein the executable instructions, when executed, cause the processor to perform any of the above methods.

[0010] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, which stores one or more programs, and when the one or more programs are executed by an electronic device including multiple applications, the electronic device executes any of the above methods.

[0011] The method adopted in the embodiment of the present application can at least achieve the following beneficial effects:

[0012] The present application provides a routing registration method for a gin framework. When a gin project is started, its controller is initialized and the memory address of the controller is obtained. Then, based on the reflection mechanism, according to the memory address of the controller, the information including the controller name, method name, etc. in the controller is obtained. The routing information is composed of these information. Finally, the routing registration is automatically performed according to the routing information to complete the routing registration of the gin framework. The present application obtains the controller information when a project based on the gin framework is started, and realizes the automatic registration of routing according to its naming rules. Compared with the current situation in which each interface needs to be manually registered in the prior art, the efficiency of routing registration is greatly improved, and the manpower and time cost of routing registration are significantly saved; at the same time, the decoupling of the business code and the routing registration code is realized. When developing a project, developers only need to consider the business logic they are responsible for. Therefore, multi-person collaborative development will be more convenient, and there is no need to consider the conflict of version control tools, etc., which standardizes the development process and improves the development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0013] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0014] Figure 1 A schematic diagram of a process flow of a routing registration method of a gin framework according to an embodiment of the present application is shown;

[0015] Figure 2 A schematic diagram of a process flow of a routing registration method of a gin framework according to another embodiment of the present application is shown;

[0016] Figure 3 A schematic diagram of the structure of a routing registration device of a gin framework according to an embodiment of the present application is shown;

[0017] Figure 4 This is a schematic diagram of the structure of an electronic device in an embodiment of the present application. DETAILED DESCRIPTION

[0018] In order to make the purpose, technical solution and advantages of the present application clearer, the technical solution of the present application will be clearly and completely described below in combination with the specific embodiments of the present application and the corresponding drawings. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present application.

[0019] The technical solutions provided by various embodiments of the present application are described in detail below in conjunction with the accompanying drawings.

[0020] Go is a new programming language launched by Google that can reduce the complexity of code without sacrificing application performance. The Gin framework is a microframework based on the Go language, with elegant encapsulation, friendly API, clear source code comments, fast and flexible, and convenient fault tolerance. However, the Gin framework currently requires manual registration of each interface before startup and provides a processing method for each route, resulting in bloated code, and the business code and route registration code are interspersed with each other, which increases the complexity of development and the difficulty of collaborative development.

[0021] In this regard, this application is hereby filed. Figure 1 A routing registration method of a gin framework according to the present application is shown, and the method is executed by a registration server. Figure 1 It can be seen that the present application at least includes steps S110 to S130:

[0022] Step S110: Initialize at least one controller of the gin framework and obtain the memory address of the controller.

[0023] The overall concept of this application is to obtain controller information when the project starts, and automatically register routes according to their naming rules, thereby decoupling the route registration code from the business code.

[0024] It should be noted that this application is executed by a registration server, which communicates with the gin framework and is used to automatically register the routing information of the gin framework. When the automatic registration of the routing information of the gin framework is executed, the control of the gin framework lies with the registration server.

[0025] The controller of the gin framework can be one or more, and this application does not limit this. In this embodiment, the number of controllers is one for explanation. The case where there are multiple controllers is the same as the case where there is one. Multiple controllers can be processed in parallel or in series, and this application does not limit this.

[0026] First, you need to initialize the controller of the gin framework and obtain the memory address of the controller. Specifically, the initialization of at least one controller of the gin framework and obtaining the memory address of the controller include: based on the init() method, calling the Register() method under the Router file to initialize the controller; obtaining the memory address of the structure uploaded by the controller.

[0027] Among them, Router: is used to define various routing rules and conditions, and register specific routes to a handler implemented by context through HTTP service.

[0028] For a controller, each controller calls the Register() method of the Router file in the init() method and passes in the memory address of its own structure to complete the initialization of the controller.

[0029] The init() method is an initialization method that can be used to do some necessary initialization work before starting the program. This application uses this method to initialize the controller.

[0030] Step S120: Based on the reflection mechanism, according to the memory address, the routing information contained in each of the controllers is obtained, and the routing information includes a controller name and at least one method information.

[0031] Reflective programming (English: reflective programming) or reflection (English: reflection) refers to the ability of a computer program to access, detect and modify its own state or behavior at runtime. Metaphorically speaking, reflection is the ability of a program to "observe" and modify its own behavior while it is running. Reflection in the Go language also has this function. It can obtain information about the type and value of a variable during program execution, and then access or modify it. The Go language has a built-in reflect package to obtain the type and value of a variable. The corresponding methods are reflect.TypeOf() and reflect.ValueOf().

[0032] The present application cleverly utilizes the reflection mechanism to obtain the information contained in the controller. Specifically, based on the reflection mechanism, the routing information contained in each controller is obtained according to the memory address, including: based on the reflection mechanism, according to the memory address of the controller, the controller name of the controller and at least one file name of the controller are obtained; according to the controller name, a corresponding business module is constructed for the controller; based on the reflection mechanism, the method information of at least one method of the business module is obtained according to the file name, and the method information includes the method name; the controller name and at least one method name are concatenated to obtain the routing information.

[0033] After the memory address of the controller is obtained by using the Register method of Router, the name of the controller and all the file names contained in the controller are obtained based on the reflection mechanism. Further, according to the controller name, a corresponding business module is constructed for the controller. Specifically, the controller name can be used as the business module name to construct the business module. If the controller name is too long, the several characters before the controller name can be obtained as the business module name by string interception. It should be noted that the business module here refers to the business logic for registering routes for the gin framework. Then continue to use the reflection mechanism, and again use all the method information of the file name controller obtained above as the method information of the business module. For a method information, it at least includes the method name of the method. In actual scenarios, the controller usually contains multiple methods. Finally, the controller name and at least one method name are spliced ​​in a certain format to obtain the routing information. In some embodiments of the present application, the method name can be converted into a camel case name; the controller name and the camel case name are spliced ​​in a predetermined order to obtain the routing information.

[0034] After the routing information is obtained, it can be temporarily stored in the cache of the registration server, or in the memory of the registration server, which is not limited by this application. In other embodiments of the present application, for the convenience of storage, the obtained routing information is stored in a slice containing all routing information in a certain data structure for subsequent use.

[0035] Step S130: performing route registration on the routing information to implement route registration on the gin framework.

[0036] After waiting to obtain the routing information of all controllers, you can register them. Specifically, you can call the engine of the ginweb framework to automatically register all routes. Among them, Engine is used to initialize a gin object instance, which mainly contains some basic functions of the framework, such as log, middleware settings, routing control (group), and handler, context and other related methods. In the application, Engine can be used to realize automatic registration of routes.

[0037] When there are multiple pieces of routing information and the number of routing information slices is multiple, the routing information is registered to realize routing registration of the gin framework, including: traversing each of the routing information slices to extract multiple pieces of routing information; calling the engine of the gin web framework to register the extracted multiple pieces of routing information in turn to realize routing registration of the gin framework.

[0038] That is, when there are multiple routing information, a traversal mechanism can be used to register each routing information in turn. A routing information can be understood as a structure of a gin framework to the outside world. A routing information usually contains one method or several methods. After being processed in sequence, the routing registration of the gin framework is realized.

[0039] After the route registration is completed, the registration server can transfer the control right of the gin framework to the gin framework, and then the gin framework can process the external request based on the registered route information.

[0040] Depend on Figure 1 It can be seen from the method shown that the present application provides a routing registration method for the gin framework. When the gin project is started, its controller is initialized and the memory address of the controller is obtained. Then, based on the reflection mechanism, according to the memory address of the controller, the controller name, method name and other information in the controller are obtained. The information constitutes the routing information. Finally, the routing registration is automatically performed according to the routing information to complete the routing registration of the gin framework. The present application obtains the controller information when the project based on the gin framework is started, and realizes the automatic registration of the routing according to its naming rules. Compared with the current situation in the prior art where each interface needs to be manually registered, the efficiency of routing registration is greatly improved, and the manpower and time cost of routing registration are significantly saved; at the same time, the decoupling of the business code and the routing registration code is realized. When developing a project, the developer only needs to consider the business logic he is responsible for. Therefore, multi-person collaborative development will be more convenient, and there is no need to consider the conflict of version control tools, etc., which standardizes the development process and improves the development efficiency.

[0041] Figure 2 A routing registration method of the gin framework according to another embodiment of the present application is shown. Figure 2 It can be seen that this embodiment includes:

[0042] At the project entry of the gin framework based on the go language, the package where the controller is located is anonymously introduced, where the number of controllers can be multiple.

[0043] Each controller calls the Register() method of Router in the init() method and passes in its own memory address.

[0044] Based on the Router.go file, perform the following steps in sequence: obtain the name of the passed controller; obtain all methods of the passed controller; intercept the controller name, convert the method name into camelCase, concatenate the controller name and the camelCase method name to obtain the routing information, and store the routing information in the slice; traverse each routing slice, call engin of the gin framework to register the routing information in sequence; confirm that the routing registration is completed, and return the control to the gin framework.

[0045] The project is successfully started, and the gin framework can process external requests based on the registered routes.

[0046] Figure 3 A schematic diagram of the structure of a routing registration device of a gin framework according to an embodiment of the present application is shown, and the device can be deployed on a registration server for execution. Figure 3 It can be seen that the routing registration device 300 of the gin framework includes:

[0047] The initialization unit 310 is used to initialize at least one controller of the gin framework and obtain the memory address of the controller;

[0048] An acquisition unit 320, configured to acquire routing information contained in each of the controllers based on the reflection mechanism and according to the memory address, wherein the routing information includes a controller name and at least one method information;

[0049] The registration unit 330 is used to perform routing registration on the routing information to implement routing registration on the gin framework.

[0050] In some embodiments of the present application, in the above-mentioned device, the initialization unit 310 is used to call the Register() method under the Router file based on the init() method to initialize the controller; and obtain the memory address of the controller's own structure uploaded by the controller.

[0051] In some embodiments of the present application, in the above-mentioned device, the acquisition unit 320 is used to obtain the controller name of the controller and at least one file name of the controller based on the reflection mechanism and according to the memory address of the controller; build a corresponding business module for the controller based on the controller name; based on the reflection mechanism, obtain method information of at least one method of the business module according to the file name, the method information including the method name; concatenate the controller name and at least one method name to obtain the routing information.

[0052] In some embodiments of the present application, in the above-mentioned device, the acquisition unit 320 is used to convert the method name into a camelCase name; and concatenate the controller name and the camelCase name according to a first predetermined format to obtain the routing information.

[0053] In some embodiments of the present application, in the above-mentioned device, the acquisition unit 320 is further used to store the routing information in a preset format in a routing information slice before the step of performing routing registration on the routing information.

[0054] In some embodiments of the present application, in the above-mentioned device, there are multiple pieces of routing information, and the number of the routing information slices is multiple; the registration unit 330 is used to traverse each of the routing information slices and extract multiple pieces of routing information; call the engine of the gin web framework to perform routing registration for the extracted multiple pieces of routing information in turn to realize routing registration of the gin framework.

[0055] In some embodiments of the present application, the above-mentioned device also includes: a transfer unit, which is used to transfer the control of the gin framework to the gin framework after the step of performing routing registration on the routing information to realize routing registration of the gin framework, so that the gin framework processes external requests based on the registered routing information.

[0056] It should be noted that the routing registration device of the above-mentioned gin framework can implement the routing registration method of the above-mentioned gin framework one by one, which will not be repeated here one by one.

[0057] Figure 4 This is a schematic diagram of the structure of an electronic device according to an embodiment of the present application. Figure 4 At the hardware level, the electronic device includes a processor, and optionally also includes an internal bus, a network interface, and a memory. The memory may include a memory, such as a high-speed random access memory (RAM), and may also include a non-volatile memory (non-volatile memory), such as at least one disk storage. Of course, the electronic device may also include hardware required for other services.

[0058] The processor, network interface and memory can be interconnected through an internal bus, which can be an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 4 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0059] The memory is used to store the program. Specifically, the program may include a program code, and the program code includes a computer operation instruction. The memory may include a memory and a non-volatile memory, and provides instructions and data to the processor.

[0060] The processor reads the corresponding computer program from the non-volatile memory into the memory and then runs it, forming a routing registration device of the gin framework at the logical level. The processor executes the program stored in the memory and is specifically used to execute the above method.

[0061] The above application Figure 3The method performed by the routing registration device of the gin framework disclosed in the illustrated embodiment can be applied to a processor or implemented by a processor. The processor may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by an integrated logic circuit of hardware in the processor or an instruction in software form. The above processor may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present application can be implemented or executed. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application can be directly embodied as being executed by a hardware decoding processor, or executed by a combination of hardware and software modules in a decoding processor. The software module can be located in a mature storage medium in the field such as random access memory, flash memory, read-only memory, programmable read-only memory or electrically erasable programmable memory, register, etc. The storage medium is located in the memory, and the processor reads the configuration information in the memory and completes the steps of the above method in combination with its hardware.

[0062] The electronic device may also perform Figure 3 The method for executing the routing registration device of the gin framework in the Figure 3 The functions of the illustrated embodiment will not be described in detail in the embodiments of the present application.

[0063] The present application also provides a computer-readable storage medium, which stores one or more programs, wherein the one or more programs include instructions, which, when executed by an electronic device including multiple application programs, enable the electronic device to execute Figure 3 The method executed by the routing registration device of the gin framework in the illustrated embodiment is specifically used to execute the aforementioned method.

[0064] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0065] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0066] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0067] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0068] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0069] The memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.

[0070] Computer readable media include permanent and non-permanent, removable and non-removable media that can be used to store configuration information by any method or technology. Configuration information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store configuration information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0071] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the presence of other identical elements in the process, method, commodity or device including the element.

[0072] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.

[0073] The above is only an embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.

Claims

1. A routing registration method for a gin framework, characterized in that: The method is performed by a registration server, and includes: Initialize at least one controller of the gin framework and obtain the memory address of the controller; Based on the reflection mechanism, according to the memory address, the routing information contained in each of the controllers is obtained, where the routing information includes a controller name and at least one method information; Performing route registration on the routing information to implement route registration on the gin framework; Wherein, based on the reflection mechanism, obtaining the routing information contained in each controller according to the memory address includes: Based on the reflection mechanism, obtaining a controller name of the controller and at least one file name of the controller according to the memory address of the controller; According to the controller name, construct a corresponding business module for the controller; Based on the reflection mechanism, obtaining method information of at least one method of the business module according to the file name, the method information including the method name; The controller name and at least one method name are concatenated to obtain the routing information.

2. The method according to claim 1, characterized in that Initializing at least one controller of the gin framework and obtaining the memory address of the controller includes: Based on the init() method, call the Register() method under the Router file to initialize the controller; Get the memory address of the controller's own structure uploaded.

3. The method according to claim 1, characterized in that The concatenating the controller name and at least one method name to obtain the routing information includes: Convert the method name to camelCase. The controller name and the camelCase name are concatenated according to a first predetermined format to obtain the routing information.

4. The method according to claim 1, characterized in that: Before the step of registering the routing information, the method further includes: The routing information is stored in a routing information slice in a preset format.

5. The method according to claim 3, characterized in that: There are multiple pieces of routing information, and there are multiple routing information slices; The step of registering the routing information to implement the routing registration of the gin framework includes: Traversing each of the routing information slices to extract multiple routing information; The engine of the gin web framework is called to register the routes of the extracted multiple routing information in sequence, so as to realize the routing registration of the gin framework.

6. The method according to claim 1, characterized in that After the step of performing route registration on the routing information to implement route registration on the gin framework, the method further includes: The control right of the gin framework is transferred to the gin framework, so that the gin framework processes the external request based on the registered routing information.

7. A routing registration device for a gin framework, characterized in that: The device can deploy a registration server to implement the routing registration method of the gin framework described in any one of claims 1 to 6.

8. An electronic device, comprising: processor; as well as A memory arranged to store computer executable instructions, which, when executed, cause the processor to perform the method according to any one of claims 1 to 6.

9. A computer-readable storage medium storing one or more programs, which, when executed by an electronic device including a plurality of application programs, enables the electronic device to execute the method according to any one of claims 1 to 6.