Sign inSign up

jkaninda/okapi-example

By jkaninda

•Updated 8 months ago

A simple API example demonstrating the Okapi Go Web Framework

Image
0

2.6K

jkaninda/okapi-example repository overview

⁠Okapi Example

A comprehensive example application showcasing the Go Okapi API Framework's core features and best practices.

Okapi is a modern, minimalist HTTP web framework for Go, inspired by FastAPI's elegance. Built for simplicity, performance, and developer happiness, it enables you to build fast, scalable, and well-documented APIs with minimal boilerplate.

⁠✨ What's Included

This example demonstrates:

  • Core Framework Usage - Basic Okapi implementation patterns
  • Middleware Integration - Custom and built-in middleware examples
  • Route Organization - Structured route definitions and grouping
  • Real-time Communication - Server-Sent Events (SSE) and WebSocket implementations
  • Template Rendering - HTML template integration
  • API Documentation - Automatic Swagger/OpenAPI generation
  • Production-Ready Structure - Clean, maintainable code organization

ā šŸš€ Quick Start

⁠Prerequisites
  • Go 1.21 or higher
  • Git
⁠Local Development
# Clone the repository
git clone https://github.com/jkaninda/okapi-example
cd okapi-example

# Install dependencies
go mod tidy

# Run the application
go run .

The server will start at http://localhost:8080

⁠Docker Deployment
docker run --rm --name okapi-example \
  -p 8080:8080 \
  -e JWT_SECRET=your-secret-key \
  jkaninda/okapi-example
⁠With HTTPS/TLS:
docker run --rm --name okapi-example \
  -p 8080:8080 \
  -p 8443:8443 \
  -e JWT_SECRET=your-secret-key \
  -e TLS_CERT_PATH=/certs/server.crt \
  -e TLS_KEY_PATH=/certs/server.key \
  -v /path/to/certs:/certs:ro \
  jkaninda/okapi-example

ā āš™ļø Configuration

Configure the application using environment variables:

⁠Security
VariableDescriptionDefault
JWT_SECRETJWT token signing secretdefault-secret-key
⁠TLS/HTTPS
VariableDescriptionDefault
TLS_CERT_PATHPath to TLS certificate file(empty - TLS disabled)
TLS_KEY_PATHPath to TLS private key file(empty - TLS disabled)
TLS_CA_PATHPath to CA certificate for mutual TLS(empty - no client auth)
TLS_REQUIRE_AUTHRequire client certificate authenticationfalse
⁠Server
VariableDescriptionDefault
PORTHTTP server port8080
TLS_PORTHTTPS server port8443
ENABLE_DOCSEnable Swagger UI documentationtrue
⁠Example Configuration

Create a .env file in the project root:

# Security
JWT_SECRET=your-secret-key

# TLS Configuration (optional)
#TLS_CERT_PATH=/path/to/cert.pem
#TLS_KEY_PATH=/path/to/key.pem
#TLS_CA_PATH=/path/to/ca.pem
#TLS_REQUIRE_AUTH=false

# Server Configuration
ENABLE_DOCS=true
PORT=8080
TLS_PORT=8443

ā šŸ“š Explore the Example

Once running, visit:

ā šŸ“ Project Structure

okapi-example/
ā”œā”€ā”€ main.go              # Application entry point and server setup
ā”œā”€ā”€ routes/              # API route definitions and handlers
ā”œā”€ā”€ middlewares/         # Custom middleware implementations
ā”œā”€ā”€ services/            # Business logic and service layer
ā”œā”€ā”€ models/              # Data models and structures
ā”œā”€ā”€ session/             # Session management utilities
└── README.md
⁠Architecture Highlights
  • Layered Design: Clear separation between routes, services, and models
  • Middleware Pipeline: Reusable middleware for authentication, logging, and CORS
  • Service Layer: Business logic and service layer
  • Session Management: Built-in session handling utilities

ā šŸ“ø Screenshots

⁠Home Page

Home Page - Okapi Example

⁠Interactive API Documentation

Okapi automatically generates comprehensive Swagger UI documentation for all your routes, making API exploration and testing effortless.

Swagger UI - Auto-generated API documentation

ā šŸŽÆ Next Steps

After exploring this example:

  1. Read the Documentation: Visit the Okapi docs⁠ for detailed guides
  2. Customize Routes: Modify the routes in the routes/ directory to fit your needs
  3. Add Features: Extend the example with database integration, authentication, etc.
  4. Deploy: Use the included Docker setup for production deployments

ā šŸ“„ License

MIT⁠ - Feel free to use, modify, and distribute this example for any purpose.

Tag summary

Content type

Image

Digest

sha256:57dd4dbed…

Size

14.7 MB

Last updated

8 months ago

docker pull jkaninda/okapi-example