|
| 1 | +# Open API Documentation |
| 2 | + |
| 3 | +## OpenAPI / Swagger UI |
| 4 | + |
| 5 | +This project uses [utoipa](https://crates.io/crates/utoipa) and [utoipa-swagger-ui](https://crates.io/crates/utoipa-swagger-ui) to provide interactive API documentation. |
| 6 | + |
| 7 | +### Accessing the Documentation |
| 8 | + |
| 9 | +#### On GitHub Pages (Static Documentation): |
| 10 | + |
| 11 | +The project automatically publishes API documentation to GitHub Pages: |
| 12 | + |
| 13 | +- **Main Documentation**: https://mozilla-services.github.io/syncstorage-rs/ |
| 14 | +- **Rust API Docs (cargo doc)**: https://mozilla-services.github.io/syncstorage-rs/api/ |
| 15 | +- **OpenAPI/Swagger UI**: https://mozilla-services.github.io/syncstorage-rs/swagger-ui/ |
| 16 | + |
| 17 | +#### When the service is running (live deployment): |
| 18 | + |
| 19 | +- **Swagger UI (Interactive)**: `https://<your-deployment-url>/swagger-ui/` |
| 20 | +- **OpenAPI Spec (JSON)**: `https://<your-deployment-url>/api-doc/openapi.json` |
| 21 | + |
| 22 | +Replace `<your-deployment-url>` with: |
| 23 | +- **Production/Stage**: [Add your prod/stage URL here] |
| 24 | +- **Local Development**: `http://localhost:8000` (or your configured port) |
| 25 | + |
| 26 | +### API Endpoints |
| 27 | + |
| 28 | +The API is organized into three main categories: |
| 29 | + |
| 30 | +#### Syncstorage Endpoints |
| 31 | +Endpoints for Firefox Sync data storage operations: |
| 32 | +- `GET /1.5/{uid}/info/collections` - Get collection timestamps |
| 33 | +- `GET /1.5/{uid}/info/collection_counts` - Get collection counts |
| 34 | +- `GET /1.5/{uid}/info/collection_usage` - Get collection usage |
| 35 | +- `GET /1.5/{uid}/info/configuration` - Get server configuration |
| 36 | +- `GET /1.5/{uid}/info/quota` - Get quota information |
| 37 | +- `DELETE /1.5/{uid}/storage` - Delete all user data |
| 38 | +- `GET /1.5/{uid}/storage/{collection}` - Get BSOs from a collection |
| 39 | +- `POST /1.5/{uid}/storage/{collection}` - Add or update BSOs |
| 40 | +- `DELETE /1.5/{uid}/storage/{collection}` - Delete a collection or BSOs |
| 41 | +- `GET /1.5/{uid}/storage/{collection}/{bso}` - Get a specific BSO |
| 42 | +- `PUT /1.5/{uid}/storage/{collection}/{bso}` - Create or update a BSO |
| 43 | +- `DELETE /1.5/{uid}/storage/{collection}/{bso}` - Delete a specific BSO |
| 44 | + |
| 45 | +#### Tokenserver Endpoints |
| 46 | +Endpoints for Sync node allocation and authentication: |
| 47 | +- `GET /1.0/{application}/{version}` - Get sync token |
| 48 | +- `GET /__heartbeat__` - Tokenserver health check |
| 49 | + |
| 50 | +#### Dockerflow Endpoints |
| 51 | +Service health and monitoring endpoints: |
| 52 | +- `GET /__heartbeat__` - Service health check |
| 53 | +- `GET /__lbheartbeat__` - Load balancer health check |
| 54 | +- `GET /__version__` - Service version information |
| 55 | + |
| 56 | +### Maintenance |
| 57 | + |
| 58 | +When adding new endpoints: |
| 59 | +1. Add `#[utoipa::path(...)]` annotation to the handler function. |
| 60 | +2. Add the handler path to `ApiDoc` in `syncserver/src/server/mod.rs` |
| 61 | +3. If using custom types, derive `ToSchema` on request/response structs. |
| 62 | +4. Run `cargo run --example generate_openapi_spec` to verify the spec generates correctly. Follow instructions below. |
| 63 | + |
| 64 | +### Generating the OpenAPI Spec Locally |
| 65 | + |
| 66 | +You can generate the OpenAPI specification without running the server: |
| 67 | + |
| 68 | +```bash |
| 69 | +# Generate the spec to stdout |
| 70 | +cargo run --example generate_openapi_spec |
| 71 | + |
| 72 | +# Save to a file |
| 73 | +cargo run --example generate_openapi_spec > openapi.json |
| 74 | +``` |
| 75 | + |
| 76 | +### Viewing the Spec Locally (Without Running the Server) |
| 77 | + |
| 78 | +If you don't want to compile the server on your machine, you can still view the API documentation: |
| 79 | + |
| 80 | +1. **Use Docker** (simplest): |
| 81 | +This option requires you to have run `cargo run --example generate_openapi_spec > openapi.json`. |
| 82 | + ```bash |
| 83 | + docker run -p 8080:8080 -e SWAGGER_JSON=/openapi.json -v $(pwd)/openapi.json:/openapi.json swaggerapi/swagger-ui |
| 84 | + ``` |
| 85 | + Then open http://localhost:8080 |
| 86 | + |
| 87 | +2. **Generate the spec on a compatible machine** (or use CI): |
| 88 | + ```bash |
| 89 | + cargo run --example generate_openapi_spec > openapi.json |
| 90 | + ``` |
| 91 | + |
| 92 | +3. **Use online Swagger Editor**: |
| 93 | + - Go to https://editor.swagger.io/ |
| 94 | + - Copy the contents of `openapi.json` |
| 95 | + - Paste into the editor |
| 96 | + - View the interactive documentation |
| 97 | + |
| 98 | +4. **Use VS Code extension**: |
| 99 | + - Install "OpenAPI (Swagger) Editor" extension |
| 100 | + - Open `openapi.json` in VS Code |
| 101 | + - Click "Preview Swagger" to view interactive docs |
| 102 | + |
| 103 | +### Publishing to GitHub Pages |
| 104 | + |
| 105 | +The `.github/workflows/publish-docs.yaml` workflow automatically publishes these docs: |
| 106 | + |
| 107 | +1. **Generates the OpenAPI spec** using the `generate_openapi_spec` example file. |
| 108 | +2. **Downloads Swagger UI** from the official GitHub releases. |
| 109 | +3. **Replaces the default example Swagger API** with your Sync API: |
| 110 | + - The default Swagger UI comes configured to display a demo "Pet Store" API |
| 111 | + - We use `sed` to replace `https://petstore.swagger.io/v2/swagger.json` with our `openapi.json` |
| 112 | +4. **Deploys everything to GitHub Pages** at: |
| 113 | + - https://mozilla-services.github.io/syncstorage-rs/swagger-ui/ |
| 114 | + |
| 115 | +The workflow runs in parallel: |
| 116 | +- `build-mdbook` job: Builds mdBook docs + Rust cargo docs |
| 117 | +- `build-openapi` job: Generates OpenAPI spec + sets up Swagger UI |
| 118 | +- `combine-and-prepare` job: Combines both outputs |
| 119 | +- `deploy` job: Deploys to GitHub Pages |
0 commit comments