A simple RESTful server built with Go, using Gin framework, JWT authentication, and SQLite database.
- User registration and login
- JWT-based authentication
- Protected routes
- SQLite database
- Password hashing with bcrypt
- Environment-based configuration
- Auto-generated TypeScript SDK
- Install Go (version 1.21 or higher)
- Clone this repository
- Install dependencies:
go mod tidy
- Create a
.env
file in the root directory with the following variables:You can also set these as environment variables directly.PORT=8080 JWT_SECRET=your-secret-key-here DB_PATH=app.db
go run .
The server will start on the port specified in your configuration (default: 8080).
The API is documented using Swagger/OpenAPI. You can access the documentation at:
http://localhost:8080/swagger/index.html
The project includes an auto-generated TypeScript SDK. To generate the SDK:
-
Install the required tools:
npm install -g @openapitools/openapi-generator-cli
-
Run the generation script:
./scripts/generate-sdk.sh
The SDK will be generated in the sdk/typescript
directory.
-
Install the SDK in your TypeScript project:
npm install @vhybz/api-client
-
Import and use the client:
import { Configuration, DefaultApi } from '@vhybz/api-client'; const config = new Configuration({ basePath: 'http://localhost:8080', }); const api = new DefaultApi(config); // Register a new user const registerResponse = await api.register({ username: 'testuser', password: 'testpass', email: '[email protected]' }); // Login const loginResponse = await api.login({ username: 'testuser', password: 'testpass' }); // Get profile (requires authentication) const profileResponse = await api.getProfile({ headers: { Authorization: `Bearer ${loginResponse.data.token}` } });
The server uses environment variables for configuration. You can set these either in a .env
file or as system environment variables.
PORT
: The port the server will listen on (default: 8080)JWT_SECRET
: Secret key used for JWT token signing (required)DB_PATH
: Path to the SQLite database file (default: app.db)
- POST
/register
- Request body:
{ "username": "your_username", "password": "your_password", "email": "[email protected]" }
- POST
/login
- Request body:
{ "username": "your_username", "password": "your_password" }
- Returns a JWT token
- GET
/profile
- Requires Authorization header with JWT token
- Returns user profile information
- Passwords are hashed using bcrypt
- JWT tokens expire after 24 hours
- Protected routes require valid JWT token
- Configuration values can be set securely through environment variables