Quick Start
The Anar Framework includes a powerful CLI tool (anar) that helps you generate, scaffold, and manage your Go projects effortlessly.
Step 1: Install the anar tool
Install the latest version directly from Codeberg:
go install codeberg.org/anarproject/anar@latest
Tip: Make sure your Go
binpath is added to your system’s$PATHso theanarcommand is recognized globally.
Step 2: Generate a new project
anar init codeberg.org/alimiracle/your-project-name
cd your_project_name
This will:
- Create a new project directory named
your_project_name - Scaffold the standard project structure (
models/,handlers/,routes/, etc.) - Initialize a new Go module automatically
Step 3: Configure your environment
Create a .env file in your project root and configure your settings:
# Server
SERVER_PORT=8002
# JWT
JWT_SECRET=mysecret
JWT_EXPIRATION=72h
TOKEN_ISSUER=anar-api
# Database
DB_DRIVER=postgres
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=password
DB_NAME=anardb
DB_SSLMODE=disable
# Redis
REDIS_ENABLED=true
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_TTL=30
# SMTP
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_USER=myemail@example.com
SMTP_PASSWORD=secret
SMTP_FROM=noreply@example.com
# Application
APP_URL=http://localhost:8002
# S3 Storage
S3_ENABLED=true
S3_ENDPOINT=http://localhost:9000
S3_REGION=us-east-1
S3_ACCESS_KEY=minioadmin
S3_SECRET_KEY=minioadmin
S3_BUCKET=anar
S3_USE_SSL=false
Note: Anar uses the
.envfile for configuration across services including server, database, Redis, email, and storage.
Step 4: Create your models
You can define models manually or generate them dynamically via the CLI.
Option A: Manual
type Post struct {
gorm.Model
Title string `json:"title"`
Content string `json:"content"`
Image types.FileField `json:"image"`
}
type Comment struct {
gorm.Model
PostID uint `json:"post_id"`
Author string `json:"author"`
Content string `json:"content"`
}
Option B: Generate dynamically
# Generate a Post model
anar model Post title:string content:text image:string
# Generate a Comment model with a relation
anar model Comment post:belongsTo author:string content:text
Auto-magic: The generated models automatically include GORM tags and relationships.
Step 5: Generate the scaffold
anar scaffold
This generates:
- CRUD handlers
- Database migrations
- Boilerplate code
Step 6: Register the routes
Add your generated handlers to routes/routes.go:
func SetupRoutes(r *gin.Engine, database db.Database, config *config.Config) {
handler := handlers.Init(database, config)
jwtMiddleware := jwt.NewJWTMiddleware(config)
api := r.Group("/api")
// Public routes
api.POST("/register", handler.Register)
api.GET("/confirm/:token", handler.ConfirmEmail)
api.POST("/login", handler.Login)
// Generated model routes
api.GET("/posts", handler.GetAllPosts)
api.GET("/posts/:id", handler.GetPostByID)
api.POST("/posts", handler.AddPost)
api.PUT("/posts/:id", handler.UpdatePost)
api.DELETE("/posts/:id", handler.DeletePost)
// Protected routes
protected := api.Group("/")
protected.Use(jwtMiddleware.AuthRequired())
{
protected.POST("/comments", handler.AddComment)
protected.PUT("/comments/:id", handler.UpdateComment)
protected.DELETE("/comments/:id", handler.DeleteComment)
}
}
Pro Tip: Group routes logically using
api.Group()for better scalability.
Step 7: Build and run the project
Option A: Manual build
anar build
./anar
Option B: Live reload (Recommended)
anar run
Note:
anar runautomatically watches for code changes and restarts your app - perfect for development.
Step 8: Example workflow
# Install the anar tool
go install codeberg.org/anarproject/anar/cmd/anar@latest
# Generate a new project
anar init codeberg.org/alimiracle/your_project_name
cd your_project_name
# Configure environment and write models
# Generate scaffold
anar scaffold
# Register routes in routes/routes.go
# Build or run
anar build && ./your_project_name
# or
anar run
That’s it!
Your Anar app is now up and running!
Use anar help anytime to explore commands and options.