mirror of
https://github.com/nasa/trick.git
synced 2025-06-16 06:08:17 +00:00
Merge Caleb Herpins port of Trick webserver from Mongoose to CivetWeb #730.
This commit is contained in:
@ -1,77 +1,79 @@
|
||||
##Extending the HTTP-API
|
||||
|
||||
The HTTP-API is implemented as a collection of ```httpMethodHandlers```. An ```httpMethodHandler``` is a pointer to a function that is expected to respond to an HTTP GET request, using the **Cesanta Mongoose** framework. An ```httpMethodHandler``` is defined (in ```trick/WebServer.hh```) as follows:
|
||||
|
||||
|
||||
|
||||
|
||||
## Extending the HTTP-API
|
||||
|
||||
```c
|
||||
typedef void (*httpMethodHandler)(struct mg_connection*, struct http_message*);
|
||||
```
|
||||
|
||||
Documentation for the **Cesanta Mongoose Networking Library** can be found at:
|
||||
[https://cesanta.com/docs/overview/intro.html](https://cesanta.com/docs/overview/intro.html)
|
||||
|
||||
## Example HTTP-API Extension
|
||||
|
||||
Suppose you want your web server to send you a JSON message:
|
||||
|
||||
```json
|
||||
{ "greeting" : "Hello Trick Sim Developer!" }
|
||||
```
|
||||
|
||||
when you invoke the URL: ```http://localhost:8888/api/http/hello```.
|
||||
|
||||
### Creating an ```httpMethodHandler```.
|
||||
|
||||
The following two files will be our implementation of an ```httpMethodHandler```. We'll put these in some models directory ```httpMethods/```.
|
||||
|
||||
**```handle_HTTP_GET_hello.h```**
|
||||
The HTTP-API is implemented as a collection of ```httpMethodHandlers```. An ```httpMethodHandler``` is a pointer to a function that is expected to respond to an HTTP GET request, using the **CivetWeb** framework. An ```httpMethodHandler``` is defined (in ```trick/CivetWeb.hh```) as follows:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
```c
|
||||
typedef void (*httpMethodHandler)(struct mg_connection *, void* cbdata);
|
||||
```
|
||||
|
||||
Documentation for the **CivetWeb Networking Library** can be found at:
|
||||
[https://cesanta.com/docs/overview/intro.html](http://civetweb.github.io/civetweb/)
|
||||
|
||||
## Example HTTP-API Extension
|
||||
|
||||
Suppose you want your web server to send you a JSON message:
|
||||
|
||||
```json
|
||||
{ "greeting" : "Hello Trick Sim Developer!" }
|
||||
```
|
||||
|
||||
when you invoke the URL: ```http://localhost:8888/api/http/hello```.
|
||||
|
||||
### Creating an ```httpMethodHandler```.
|
||||
|
||||
The following two files will be our implementation of an ```httpMethodHandler```. We'll put these in some models directory ```httpMethods/```.
|
||||
|
||||
**```handle_HTTP_GET_hello.h```**
|
||||
|
||||
```c
|
||||
#ifndef HANDLE_HTTP_GET_HELLO
|
||||
#define HANDLE_HTTP_GET_HELLO
|
||||
|
||||
#ifndef SWIG
|
||||
void handle_HTTP_GET_hello(struct mg_connection *nc, struct http_message *hm);
|
||||
void handle_HTTP_GET_hello(struct mg_connection *nc, void *hm);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
```
|
||||
|
||||
**```handle_HTTP_GET_hello.c```**
|
||||
|
||||
```c
|
||||
#include "mongoose/mongoose.h"
|
||||
|
||||
void handle_HTTP_GET_hello(struct mg_connection *nc, struct http_message *hm) {
|
||||
**```handle_HTTP_GET_hello.c```**
|
||||
|
||||
```c
|
||||
#include "civet/CivetServer.h"
|
||||
#include "civet/civetweb.h"
|
||||
#include <string.h>
|
||||
|
||||
void handle_HTTP_GET_hello(struct mg_connection *nc, void *hm) {
|
||||
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||
const char* json_text =
|
||||
"{ \"greeting\" : \"Hello Trick Sim Developer!\" }";
|
||||
mg_printf_http_chunk(nc, "%s", json_text);
|
||||
mg_send_http_chunk(nc, "", 0);
|
||||
}
|
||||
```
|
||||
|
||||
### Installing our ```httpMethodHandler```.
|
||||
|
||||
We'll do this from our **S_define** file:
|
||||
|
||||
* Add ```(httpMethods/handle_HTTP_GET_hello.c)``` to the ```LIBRARY DEPENDENCIES```.
|
||||
|
||||
* Include our header file:
|
||||
|
||||
```##include "httpMethods/handle_HTTP_GET_hello.h"```
|
||||
|
||||
* In ```create_connections()``` add :
|
||||
|
||||
```c
|
||||
web.server.installHTTPGEThandler( "hello", &handle_HTTP_GET_hello );
|
||||
```
|
||||
### A Complete S_define
|
||||
|
||||
```c++
|
||||
mg_send_chunk(nc, json_text, strlen(json_text));
|
||||
mg_send_chunk(nc, "", 0);
|
||||
}
|
||||
```
|
||||
|
||||
### Installing our ```httpMethodHandler```.
|
||||
|
||||
We'll do this from our **S_define** file:
|
||||
|
||||
* Add ```(httpMethods/handle_HTTP_GET_hello.c)``` to the ```LIBRARY DEPENDENCIES```.
|
||||
|
||||
* Include our header file:
|
||||
|
||||
```##include "httpMethods/handle_HTTP_GET_hello.h"```
|
||||
|
||||
* In ```create_connections()``` add :
|
||||
|
||||
```c
|
||||
web.server.installHTTPGEThandler( "hello", &handle_HTTP_GET_hello );
|
||||
```
|
||||
### A Complete S_define
|
||||
|
||||
```c++
|
||||
/***********************TRICK HEADER*************************
|
||||
PURPOSE:
|
||||
(Cannon Numeric)
|
||||
@ -84,7 +86,7 @@ LIBRARY DEPENDENCIES:
|
||||
*************************************************************/
|
||||
|
||||
#include "sim_objects/default_trick_sys.sm"
|
||||
#include "sim_objects/WebServer.sm"
|
||||
#include "sim_objects/CivetServer.sm"
|
||||
##include "cannon/gravity/include/cannon_numeric.h"
|
||||
##include "httpMethods/handle_HTTP_GET_hello.h"
|
||||
|
||||
@ -109,5 +111,5 @@ void create_connections() {
|
||||
dyn_integloop.getIntegrator(Runge_Kutta_4, 5);
|
||||
web.server.installHTTPGEThandler( "hello", &handle_HTTP_GET_hello );
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
|
Reference in New Issue
Block a user