Method and system for constructing RESTful service based on LiteOS system
By registering the RESTFUL route on the LiteOS system and using the Route Engine to manage the HTTP route, the LiteOS system in the existing technology has not realized the problem of web service solutions based on the RESTFUL service production environment, and the efficient RESTFUL service construction and request processing can be achieved. 降低了开发成本。
Patent Information
- Application Number
- CN202411944517.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-27
- Publication Date
- 2025-01-28
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The existing LiteOS system does not implement a web service scheme available in the production environment based on RESTful services, and the CGI technology relies on the unimplemented general operating system API, and cannot implement the CGI scheme without changing the web server.
By registering the restful routing on the LiteOS system, using Route Engine to manage the HTTP routing, implement HTTP Method and URL request distribution, judge static or dynamic resource requests, and perform corresponding callback processing to build a LiteOS -based production environment.
Establish a RESTFUL service on the LiteOS system, avoid the overhead of the CGI process creation and destruction, improve the request processing efficiency. The RESTFUL API is easier to understand and test, reduce development costs, and achieve production availability on Liteos.
Smart Images

Figure CN119383220A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of Internet of Things, and in particular to a method and system for constructing RESTful services based on a LiteOS system. Background Art
[0002] RESTful technology and CGI technology are both technologies for building web services. The existing LiteOS web service solution is based on CGI implementation (and this solution is not actually available in production now), and there is no implementation based on RESTful service.
[0003] LiteOS is a specific implementation of a lightweight RTOS (real-time operating system) system and an IoT operating system. This operating system is different from a general operating system. LiteOS does not have a user space and does not implement system APIs such as fork / exec / pipe of a general operating system. However, web CGI technology strongly relies on these system APIs. Therefore, it is impossible to implement the CGI solution without changing the web server implementation. This is also the current status of LiteOS services, that is, there is no web service implementation available in a production environment based on LiteOS. Summary of the invention
[0004] The purpose of the present invention is to overcome the shortcomings of the prior art, to innovate on the existing CGI scheme on the LiteOS system, to provide a method and system for building RESTful services based on the LiteOS system, to build a set of RESTful services, and to realize the use of RESTful services in the production environment based on LiteOS.
[0005] The objective of the present invention is achieved through the following technical solutions:
[0006] A method for constructing a RESTful service in a LiteOS system, the method comprising initialization and HTTP request, and the specific steps are:
[0007] S1. Initialization: Register RESTful routes and transmit the registration information to Route Engine for processing;
[0008] S2.HTTP request: Send an HTTP request to the HTTP router (HTTP METHOD+URL) to request resource distribution, and then return it through the HTTP response and reply to the browser.
[0009] Furthermore, during the initialization phase, the system provides an HTTP route registration interface, through which the route information provided by each module of the system is registered to the Route Engine.
[0010] Furthermore, in the HTTP request process, during the system operation phase, after the HTTP request arrives, a determination is made as to whether it is a static file resource (ordinary web page request) or a dynamic RESTful service resource;
[0011] If it is a static resource request, the file is read and returned via HTTP response;
[0012] If it is a dynamic RESTful service resource, the HTTP Route information library is searched (the route information is stored in the HTTPRoute Engine). If a RESTful route exists, the RESTful route callback is executed and then returned through an HTTP response. If no RESTful route exists, the predefined RESTful route error callback is executed and then returned through an HTTP response.
[0013] Furthermore, after the HTTP request arrives, the static or dynamic nature of the resource is determined through HTTP routing and based on the HTTP routing decision request.
[0014] A system for building RESTful services based on LiteOS system is provided. The system adds HTTP METHOD and URL request distribution (for static resource judgment) and Route Engine (responsible for RESTful route registration / route lookup / route callback processing) for building RESTful services.
[0015] The beneficial effects of the present invention are:
[0016] (1) A RESTful service construction solution based on RESTful technology will be provided for the LiteOS system, which can realize web applications based on RESTful services. The RESTful service created by this technology avoids the overhead of CGI process creation and destruction, improves request processing efficiency, and makes RESTful API easier to understand and test.
[0017] (2) The created RESTful service complies with REST principles, is usually more clearly structured than CGI implementation, and has obvious advantages in maintainability, testability, and document description, thus reducing the mental cost of developers and the development cost of enterprises;
[0018] (3) Compared with existing technical solutions, this solution can be implemented on LiteOS and can be used in production. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 It is a flow chart of the processing process of the prior art CGI technical solution;
[0020] Figure 2 This is a diagram of the RESTful service request processing process;
[0021] Figure 3 HTTP request processing flow chart;
[0022] Figure 4 Initialize the registered routing flow chart. DETAILED DESCRIPTION
[0023] The technical solution of the present invention will be clearly and completely described below in conjunction with the embodiments. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0024] See also Figure 1 , which is a flow chart of the processing process of the prior art CGI technical solution; the complete process of HTTP request processing mainly involves the application of CGI (Common Gateway Interface) technology. The user sends an HTTP request through the browser. After the server receives the request, it will first analyze the content of the request to determine whether it conforms to the CGI mode. If the request conforms to the CGI requirements, the server will call the corresponding CGI program to process the request. These programs can be scripts or applications written in a variety of programming languages, which can dynamically generate content based on user input, such as personalized web pages or data query results.
[0025] If the request does not require CGI processing, the server will directly return the static resources stored on the server, such as HTML files, CSS style sheets, JavaScript files or pictures, etc. This method of processing static resources is relatively simple, because the server only needs to find and return the corresponding files without additional calculations or logic processing.
[0026] Ultimately, whether it is dynamic content generated by a CGI program or static resources returned directly, the server will encapsulate the result into an HTTP response and send it back to the user's browser. This response contains the result of the request, which may be a complete web page, data, or other resources.
[0027] However, the CGI technical solution relies on general operating system APIs such as fork / exec / pipe, while LiteOS does not implement these APIs and does not support the execution of user-mode applications. This technical solution is currently a theoretical solution and has not been implemented on LiteOS.
[0028] See also Figure 2This embodiment provides a method for building a RESTful service in a LiteOS system. In the web system initialization phase, RESTful route registration is performed, and a RESTful callback method is registered with HTTP route (HTTP METHOD and URL) as a key; in the HTTP request processing phase, HTTP METHOD and URL identification are performed, and ordinary web page requests or RESTful requests are distributed; in the RESTful request processing phase, RESTful route search is performed, and a RESTful callback is performed according to the matching result; and the RESTful callback result is returned to the browser through an HTTP response.
[0029] In the computer field, route engine is a component used to manage and optimize network routes. It is mainly responsible for selecting the optimal path based on network topology and traffic conditions to ensure that data packets can be efficiently and accurately transmitted from the source address to the destination address.
[0030] Specifically, the functions of Route Engine include:
[0031] Routing decision: Select the best transmission path based on network status and strategy.
[0032] Path Optimization: Dynamically adjust routes to avoid congestion and improve network efficiency.
[0033] Protocol support: Supports multiple routing protocols, such as OSPF, BGP, etc., to adapt to different network environments.
[0034] In practical applications, Route Engine is widely used in various network devices and systems, such as routers, switches, etc., to ensure the stability and efficiency of the network. In this solution, this component is mainly responsible for HTTP route callback registration, HTTP request route lookup, and HTTP request route callback execution.
[0035] See also Figure 3 , the HTTP request processing flow in this technical solution is as follows:
[0036] a. During the system initialization phase, an HTTP routing (HTTP METHOD+URL) registration interface is provided, through which the routing information provided by each module of the system is registered to the Route Engine;
[0037] b. During the system operation phase, after an HTTP request arrives, a routing decision is first made, that is, based on the HTTP route (HTTPMETHOD and URL), it is determined whether the request is for a static file resource or a dynamic RESTful service resource;
[0038] c. If it is a static resource request, it is consistent with the existing technical solution, reading the file and returning it through HTTP response;
[0039] d. If it is a RESTful service resource, perform HTTP Route search in the HTTP Route information base (the route information is stored in the HTTP Route Engine). If it exists, execute process e; if it does not exist, execute process f;
[0040] e. Find the RESTful route and execute the RESTful route callback;
[0041] f. RESTful route not found, execute predefined RESTful route error callback;
[0042] g. The result is returned via HTTP response and replied to the browser.
[0043] The following is a specific implementation process:
[0044] Assume that the following RESTful API is added through the mechanism of this embodiment:
[0045] Get all ports information / api / v1 / ports;
[0046] (1) System initialization:
[0047] See also Figure 4 , the system is initialized, and then calls the new_router() function to create a new routing engine instance. Next, the system declares an API callback function rest_get_ports, which is defined as int rest_get_ports(void* ctx) to handle specific HTTP GET requests;
[0048] After defining the callback function, the system will call the route_base_add_get method to register the callback function. The specific registration method is route_base_add_get(" / api / v1 / ports", rest_get_ports). This means that when a GET request to / api / v1 / ports is received, the routing engine will automatically call the rest_get_ports function to process the request and generate a response.
[0049] Through the initialization steps, the entire process ends with the successful initialization of the routing engine, ensuring that it can correctly respond to and process the corresponding HTTP requests. This process is used to set up and manage API routes in Web applications, providing a basis for processing client requests, allowing applications to dynamically respond to different request types and paths, thereby achieving flexible functional expansion.
[0050] (2) HTTP request processing flow:
[0051] Receive HTTP request GET / api / v1 / ports;
[0052] First, determine whether it is a static resource access. This is not a static resource, and the request is a RESTful API.
[0053] Call route_base_find to find the route (the information is stored in Route Engine). If it does not exist, execute the predefined error callback. Here, there is a GET / api / v1 / ports request route, and execute the normal route callback;
[0054] Execute the rest_get_ports callback and reply with an HTTP response to the browser.
[0055] By comparison, we can see that this embodiment innovatively removes the CGI processing related modules, adds HTTPMETHOD and URL request distribution modules (for static resource decision), and Route Engine (responsible for RESTful route registration / route lookup / route callback processing). Based on this solution, the following effects can be achieved:
[0056] 1). Based on the solution of the present invention, RESTful services can be constructed;
[0057] 2). Based on the solution of the present invention, a web application based on RESTful service can be constructed;
[0058] 3) Based on the solution of the present invention, it can be actually implemented in production activities.
[0059] The key to this embodiment is to add Route Engine, which is responsible for implementing route callback registration based on HTTP METHOD+URL, and for route lookup and route callback execution when HTTP URL request arrives. Therefore, any RESTful service, web service, HTTP service, and RPC service implemented on the LiteOS system based on this idea all fall within the scope of this method. And each processing flow and program component can be merged or dispersed into multiple components.
[0060] The use of this method will provide a RESTful service construction solution based on RESTful technology for the LiteOS system, which can realize web applications based on RESTful services. The RESTful service created by this technology avoids the overhead of CGI process creation and destruction, improves request processing efficiency, and makes RESTful API easier to understand and more convenient to test. Compared with CGI implementation, the RESTful service created by this technology complies with REST principles, usually has a clearer structure, and has obvious advantages in maintainability, testability, and document description, reducing the mental cost of developers and the development cost of enterprises.
[0061] Compared with the original technical solution, this technical solution can be actually implemented on LiteOS and can be used in production.
[0062] The above is only a preferred embodiment of the present invention. It should be understood that the present invention is not limited to the form disclosed herein, and should not be regarded as excluding other embodiments, but can be used in various other combinations, modifications and environments, and can be modified within the scope of the concept described herein through the above teachings or the technology or knowledge of the relevant field. The changes and modifications made by those skilled in the art shall not deviate from the spirit and scope of the present invention, and shall be within the scope of protection of the claims attached to the present invention.
Claims
1. A method for constructing a RESTful service in a LiteOS system, characterized in that: The method includes initialization and HTTP request, and the specific steps are: S1. Initialization: Register RESTful routes and transmit the registration information to Route Engine for processing; S2.HTTP request: Send HTTP request to HTTP routing request to distribute resources, and then return it through HTTP response and reply to the browser.
2. A method for constructing a RESTful service in a LiteOS system according to claim 1, characterized in that: In the initialization phase, an HTTP route registration interface is provided, through which the route information provided by each module of the system is registered to the Route Engine.
3. According to claim 1, a method for constructing a RESTful service in a LiteOS system, characterized in that: In the HTTP request process, during the system operation phase, after the HTTP request arrives, a judgment is made as to whether it is a static file resource or a dynamic RESTful service resource; If it is a static resource request, the file is read and returned via HTTP response; If it is a dynamic RESTful service resource, the HTTP Route information library is searched. If a RESTful route exists, the RESTful route callback is executed and then returned through an HTTP response; If no RESTful route exists, the predefined RESTful route error callback is executed and then returned through an HTTP response.
4. A method for constructing a RESTful service in a LiteOS system according to claim 3, characterized in that: After the HTTP request arrives, the static or dynamic nature of the resource is determined through HTTP routing and based on the HTTP routing decision request.
5. A system for building RESTful services based on LiteOS system, characterized in that: The system is used to perform the method according to any one of claims 1-4.
Citation Information
Patent Citations
In-vehicle communication system and method based on HTTP
CN113014621A
AIOT PaaS Internet of Things operation platform
CN113382058A
Participant-managed, independent-trust authentication service for secure messaging
US20210367941A1