|
1 | 1 | # Rest API
|
2 | 2 |
|
3 |
| -It is possible to run **rust-code-analysis-cli** as a `HTTP` service using |
4 |
| -`REST API` to share data between client and server. |
5 |
| -We will use the port `9090` to show you the possible ways to |
6 |
| -interact with the server. |
| 3 | +**rust-code-analysis-web** is a web server that allows users to analyze source code through a REST API. This service is useful for anyone looking to perform code analysis over HTTP. |
7 | 4 |
|
8 |
| -## Server |
| 5 | +The server can be run on any host and port, and supports the following main functionalities: |
9 | 6 |
|
10 |
| -**rust-code-analysis-cli** can act as a server running on your `localhost` |
11 |
| -at a specific port. |
| 7 | +- Remove Comments from source code. |
| 8 | +- Retrieve Function Spans for given code. |
| 9 | +- Compute Metrics for the provided source code. |
12 | 10 |
|
| 11 | + |
| 12 | +## Running the Server |
| 13 | + |
| 14 | +To run the server, you can use the following command: |
| 15 | + |
| 16 | +```sh |
| 17 | +rust-code-analysis-web --host 127.0.0.1 --port 9090 |
13 | 18 | ```
|
14 |
| -rust-code-analysis-cli --serve --port 9090 |
| 19 | + |
| 20 | +- `--host` specifies the IP address where the server should run (default is 127.0.0.1). |
| 21 | +- `--port` specifies the port to be used (default is 8080). |
| 22 | +- `-j` specifies the number of parallel jobs (optional). |
| 23 | + |
| 24 | +## Endpoints |
| 25 | + |
| 26 | +### 1. Ping the Server |
| 27 | + |
| 28 | +Use this endpoint to check if the server is running. |
| 29 | + |
| 30 | +**Request:** |
| 31 | + |
| 32 | +```http |
| 33 | +GET http://127.0.0.1:8080/ping |
15 | 34 | ```
|
16 | 35 |
|
17 |
| -The `--port` option sets the port used by the server. One possible value |
18 |
| -could be `9090`. |
| 36 | +**Response:** |
19 | 37 |
|
20 |
| -## Ping |
| 38 | +- Status Code: `200 OK` |
| 39 | +- Body: |
21 | 40 |
|
22 |
| -If you want to ping the server, make a `GET` request at this `URL`: |
| 41 | +```json |
| 42 | +{ |
| 43 | + "message": "pong" |
| 44 | +} |
| 45 | +``` |
23 | 46 |
|
| 47 | +### 2. Remove Comments |
| 48 | + |
| 49 | +This endpoint removes comments from the provided source code. |
| 50 | + |
| 51 | +**Request:** |
| 52 | + |
| 53 | +```http |
| 54 | +POST http://127.0.0.1:8080/comments |
24 | 55 | ```
|
25 |
| -http://127.0.0.1:9090/ping |
| 56 | + |
| 57 | +**Payload:** |
| 58 | + |
| 59 | +```json |
| 60 | +{ |
| 61 | + "id": "unique-id", |
| 62 | + "file_name": "filename.ext", |
| 63 | + "code": "source code with comments" |
| 64 | +} |
26 | 65 | ```
|
27 | 66 |
|
28 |
| -## Metrics |
| 67 | +- `id`: A unique identifier for the request. |
| 68 | +- `file_name`: The name of the file being analyzed. |
| 69 | +- `code`: The source code with comments. |
29 | 70 |
|
30 |
| -To get metrics formatted as a `json` file, make a `POST` request at this `URL`: |
| 71 | +**Response:** |
31 | 72 |
|
| 73 | +```json |
| 74 | +{ |
| 75 | + "id": "unique-id", |
| 76 | + "code": "source code without comments" |
| 77 | +} |
32 | 78 | ```
|
33 |
| -http://127.0.0.1:9090/metrics?file_name={filename}&unit={unit} |
| 79 | + |
| 80 | +### 3. Retrieve Function Spans |
| 81 | + |
| 82 | +This endpoint retrieves the spans of functions in the provided source code. |
| 83 | + |
| 84 | +**Request:** |
| 85 | + |
| 86 | +```http |
| 87 | +POST http://127.0.0.1:8080/functions |
34 | 88 | ```
|
35 | 89 |
|
36 |
| -The `filename` parameter represents the path to the source file to be analyzed, |
37 |
| -while `unit` is a boolean value that can assume only `0` or `1`. The latter |
38 |
| -tells **rust-code-analysis-cli** to consider only top-level metrics, while the |
39 |
| -former returns detailed metrics for all classes, functions, nested functions, |
40 |
| -and other sub-spaces. |
| 90 | +**Payload:** |
| 91 | + |
| 92 | +```json |
| 93 | +{ |
| 94 | + "id": "unique-id", |
| 95 | + "file_name": "filename.ext", |
| 96 | + "code": "source code with functions" |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +- `id`: A unique identifier for the request. |
| 101 | +- `file_name`: The name of the file being analyzed. |
| 102 | +- `code`: The source code with functions. |
| 103 | + |
| 104 | +**Response:** |
| 105 | + |
| 106 | +```json |
| 107 | +{ |
| 108 | + "id": "unique-id", |
| 109 | + "spans": [ |
| 110 | + { |
| 111 | + "name": "function_name", |
| 112 | + "start_line": 1, |
| 113 | + "end_line": 10 |
| 114 | + } |
| 115 | + ] |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### 4. Compute Metrics |
| 120 | + |
| 121 | +This endpoint computes various metrics for the provided source code. |
| 122 | + |
| 123 | +**Request:** |
| 124 | + |
| 125 | +```http |
| 126 | +POST http://127.0.0.1:8080/metrics |
| 127 | +``` |
| 128 | + |
| 129 | +**Payload:** |
| 130 | + |
| 131 | +```json |
| 132 | +{ |
| 133 | + "id": "unique-id", |
| 134 | + "file_name": "filename.ext", |
| 135 | + "code": "source code for metrics" |
| 136 | + "unit": false |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +- `id`: Unique identifier for the request. |
| 141 | +- `file_name`: The filename of the source code file. |
| 142 | +- `code`: The source code to analyze. |
| 143 | +- `unit`: A boolean value. `true` to compute only top-level metrics, `false` for detailed metrics across all units (functions, classes, etc.). |
| 144 | + |
| 145 | +**Response:** |
| 146 | + |
| 147 | +```json |
| 148 | +{ |
| 149 | + "id": "unique-id", |
| 150 | + "language": "Rust", |
| 151 | + "spaces": { |
| 152 | + "metrics": { |
| 153 | + "cyclomatic_complexity": 5, |
| 154 | + "lines_of_code": 100, |
| 155 | + "function_count": 10 |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
0 commit comments