Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env

This file was deleted.

9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
JWT_SECRET=default-secret-key
TLS_CA_PATH=
TLS_CERT_PATH=
TLS_KEY_PATH=
TLS_REQUIRE_AUTH=false

ENABLE_DOCS=true
PORT=8080
TLS_PORT=8443
23 changes: 23 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Lint

on:
push:
pull_request:

jobs:
lint:
name: Run on Ubuntu
runs-on: ubuntu-latest
steps:
- name: Clone the code
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '~1.25'

- name: Run linter
uses: golangci/golangci-lint-action@v7
with:
version: v2.8.0
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Test

on:
push:
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.25.5'

- name: Test
run: go test -v ./...
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
public
.DS_Store
/.history
Makefile
Makefile
certs
.env
coverage.txt
47 changes: 47 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
version: "2"
run:
allow-parallel-runners: true
linters:
default: none
enable:
- copyloopvar
- dupl
- errcheck
- ginkgolinter
- goconst
- gocyclo
- govet
- ineffassign
- lll
- misspell
- nakedret
- prealloc
- revive
- staticcheck
- unconvert
- unparam
- unused
settings:
revive:
rules:
- name: comment-spacings
exclusions:
generated: lax
rules:
- linters:
- dupl
- lll
path: /*
paths:
- third_party$
- builtin$
- examples$
formatters:
enable:
- gofmt
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/okapi-example.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

173 changes: 129 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,167 @@
# Okapi Example

A simple example demonstrating the Okapi API Framework
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. Designed for simplicity, performance, and developer happiness, it helps you build fast, scalable, and well-documented APIs with minimal boilerplate.
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.

* [Okapi](https://github.com/jkaninda/okapi)
* [Source Code](https://github.com/jkaninda/okapi-example)
* [Docker Hub](https://hub.docker.com/r/jkaninda/okapi-example)
## 🔗 Links

## Prerequisites
- **Framework**: [Okapi on GitHub](https://github.com/jkaninda/okapi)
- **Source Code**: [okapi-example](https://github.com/jkaninda/okapi-example)
- **Docker**: [jkaninda/okapi-example](https://hub.docker.com/r/jkaninda/okapi-example)

- Go installed
- Git installed
## ✨ What's Included

## Features
This example demonstrates:

- Basic Okapi implementation example
- Okapi middlewares
- Okapi Route Definition
- Ready-to-run code structure
- **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

## Getting Started
## 🚀 Quick Start

### Clone the Repository
### Prerequisites

- Go 1.21 or higher
- Git

### Local Development

```shell
# Clone the repository
git clone https://github.com/jkaninda/okapi-example
cd okapi-example
```

### Install Dependencies

```shell
# Install dependencies
go mod tidy
```

### Run the Application

```shell
# Run the application
go run .
```

### Using Docker
The server will start at `http://localhost:8080`

### Docker Deployment

```shell
docker run --rm --name okapi-example -p 8080:8080 jkaninda/okapi-example
docker run --rm --name okapi-example \
-p 8080:8080 \
-e JWT_SIGNING_SECRET=your-secret-key \
jkaninda/okapi-example
```

### With HTTPS/TLS:

```sh
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
```
Use `JWT_SIGNING_SECRET` environment variable if you want to change JWT secret, default: `supersecret`

Visit [`http://localhost:8080`](http://localhost:8080) to see the response:
## ⚙️ Configuration

Configure the application using environment variables:

### Security

| Variable | Description | Default |
| ------------ | ------------------------ | -------------------- |
| `JWT_SECRET` | JWT token signing secret | `default-secret-key` |

### TLS/HTTPS

```json
{"message": "Welcome to the Okapi Web Framework!"}
| Variable | Description | Default |
| ------------------ | ----------------------------------------- | -------------------------- |
| `TLS_CERT_PATH` | Path to TLS certificate file | _(empty - TLS disabled)_ |
| `TLS_KEY_PATH` | Path to TLS private key file | _(empty - TLS disabled)_ |
| `TLS_CA_PATH` | Path to CA certificate for mutual TLS | _(empty - no client auth)_ |
| `TLS_REQUIRE_AUTH` | Require client certificate authentication | `false` |

### Server

| Variable | Description | Default |
| ------------- | ------------------------------- | ------- |
| `PORT` | HTTP server port | `8080` |
| `TLS_PORT` | HTTPS server port | `8443` |
| `ENABLE_DOCS` | Enable Swagger UI documentation | `true` |

### Example Configuration

Create a `.env` file in the project root:

```dotenv
# 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
```

Visit [`http://localhost:8080/docs/`](http://localhost:8080/docs/) to see the documentation
## 📚 Explore the Example

Once running, visit:

## Project Structure
- **Home Page**: [http://localhost:8080](http://localhost:8080) - Welcome page with example overview
- **API Documentation**: [http://localhost:8080/docs/](http://localhost:8080/docs/) - Interactive Swagger UI

## 📁 Project Structure

```
.
├── main.go # Main application file
├── middlewares # Middlewares package
├── controllers # Controllers package
├── routes # Routes package
├── models # Models package
└── README.md # Project documentation
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
```

### Swagger UI Preview
### 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](https://raw.githubusercontent.com/jkaninda/okapi-example/main/home-page.png)

### Interactive API Documentation

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

Okapi automatically generates Swagger UI for all routes:
![Swagger UI - Auto-generated API documentation](https://raw.githubusercontent.com/jkaninda/okapi-example/main/swagger.png)

## 🎯 Next Steps

![Okapi Swagger Interface](https://raw.githubusercontent.com/jkaninda/okapi-example/main/swagger.png)
After exploring this example:

---
## License
1. **Read the Documentation**: Visit the [Okapi docs](https://github.com/jkaninda/okapi) 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

[MIT](LICENSE) - Feel free to use and modify this example.
## 📄 License

[MIT](LICENSE) - Feel free to use, modify, and distribute this example for any purpose.
Loading