Serve GitBook is a FastAPI-based service that fetches content from a GitBook space. This service allows you to retrieve GitBook content in different formats via a simple API.
- Fetch content from GitBook using the GitBook API.
- Supports different content formats (e.g., Markdown).
- Deployable via Docker for production environments.
Before you start, ensure you have the following installed:
- Python 3.10+
- Poetry (for dependency management)
- Docker (for containerization)
- Docker Compose (for multi-container management)
-
Clone the repository:
git clone https://github.com/rijumone/serve-gitbook.git cd serve-gitbook -
Set up your environment:
Ensure you have the
GITBOOK_ACCESS_TOKENenvironment variable set:export GITBOOK_ACCESS_TOKEN=your_access_token_here -
Install dependencies:
Use Poetry to install the project dependencies:
poetry install
-
Run the service locally:
Start the FastAPI application using Uvicorn:
poetry run uvicorn serve_gitbook.main:app --reload
The service will be available at
http://127.0.0.1:8000.
-
Build the Docker image:
docker build -t serve-gitbook . -
Run the Docker container:
docker run -d -p 8000:8000 --name serve-gitbook-container serve-gitbook
The service will be accessible at
http://localhost:8000.
For easier management in a production environment, you can use Docker Compose.
-
Create a
.envfile:In the root directory, create a
.envfile and add your environment variables:touch .env
Add your GitBook access token:
GITBOOK_ACCESS_TOKEN=your_access_token_here
-
Start the service using Docker Compose:
docker-compose up --build -d
This will build the Docker image (if not already built) and start the container.
-
Stopping the service:
To stop the service, use:
docker-compose down
Once the service is running, you can fetch content from your GitBook space using the following API endpoint:
GET /spaces/{gitbook_space_id}/content/path/{top_path}?format={format}
- Parameters:
gitbook_space_id: The ID of your GitBook space.top_path: The path to the content you wish to retrieve.format: (Optional) The format of the content (e.g.,markdown,json). Defaults tomarkdown.
Example Request:
curl "http://localhost:8000/spaces/12345/content/path/introduction?format=markdown"Example Response:
{
"content": "# Introduction\n\nThis is the introduction to our GitBook space..."
}This project is production-ready with Docker and can be deployed on any Docker-compatible infrastructure. Ensure you have properly configured your .env file with the necessary environment variables before deploying.
- Command not found: gunicorn: Ensure that
gunicornis listed in thepyproject.tomlfile under[project.dependencies]. Rebuild the Docker image if necessary. - Connection issues: Ensure that your
GITBOOK_ACCESS_TOKENis correctly set and valid.
Contributions are welcome! Please fork this repository and submit a pull request with your changes.
This project is licensed under the MIT License. See the LICENSE file for more details.