Authify Service
Authify is an authentication service built using Go, Fiber, and SQLite. It provides functionalities for user registration, login, and JWT-based authentication.
Features
- Fast and efficient authentication service
- Supports user registration and login functionalities
- JWT-based secure authentication
- SQLite database support with both on-disk and in-memory options
- Synchronous API ensuring better performance and concurrency handling
- Rate limiting for request control
- CORS support for secure API access from different origins
- Middleware handling for error logging, request logging, and authentication checks
Project Structure
βββ πauthify
βββ πcmd
βββ main.go
βββ πconfig
βββ config.go
βββ πdb
βββ πdatabase
βββ data.db
βββ πmigrations
βββ πdb_migrations
βββ setup.go
βββ queries.sql
βββ schema.sql
βββ sqlc.yaml
βββ πmodel
βββ db.go
βββ models.go
βββ queries.sql.go
βββ πhandler
βββ login_handler.go
βββ logout_handler.go
βββ register_handler.go
βββ πmiddleware
βββ auth_middleware.go
βββ cors_middleware.go
βββ error_handling.go
βββ rate_limiting.go
βββ request_logging.go
βββ πmodels
βββ user.go
βββ πservices
βββ jwt_service.go
βββ .env
βββ .env.example
βββ .gitignore
βββ docker-compose.yml
βββ Dockerfile
βββ go.mod
βββ go.sum
βββ LICENSE
βββ Makefile
βββ README.md
Installation
-
Clone the repository:
git clone https://github.com/pageton/authify.git
cd authify
-
Install dependencies:
go mod tidy
-
Copy the example environment file:
cp .env.example .env
Database Setup
To apply the schema for the SQLite database, run the following command:
sqlite3 ./db/database/data.db < ./db/migrations/schema.sql
This will create the necessary tables and indexes for your application.
Using sqlc
To generate the Go code from your SQL queries, use sqlc. Ensure that you have the correct sqlc.yaml configuration file.
Run the following command to generate Go code from SQL:
make sqlc-generate
.env Configuration
The .env file contains configuration variables that the project uses. Here are the key variables:
-
SECRET_KEY: The secret key used for JWT encryption. You can generate a 256-bit key using OpenSSL:
openssl rand -base64 32
-
DATABASE_PATH: The path to the SQLite database file.
-
PORT: The port on which the server runs. For Docker or public deployment, set it as 0.0.0.0:3000.
-
LIMIT: The maximum number of requests allowed per second (rate limiting).
Example .env file:
SECRET_KEY=your_generated_secret_key
DATABASE_PATH=./db/database/data.db
PORT=:3000
LIMIT=5
Running the Project
Development Mode
To run the project in development mode:
go run ./cmd
This will execute the main.go file located in the cmd folder.
Build and Run
To build and run the project in production mode:
-
Build the project:
go build -o auth ./cmd
-
Run the built binary:
./auth
Docker Setup
Build the Docker Image
make docker-build
Run the Docker Container
make docker-run
For public deployment, ensure that the port is set to 0.0.0.0:3000 in the .env file.
API Endpoints
Registration Endpoint
Login Endpoint
Logout Endpoint
Protected Endpoint
Makefile Commands
-
run:
- Runs the Go project by executing the main file located in
cmd/main.go.
-
migrate db-up:
- Runs database migrations using the setup file in
db/migrations/db_migrations/setup.go.
-
build:
- Compiles the Go project into a binary named
authfiy.
-
clean:
- Cleans up the project by removing the compiled binary.
-
rebuild:
- Cleans and rebuilds the project from scratch.
-
all:
- Builds and then immediately runs the project.
-
docker-build:
- Builds the Docker image for the project using the Dockerfile.
-
docker-run:
- Runs the Docker container and logs the output. The container will run on the host network using the specified port from
.env.
-
docker-clean:
- Stops and removes the Docker container if it's running.
-
docker-restart:
- Stops, removes, and then restarts the Docker container.
-
docker-compose-up:
- Runs the project using
docker-compose.
-
sqlc-generate:
- Generates Go code from SQL queries based on the configuration file located at
db/migrations/sqlc.yaml.
-
help:
- Displays all available
Makefile commands with a brief description.
License
This project is licensed under the MIT License - see the LICENSE file for details.