Initial commit
This commit is contained in:
commit
87d28fffee
|
|
@ -0,0 +1,11 @@
|
|||
root = "."
|
||||
tmp_dir = "docker/air/tmp"
|
||||
|
||||
[build]
|
||||
bin = "docker/air/tmp/main"
|
||||
cmd = "go build -o docker/air/tmp/main cmd/main.go"
|
||||
include_ext = ["go"]
|
||||
exclude_dir = ["vendor", "tmp"]
|
||||
|
||||
[log]
|
||||
level = "debug"
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
APP_NAME=Go.Gin.Template
|
||||
IS_LOGGER=true
|
||||
|
||||
DB_HOST=postgres
|
||||
DB_USER=postgres
|
||||
DB_PASS=<your password>
|
||||
DB_NAME=<your database name>
|
||||
DB_PORT=5432
|
||||
|
||||
NGINX_PORT=80
|
||||
GOLANG_PORT=8888
|
||||
APP_ENV=localhost
|
||||
JWT_SECRET=<your secret key>
|
||||
|
||||
SMTP_HOST=smtp.gmail.com
|
||||
SMTP_PORT=587
|
||||
SMTP_SENDER_NAME="Go.Gin.Template <no-reply@testing.com>"
|
||||
SMTP_AUTH_EMAIL=<your email>
|
||||
SMTP_AUTH_PASSWORD=<your password>
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
branchName: "is-${issue.number}-${issue.title}"
|
||||
commentMessage: "Branch ${branchName} created for issue: ${issue.title}"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
## Description
|
||||
|
||||
## Screenshoot
|
||||
|
||||
(Optional)
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
# Description
|
||||
|
||||
Please include a summary of the changes and the related issue.
|
||||
|
||||
Fixes #(issue)
|
||||
|
||||
## Type of change
|
||||
|
||||
Please check all options that are relevant.
|
||||
|
||||
- [ ] Bug fix (non-breaking change which fixes an issue)
|
||||
- [ ] New feature (non-breaking change which adds functionality)
|
||||
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
|
||||
|
||||
# Screenshot
|
||||
|
||||
(Opsional)
|
||||
|
||||
# Checklist:
|
||||
|
||||
- [ ] I have performed a self-review of my code
|
||||
- [ ] I have commented my code, particularly in hard-to-understand areas
|
||||
- [ ] My changes generate no new warnings
|
||||
- [ ] I have added tests that prove my fix is effective or that my feature works
|
||||
- [ ] I have made API documentation and example response in Postman
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
name: Create Branch from Issue
|
||||
|
||||
on:
|
||||
issues:
|
||||
types: [assigned]
|
||||
|
||||
jobs:
|
||||
create_issue_branch_job:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Create Issue Branch
|
||||
uses: robvanderleek/create-issue-branch@main
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
# This workflow will build a golang project
|
||||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
|
||||
|
||||
name: Go
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
pull_request:
|
||||
branches: [ "main" ]
|
||||
|
||||
jobs:
|
||||
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.21
|
||||
|
||||
- name: Build
|
||||
# run go mod tidy and go build -v ./...
|
||||
run: go mod tidy && go build -v ./...
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
name: "Issue AutoLink"
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened]
|
||||
|
||||
jobs:
|
||||
issue-links:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: tkt-actions/add-issue-links@v1.6.0
|
||||
with:
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
branch-prefix: "is-"
|
||||
resolve: true
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
*.env
|
||||
storage/
|
||||
assets/
|
||||
volumes/
|
||||
.idea/
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2023 M Naufal Badruttamam
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
# Import .env file
|
||||
ifneq (,$(wildcard ./.env))
|
||||
include .env
|
||||
export $(shell sed 's/=.*//' .env)
|
||||
endif
|
||||
|
||||
# Variables
|
||||
CONTAINER_NAME=${APP_NAME}-app
|
||||
POSTGRES_CONTAINER_NAME=${APP_NAME}-db
|
||||
|
||||
# Commands
|
||||
dep:
|
||||
go mod tidy
|
||||
|
||||
run:
|
||||
go run cmd/main.go
|
||||
|
||||
build:
|
||||
go build -o main cmd/main.go
|
||||
|
||||
run-build: build
|
||||
./main
|
||||
|
||||
test:
|
||||
go test -v ./tests
|
||||
|
||||
# Local commands (without docker)
|
||||
migrate-local:
|
||||
go run cmd/main.go --migrate
|
||||
|
||||
seed-local:
|
||||
go run cmd/main.go --seed
|
||||
|
||||
migrate-seed-local:
|
||||
go run cmd/main.go --migrate --seed
|
||||
|
||||
init-docker:
|
||||
docker compose up -d --build
|
||||
|
||||
up:
|
||||
docker-compose up -d
|
||||
|
||||
down:
|
||||
docker-compose down
|
||||
|
||||
logs:
|
||||
docker-compose logs -f
|
||||
|
||||
# Postgres commands
|
||||
container-postgres:
|
||||
docker exec -it ${POSTGRES_CONTAINER_NAME} /bin/sh
|
||||
|
||||
create-db:
|
||||
docker exec -it ${POSTGRES_CONTAINER_NAME} /bin/sh -c "createdb --username=${DB_USER} --owner=${DB_USER} ${DB_NAME}"
|
||||
|
||||
init-uuid:
|
||||
docker exec -it ${POSTGRES_CONTAINER_NAME} /bin/sh -c "psql -U ${DB_USER} -d ${DB_NAME} -c 'CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";'"
|
||||
|
||||
# Docker commands
|
||||
container-go:
|
||||
docker exec -it ${CONTAINER_NAME} /bin/sh
|
||||
|
||||
migrate:
|
||||
docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run cmd/main.go --migrate"
|
||||
|
||||
seed:
|
||||
docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run cmd/main.go --seed"
|
||||
|
||||
migrate-seed:
|
||||
docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run cmd/main.go --migrate --seed"
|
||||
|
||||
go-tidy:
|
||||
docker exec -it ${CONTAINER_NAME} /bin/sh -c "go mod tidy"
|
||||
|
|
@ -0,0 +1,174 @@
|
|||
# Golang Gin Clean Starter
|
||||
You can join in the development (Open Source). **Let's Go!!!**
|
||||
|
||||
## Introduction 👋
|
||||
> This project implements **Clean Architecture** principles with the Controller–Service–Repository pattern. This approach emphasizes clear separation of responsibilities across different layers in Golang applications. The architecture helps keep the codebase clean, testable, and scalable by dividing application logic into distinct modules with well-defined boundaries.
|
||||
|
||||

|
||||
|
||||
## Logs Feature 📋
|
||||
|
||||
The application includes a built-in logging system that allows you to monitor and track system queries. You can access the logs through a modern, user-friendly interface.
|
||||
|
||||
### Accessing Logs
|
||||
To view the logs:
|
||||
1. Make sure the application is running
|
||||
2. Open your browser and navigate to:
|
||||
```bash
|
||||
http://your-domain/logs
|
||||
```
|
||||
|
||||
### Features
|
||||
- **Monthly Filtering**: Filter logs by selecting different months
|
||||
- **Real-time Refresh**: Instantly refresh logs with the refresh button
|
||||
- **Expandable Entries**: Click on any log entry to view its full content
|
||||
- **Modern UI**: Clean and responsive interface with glass-morphism design
|
||||
|
||||

|
||||
|
||||
|
||||
## Prerequisite 🏆
|
||||
- Go Version `>= go 1.20`
|
||||
- PostgreSQL Version `>= version 15.0`
|
||||
|
||||
## How To Use
|
||||
1. Clone the repository or **Use This Template**
|
||||
```bash
|
||||
git clone https://github.com/Caknoooo/go-gin-clean-starter.git
|
||||
```
|
||||
2. Navigate to the project directory:
|
||||
```bash
|
||||
cd go-gin-clean-starter
|
||||
```
|
||||
3. Copy the example environment file and configure it:
|
||||
```bash
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
## Available Make Commands 🚀
|
||||
The project includes a comprehensive Makefile with the following commands:
|
||||
|
||||
### Development Commands
|
||||
```bash
|
||||
make dep # Install and tidy dependencies
|
||||
make run # Run the application locally
|
||||
make build # Build the application binary
|
||||
make test # Run tests
|
||||
```
|
||||
|
||||
### Local Database Commands (without Docker)
|
||||
```bash
|
||||
make migrate-local # Run migrations locally
|
||||
make seed-local # Run seeders locally
|
||||
make migrate-seed-local # Run migrations + seeders locally
|
||||
```
|
||||
|
||||
### Docker Commands
|
||||
```bash
|
||||
make init-docker # Initialize and build Docker containers
|
||||
make up # Start Docker containers
|
||||
make down # Stop Docker containers
|
||||
make logs # View Docker logs
|
||||
```
|
||||
|
||||
### Docker Database Commands
|
||||
```bash
|
||||
make migrate # Run migrations in Docker
|
||||
make seed # Run seeders in Docker
|
||||
make migrate-seed # Run migrations + seeders in Docker
|
||||
make container-go # Access Go container shell
|
||||
make container-postgres # Access PostgreSQL container
|
||||
```
|
||||
|
||||
There are 2 ways to run the application:
|
||||
### With Docker
|
||||
1. Build and start Docker containers:
|
||||
```bash
|
||||
make init-docker
|
||||
```
|
||||
2. Run Initial UUID V4 for Auto Generate UUID:
|
||||
```bash
|
||||
make init-uuid
|
||||
```
|
||||
3. Run Migration and Seeder:
|
||||
```bash
|
||||
make migrate-seed
|
||||
```
|
||||
|
||||
### Without Docker
|
||||
1. Configure `.env` with your PostgreSQL credentials:
|
||||
```bash
|
||||
DB_HOST=localhost
|
||||
DB_USER=postgres
|
||||
DB_PASS=
|
||||
DB_NAME=
|
||||
DB_PORT=5432
|
||||
```
|
||||
2. Open the terminal and set up PostgreSQL:
|
||||
- If you haven't downloaded PostgreSQL, download it first.
|
||||
- Run:
|
||||
```bash
|
||||
psql -U postgres
|
||||
```
|
||||
- Create the database according to what you put in `.env`:
|
||||
```bash
|
||||
CREATE DATABASE your_database;
|
||||
\c your_database
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
\q
|
||||
```
|
||||
3. Install dependencies and run the application:
|
||||
```bash
|
||||
make dep # Install dependencies
|
||||
make migrate-local # Run migrations
|
||||
make seed-local # Run seeders (optional)
|
||||
make run # Start the application
|
||||
```
|
||||
|
||||
## Run Migrations, Seeder, and Script
|
||||
To run migrations, seed the database, and execute a script while keeping the application running, use the following command:
|
||||
|
||||
```bash
|
||||
go run cmd/main.go --migrate --seed --run --script:example_script
|
||||
```
|
||||
|
||||
- ``--migrate`` will apply all pending migrations.
|
||||
- ``--seed`` will seed the database with initial data.
|
||||
- ``--script:example_script`` will run the specified script (replace ``example_script`` with your script name).
|
||||
- ``--run`` will ensure the application continues running after executing the commands above.
|
||||
|
||||
#### Migrate Database
|
||||
To migrate the database schema
|
||||
```bash
|
||||
go run cmd/main.go --migrate
|
||||
```
|
||||
This command will apply all pending migrations to your PostgreSQL database specified in `.env`
|
||||
|
||||
#### Seeder Database
|
||||
To seed the database with initial data:
|
||||
```bash
|
||||
go run cmd/main.go --seed
|
||||
```
|
||||
This command will populate the database with initial data using the seeders defined in your application.
|
||||
|
||||
#### Script Run
|
||||
To run a specific script:
|
||||
```bash
|
||||
go run cmd/main.go --script:example_script
|
||||
```
|
||||
Replace ``example_script`` with the actual script name in **script.go** at script folder
|
||||
|
||||
If you need the application to continue running after performing migrations, seeding, or executing a script, always append the ``--run`` option.
|
||||
|
||||
## What did you get?
|
||||
By using this template, you get a ready-to-go architecture with pre-configured endpoints. The template provides a structured foundation for building your application using Golang with Clean Architecture principles.
|
||||
|
||||
### Postman Documentation
|
||||
You can explore the available endpoints and their usage in the [Postman Documentation](https://documenter.getpostman.com/view/29665461/2s9YJaZQCG). This documentation provides a comprehensive overview of the API endpoints, including request and response examples, making it easier to understand how to interact with the API.
|
||||
|
||||
### Issue / Pull Request Template
|
||||
|
||||
The repository includes templates for issues and pull requests to standardize contributions and improve the quality of discussions and code reviews.
|
||||
|
||||
- **Issue Template**: Helps in reporting bugs or suggesting features by providing a structured format to capture all necessary information.
|
||||
- **Pull Request Template**: Guides contributors to provide a clear description of changes, related issues, and testing steps, ensuring smooth and efficient code reviews.
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/middlewares"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/auth"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/providers"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/script"
|
||||
"github.com/samber/do"
|
||||
|
||||
"github.com/common-nighthawk/go-figure"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func args(injector *do.Injector) bool {
|
||||
if len(os.Args) > 1 {
|
||||
flag := script.Commands(injector)
|
||||
return flag
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func run(server *gin.Engine) {
|
||||
server.Static("/assets", "./assets")
|
||||
|
||||
port := os.Getenv("GOLANG_PORT")
|
||||
if port == "" {
|
||||
port = "8888"
|
||||
}
|
||||
|
||||
var serve string
|
||||
if os.Getenv("APP_ENV") == "localhost" {
|
||||
serve = "0.0.0.0:" + port
|
||||
} else {
|
||||
serve = ":" + port
|
||||
}
|
||||
|
||||
myFigure := figure.NewColorFigure("Caknoo", "", "green", true)
|
||||
myFigure.Print()
|
||||
|
||||
if err := server.Run(serve); err != nil {
|
||||
log.Fatalf("error running server: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
var (
|
||||
injector = do.New()
|
||||
)
|
||||
|
||||
providers.RegisterDependencies(injector)
|
||||
|
||||
if !args(injector) {
|
||||
return
|
||||
}
|
||||
|
||||
server := gin.Default()
|
||||
server.Use(middlewares.CORSMiddleware())
|
||||
|
||||
// Register module routes
|
||||
user.RegisterRoutes(server, injector)
|
||||
auth.RegisterRoutes(server, injector)
|
||||
|
||||
run(server)
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||
"github.com/joho/godotenv"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func RunExtension(db *gorm.DB) {
|
||||
db.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";")
|
||||
}
|
||||
|
||||
func SetUpDatabaseConnection() *gorm.DB {
|
||||
if os.Getenv("APP_ENV") != constants.ENUM_RUN_PRODUCTION {
|
||||
err := godotenv.Load(".env")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
dbUser := os.Getenv("DB_USER")
|
||||
dbPass := os.Getenv("DB_PASS")
|
||||
dbHost := os.Getenv("DB_HOST")
|
||||
dbName := os.Getenv("DB_NAME")
|
||||
dbPort := os.Getenv("DB_PORT")
|
||||
|
||||
dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v", dbHost, dbUser, dbPass, dbName, dbPort)
|
||||
|
||||
db, err := gorm.Open(postgres.New(postgres.Config{
|
||||
DSN: dsn,
|
||||
PreferSimpleProtocol: true,
|
||||
}), &gorm.Config{
|
||||
Logger: SetupLogger(),
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
RunExtension(db)
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func CloseDatabaseConnection(db *gorm.DB) {
|
||||
dbSQL, err := db.DB()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
dbSQL.Close()
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type EmailConfig struct {
|
||||
Host string `mapstructure:"SMTP_HOST"`
|
||||
Port int `mapstructure:"SMTP_PORT"`
|
||||
SenderName string `mapstructure:"SMTP_SENDER_NAME"`
|
||||
AuthEmail string `mapstructure:"SMTP_AUTH_EMAIL"`
|
||||
AuthPassword string `mapstructure:"SMTP_AUTH_PASSWORD"`
|
||||
}
|
||||
|
||||
func NewEmailConfig() (*EmailConfig, error) {
|
||||
viper.SetConfigFile(".env")
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
viper.AutomaticEnv()
|
||||
|
||||
var config EmailConfig
|
||||
if err := viper.Unmarshal(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
LOG_DIR = "./config/logs/query_log"
|
||||
)
|
||||
|
||||
func SetupLogger() logger.Interface {
|
||||
err := os.MkdirAll(LOG_DIR, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create log directory: %v", err)
|
||||
}
|
||||
|
||||
currentMonth := time.Now().Format("January")
|
||||
currentMonth = strings.ToLower(currentMonth)
|
||||
logFileName := currentMonth + "_query.log"
|
||||
|
||||
logFile, err := os.OpenFile(LOG_DIR+"/"+logFileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open log file: %v", err)
|
||||
}
|
||||
|
||||
newLogger := logger.New(
|
||||
log.New(logFile, "\r\n", log.LstdFlags),
|
||||
logger.Config{
|
||||
SlowThreshold: time.Second,
|
||||
LogLevel: logger.Info,
|
||||
Colorful: false,
|
||||
},
|
||||
)
|
||||
|
||||
return newLogger
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
*
|
||||
!.gitignore
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Timestamp struct {
|
||||
CreatedAt time.Time `gorm:"type:timestamp with time zone" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"type:timestamp with time zone" json:"updated_at"`
|
||||
}
|
||||
|
||||
type Authorization struct {
|
||||
Token string `json:"token" binding:"required"`
|
||||
Role string `json:"role" binding:"required,oneof=user admin"`
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type RefreshToken struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
UserID uuid.UUID `gorm:"type:uuid;not null" json:"user_id"`
|
||||
Token string `gorm:"type:varchar(255);not null;uniqueIndex" json:"token"`
|
||||
ExpiresAt time.Time `gorm:"type:timestamp with time zone;not null" json:"expires_at"`
|
||||
User User `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-"`
|
||||
|
||||
Timestamp
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/helpers"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
Email string `gorm:"type:varchar(255);uniqueIndex;not null" json:"email"`
|
||||
TelpNumber string `gorm:"type:varchar(20);index" json:"telp_number"`
|
||||
Password string `gorm:"type:varchar(255);not null" json:"password"`
|
||||
Role string `gorm:"type:varchar(50);not null;default:'user'" json:"role"`
|
||||
ImageUrl string `gorm:"type:varchar(255)" json:"image_url"`
|
||||
IsVerified bool `gorm:"default:false" json:"is_verified"`
|
||||
|
||||
Timestamp
|
||||
}
|
||||
|
||||
// BeforeCreate hook to hash password and set defaults
|
||||
func (u *User) BeforeCreate(_ *gorm.DB) (err error) {
|
||||
// Hash password
|
||||
if u.Password != "" {
|
||||
u.Password, err = helpers.HashPassword(u.Password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure UUID is set
|
||||
if u.ID == uuid.Nil {
|
||||
u.ID = uuid.New()
|
||||
}
|
||||
|
||||
// Set default role if not specified
|
||||
if u.Role == "" {
|
||||
u.Role = "user"
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// BeforeUpdate hook to handle password updates
|
||||
func (u *User) BeforeUpdate(_ *gorm.DB) (err error) {
|
||||
// Only hash password if it has been changed
|
||||
if u.Password != "" {
|
||||
u.Password, err = helpers.HashPassword(u.Password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Migrate(db *gorm.DB) error {
|
||||
if err := db.AutoMigrate(
|
||||
&entities.User{},
|
||||
&entities.RefreshToken{},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
CREATE DATABASE golang_template;
|
||||
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
-- migrations/users.sql
|
||||
CREATE TABLE users (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL,
|
||||
nama VARCHAR(100) NOT NULL,
|
||||
no_telp VARCHAR(30) NOT NULL,
|
||||
email VARCHAR(100) NOT NULL,
|
||||
password VARCHAR(100) NOT NULL,
|
||||
role VARCHAR(100) NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- migrations/refresh_tokens.sql
|
||||
CREATE TABLE refresh_tokens (
|
||||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
token VARCHAR(255) NOT NULL,
|
||||
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
deleted_at TIMESTAMP WITH TIME ZONE
|
||||
);
|
||||
|
||||
CREATE INDEX idx_refresh_tokens_user_id ON refresh_tokens(user_id);
|
||||
CREATE UNIQUE INDEX idx_refresh_tokens_token ON refresh_tokens(token);
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/seeders/seeds"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Seeder(db *gorm.DB) error {
|
||||
if err := seeds.ListUserSeeder(db); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
[
|
||||
{
|
||||
"name": "admin",
|
||||
"telp_number": "08123456789",
|
||||
"email": "admin1234@gmail.com",
|
||||
"password": "admin1234",
|
||||
"role": "admin",
|
||||
"is_verified": true
|
||||
},
|
||||
{
|
||||
"name": "user",
|
||||
"telp_number": "08123456789",
|
||||
"email": "user1234@gmail.com",
|
||||
"password": "user1234",
|
||||
"role": "user",
|
||||
"is_verified": true
|
||||
}
|
||||
]
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package seeds
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func ListUserSeeder(db *gorm.DB) error {
|
||||
jsonFile, err := os.Open("./database/seeders/json/users.json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
jsonData, err := io.ReadAll(jsonFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var listUser []entities.User
|
||||
if err := json.Unmarshal(jsonData, &listUser); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hasTable := db.Migrator().HasTable(&entities.User{})
|
||||
if !hasTable {
|
||||
if err := db.Migrator().CreateTable(&entities.User{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, data := range listUser {
|
||||
var user entities.User
|
||||
err := db.Where(&entities.User{Email: data.Email}).First(&user).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
isData := db.Find(&user, "email = ?", data.Email).RowsAffected
|
||||
if isData == 0 {
|
||||
if err := db.Create(&data).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
services:
|
||||
app:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: ./docker/Dockerfile
|
||||
container_name: ${APP_NAME:-go-gin-clean-starter}-app
|
||||
volumes:
|
||||
- .:/app
|
||||
ports:
|
||||
- ${GOLANG_PORT:-8888}:8888
|
||||
networks:
|
||||
- app-network
|
||||
|
||||
nginx:
|
||||
image: nginx:latest
|
||||
container_name: ${APP_NAME:-go-gin-clean-starter}-nginx
|
||||
ports:
|
||||
- ${NGINX_PORT:-81}:80
|
||||
volumes:
|
||||
- .:/var/www/html
|
||||
- ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
|
||||
depends_on:
|
||||
- app
|
||||
networks:
|
||||
- app-network
|
||||
|
||||
postgres:
|
||||
hostname: postgres
|
||||
container_name: ${APP_NAME:-go-gin-clean-starter}-db
|
||||
image: postgres:latest
|
||||
ports:
|
||||
- ${DB_PORT}:5432
|
||||
volumes:
|
||||
- ./docker/postgresql/tmp:/var/lib/postgresql/data
|
||||
- app-data:/var/lib/postgresql/data
|
||||
environment:
|
||||
- POSTGRES_USER=${DB_USER}
|
||||
- POSTGRES_PASSWORD=${DB_PASS}
|
||||
- POSTGRES_DB=${DB_NAME}
|
||||
networks:
|
||||
- app-network
|
||||
|
||||
volumes:
|
||||
app-data:
|
||||
|
||||
networks:
|
||||
app-network:
|
||||
driver: bridge
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
FROM golang:1.23-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN go install github.com/air-verse/air@latest
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN go mod tidy
|
||||
|
||||
CMD ["air"]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
*
|
||||
!.gitignore
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
server {
|
||||
listen 80;
|
||||
server_name localhost;
|
||||
|
||||
location / {
|
||||
proxy_pass http://app:8888;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection keep-alive;
|
||||
proxy_set_header Host $host;
|
||||
proxy_cache_bypass $http_upgrade;
|
||||
}
|
||||
|
||||
error_page 404 /404.html;
|
||||
location = /404.html {
|
||||
root /usr/share/nginx/html;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
*
|
||||
!.gitignore
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
module github.com/Caknoooo/go-gin-clean-starter
|
||||
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.24.1
|
||||
|
||||
require (
|
||||
github.com/Caknoooo/go-pagination v0.1.0
|
||||
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/golang-jwt/jwt/v4 v4.5.1
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/samber/do v1.6.0
|
||||
github.com/spf13/viper v1.20.0
|
||||
github.com/stretchr/testify v1.10.0
|
||||
golang.org/x/crypto v0.36.0
|
||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
|
||||
gorm.io/driver/postgres v1.5.11
|
||||
gorm.io/gorm v1.25.12
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.13.1 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.4 // indirect
|
||||
github.com/cloudwego/base64x v0.1.5 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/fsnotify/fsnotify v1.8.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||
github.com/gin-contrib/sse v1.0.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.25.0 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/pgx/v5 v5.7.2 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/sagikazarmark/locafero v0.8.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||
github.com/spf13/afero v1.14.0 // indirect
|
||||
github.com/spf13/cast v1.7.1 // indirect
|
||||
github.com/spf13/pflag v1.0.6 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/arch v0.15.0 // indirect
|
||||
golang.org/x/net v0.37.0 // indirect
|
||||
golang.org/x/sync v0.12.0 // indirect
|
||||
golang.org/x/sys v0.31.0 // indirect
|
||||
golang.org/x/text v0.23.0 // indirect
|
||||
google.golang.org/protobuf v1.36.5 // indirect
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
@ -0,0 +1,151 @@
|
|||
github.com/Caknoooo/go-pagination v0.1.0 h1:DoSs9IaNmzOMb7I8zZddZeqyU/6Ss27lrv1G3N8b3KA=
|
||||
github.com/Caknoooo/go-pagination v0.1.0/go.mod h1:JFrym1XOpBuX5ovwsJ885n6onqIVWMZwOmh1W3P2wbk=
|
||||
github.com/bytedance/sonic v1.13.1 h1:Jyd5CIvdFnkOWuKXr+wm4Nyk2h0yAFsr8ucJgEasO3g=
|
||||
github.com/bytedance/sonic v1.13.1/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY=
|
||||
github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
|
||||
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
|
||||
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be h1:J5BL2kskAlV9ckgEsNQXscjIaLiOYiZ75d4e94E6dcQ=
|
||||
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
|
||||
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
||||
github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E=
|
||||
github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0=
|
||||
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8=
|
||||
github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
|
||||
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI=
|
||||
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||
github.com/sagikazarmark/locafero v0.8.0 h1:mXaMVw7IqxNBxfv3LdWt9MDmcWDQ1fagDH918lOdVaQ=
|
||||
github.com/sagikazarmark/locafero v0.8.0/go.mod h1:UBUyz37V+EdMS3hDF3QWIiVr/2dPrx49OMO0Bn0hJqk=
|
||||
github.com/samber/do v1.6.0 h1:Jy/N++BXINDB6lAx5wBlbpHlUdl0FKpLWgGEV9YWqaU=
|
||||
github.com/samber/do v1.6.0/go.mod h1:DWqBvumy8dyb2vEnYZE7D7zaVEB64J45B0NjTlY/M4k=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA=
|
||||
github.com/spf13/afero v1.14.0/go.mod h1:acJQ8t0ohCGuMN3O+Pv0V0hgMxNYDlvdk+VTfyZmbYo=
|
||||
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
|
||||
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.20.0 h1:zrxIyR3RQIOsarIrgL8+sAvALXul9jeEPa06Y0Ph6vY=
|
||||
github.com/spf13/viper v1.20.0/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
golang.org/x/arch v0.15.0 h1:QtOrQd0bTUnhNVNndMpLHNWrDmYzZ2KDqSrEymqInZw=
|
||||
golang.org/x/arch v0.15.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE=
|
||||
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
|
||||
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
|
||||
golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c=
|
||||
golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
||||
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
|
||||
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
|
||||
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
|
||||
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
|
||||
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
|
||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
|
||||
gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
|
||||
gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I=
|
||||
gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
|
||||
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
|
||||
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
|
|
@ -0,0 +1,200 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Logs</title>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<script src="https://kit.fontawesome.com/a82697a287.js" crossorigin="anonymous"></script>
|
||||
<style>
|
||||
body {
|
||||
background: linear-gradient(135deg, #f6f8fc 0%, #e9ecef 100%);
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.glass-effect {
|
||||
background: rgba(255, 255, 255, 0.95);
|
||||
backdrop-filter: blur(10px);
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.log-item {
|
||||
position: relative;
|
||||
max-height: 10rem;
|
||||
overflow: hidden;
|
||||
transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
|
||||
border: 1px solid rgba(226, 232, 240, 0.7);
|
||||
}
|
||||
|
||||
.log-text {
|
||||
display: block;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
color: #4a5568;
|
||||
}
|
||||
|
||||
.log-item:hover {
|
||||
max-height: none;
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.log-item:hover .log-text {
|
||||
white-space: normal;
|
||||
}
|
||||
|
||||
.custom-select {
|
||||
background: rgba(255, 255, 255, 0.9);
|
||||
color: #4f46e5;
|
||||
border: 2px solid rgba(79, 70, 229, 0.2);
|
||||
font-weight: 600;
|
||||
padding-right: 2.5rem;
|
||||
appearance: none;
|
||||
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%234f46e5'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
|
||||
background-repeat: no-repeat;
|
||||
background-position: right 0.75rem center;
|
||||
background-size: 1.25rem;
|
||||
}
|
||||
|
||||
.custom-select:hover {
|
||||
border-color: #4f46e5;
|
||||
background-color: rgba(79, 70, 229, 0.05);
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
.custom-select:focus {
|
||||
outline: none;
|
||||
border-color: #4f46e5;
|
||||
box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.1);
|
||||
}
|
||||
|
||||
.refresh-button {
|
||||
background: rgba(255, 255, 255, 0.9);
|
||||
color: #4f46e5;
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
border-radius: 16px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
transition: all 0.4s ease;
|
||||
border: 2px solid rgba(79, 70, 229, 0.2);
|
||||
font-size: 1.25rem;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.refresh-button::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: linear-gradient(45deg,
|
||||
rgba(79, 70, 229, 0.1) 0%,
|
||||
rgba(79, 70, 229, 0) 100%);
|
||||
opacity: 0;
|
||||
transition: opacity 0.3s ease;
|
||||
}
|
||||
|
||||
.refresh-button:hover {
|
||||
border-color: #4f46e5;
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 8px 16px rgba(79, 70, 229, 0.15);
|
||||
}
|
||||
|
||||
.refresh-button:hover::before {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.refresh-button:hover i {
|
||||
transform: rotate(180deg);
|
||||
}
|
||||
|
||||
.refresh-button i {
|
||||
transition: transform 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
|
||||
}
|
||||
|
||||
.controls-wrapper {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
align-items: center;
|
||||
background: rgba(255, 255, 255, 0.5);
|
||||
padding: 0.5rem;
|
||||
border-radius: 20px;
|
||||
backdrop-filter: blur(8px);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body class="font-sans">
|
||||
<div class="max-w-5xl mx-auto p-8 glass-effect rounded-3xl shadow-2xl mt-12 mb-12">
|
||||
<div class="flex items-center justify-between mb-12">
|
||||
<div>
|
||||
<h1 class="text-4xl font-bold bg-gradient-to-r from-indigo-600 to-purple-600 bg-clip-text text-transparent">Query Logs</h1>
|
||||
<p class="text-gray-500 mt-2">Monitor and track system queries</p>
|
||||
</div>
|
||||
|
||||
<div class="controls-wrapper">
|
||||
<button onclick="location.reload()" class="refresh-button" title="Refresh logs">
|
||||
<i class="fa-solid fa-arrows-rotate"></i>
|
||||
</button>
|
||||
|
||||
<div class="relative">
|
||||
<select id="month" onchange="changeMonth()" class="custom-select w-48 px-6 py-3 rounded-xl text-sm font-medium">
|
||||
<option value="january" {{ if eq .Month "january" }}selected{{ end }}>January</option>
|
||||
<option value="february" {{ if eq .Month "february" }}selected{{ end }}>February</option>
|
||||
<option value="march" {{ if eq .Month "march" }}selected{{ end }}>March</option>
|
||||
<option value="april" {{ if eq .Month "april" }}selected{{ end }}>April</option>
|
||||
<option value="may" {{ if eq .Month "may" }}selected{{ end }}>May</option>
|
||||
<option value="june" {{ if eq .Month "june" }}selected{{ end }}>June</option>
|
||||
<option value="july" {{ if eq .Month "july" }}selected{{ end }}>July</option>
|
||||
<option value="august" {{ if eq .Month "august" }}selected{{ end }}>August</option>
|
||||
<option value="september" {{ if eq .Month "september" }}selected{{ end }}>September</option>
|
||||
<option value="october" {{ if eq .Month "october" }}selected{{ end }}>October</option>
|
||||
<option value="november" {{ if eq .Month "november" }}selected{{ end }}>November</option>
|
||||
<option value="december" {{ if eq .Month "december" }}selected{{ end }}>December</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<ul class="space-y-6">
|
||||
{{ range .Logs }}
|
||||
<li class="log-item p-6 glass-effect rounded-2xl hover:bg-gradient-to-r hover:from-indigo-50 hover:to-purple-50 transition-all">
|
||||
<div class="flex items-center gap-2 text-indigo-600 mb-3">
|
||||
<i class="fa-solid fa-terminal text-sm"></i>
|
||||
<span class="font-semibold">Log Entry</span>
|
||||
</div>
|
||||
<pre class="text-sm whitespace-pre-wrap break-words log-text">{{ . }}</pre>
|
||||
</li>
|
||||
{{ else }}
|
||||
<li class="p-8 glass-effect rounded-2xl text-center">
|
||||
<i class="fa-solid fa-inbox text-4xl text-gray-300 mb-3"></i>
|
||||
<p class="text-gray-500">No logs found for this month.</p>
|
||||
</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
const monthSelect = document.getElementById("month");
|
||||
if (!monthSelect.value) {
|
||||
const currentMonthIndex = new Date().getMonth();
|
||||
const months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
|
||||
monthSelect.value = months[currentMonthIndex];
|
||||
}
|
||||
});
|
||||
|
||||
function changeMonth() {
|
||||
const selectedMonth = document.getElementById("month").value;
|
||||
window.location.href = `/logs/${selectedMonth}`;
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
package middlewares
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/dto"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Authenticate(jwtService service.JWTService) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
authHeader := ctx.GetHeader("Authorization")
|
||||
|
||||
if authHeader == "" {
|
||||
response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, dto.MESSAGE_FAILED_TOKEN_NOT_FOUND, nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, response)
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.Contains(authHeader, "Bearer ") {
|
||||
response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, dto.MESSAGE_FAILED_TOKEN_NOT_VALID, nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, response)
|
||||
return
|
||||
}
|
||||
|
||||
authHeader = strings.Replace(authHeader, "Bearer ", "", -1)
|
||||
token, err := jwtService.ValidateToken(authHeader)
|
||||
if err != nil {
|
||||
response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, dto.MESSAGE_FAILED_TOKEN_NOT_VALID, nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, response)
|
||||
return
|
||||
}
|
||||
|
||||
if !token.Valid {
|
||||
response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, dto.MESSAGE_FAILED_DENIED_ACCESS, nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, response)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := jwtService.GetUserIDByToken(authHeader)
|
||||
if err != nil {
|
||||
response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, err.Error(), nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, response)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Set("token", authHeader)
|
||||
ctx.Set("user_id", userId)
|
||||
ctx.Next()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package middlewares
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func CORSMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
c.Header("Access-Control-Allow-Credentials", "true")
|
||||
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
|
||||
c.Header("Access-Control-Allow-Methods", "POST, HEAD, PATCH, OPTIONS, GET, PUT, DELETE")
|
||||
|
||||
if c.Request.Method == http.MethodOptions {
|
||||
c.AbortWithStatus(204)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package dto
|
||||
|
||||
const (
|
||||
MESSAGE_SUCCESS_REFRESH_TOKEN = "Successfully refreshed token"
|
||||
MESSAGE_FAILED_REFRESH_TOKEN = "Failed to refresh token"
|
||||
MESSAGE_FAILED_INVALID_REFRESH_TOKEN = "Invalid refresh token"
|
||||
MESSAGE_FAILED_EXPIRED_REFRESH_TOKEN = "Refresh token has expired"
|
||||
)
|
||||
|
||||
type TokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
type RefreshTokenRequest struct {
|
||||
RefreshToken string `json:"refresh_token" binding:"required"`
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type RefreshTokenRepository interface {
|
||||
Create(ctx context.Context, tx *gorm.DB, token entities.RefreshToken) (entities.RefreshToken, error)
|
||||
FindByToken(ctx context.Context, tx *gorm.DB, token string) (entities.RefreshToken, error)
|
||||
DeleteByUserID(ctx context.Context, tx *gorm.DB, userID string) error
|
||||
DeleteByToken(ctx context.Context, tx *gorm.DB, token string) error
|
||||
DeleteExpired(ctx context.Context, tx *gorm.DB) error
|
||||
}
|
||||
|
||||
type refreshTokenRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRefreshTokenRepository(db *gorm.DB) RefreshTokenRepository {
|
||||
return &refreshTokenRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *refreshTokenRepository) Create(
|
||||
ctx context.Context,
|
||||
tx *gorm.DB,
|
||||
token entities.RefreshToken,
|
||||
) (entities.RefreshToken, error) {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
|
||||
if err := tx.WithContext(ctx).Create(&token).Error; err != nil {
|
||||
return entities.RefreshToken{}, err
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (r *refreshTokenRepository) FindByToken(ctx context.Context, tx *gorm.DB, token string) (
|
||||
entities.RefreshToken,
|
||||
error,
|
||||
) {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
|
||||
var refreshToken entities.RefreshToken
|
||||
if err := tx.WithContext(ctx).Where("token = ?", token).Preload("User").Take(&refreshToken).Error; err != nil {
|
||||
return entities.RefreshToken{}, err
|
||||
}
|
||||
|
||||
return refreshToken, nil
|
||||
}
|
||||
|
||||
func (r *refreshTokenRepository) DeleteByUserID(ctx context.Context, tx *gorm.DB, userID string) error {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
|
||||
if err := tx.WithContext(ctx).Where("user_id = ?", userID).Delete(&entities.RefreshToken{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *refreshTokenRepository) DeleteByToken(ctx context.Context, tx *gorm.DB, token string) error {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
|
||||
if err := tx.WithContext(ctx).Where("token = ?", token).Delete(&entities.RefreshToken{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *refreshTokenRepository) DeleteExpired(ctx context.Context, tx *gorm.DB) error {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
|
||||
if err := tx.WithContext(ctx).Where("expires_at < ?", time.Now()).Delete(&entities.RefreshToken{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/samber/do"
|
||||
)
|
||||
|
||||
func RegisterRoutes(server *gin.Engine, injector *do.Injector) {
|
||||
// Auth routes akan ditambahkan nanti ketika auth controller sudah dibuat
|
||||
authRoutes := server.Group("/api/v1/auth")
|
||||
{
|
||||
// authRoutes.POST("/refresh-token", authController.RefreshToken)
|
||||
_ = authRoutes // untuk menghindari unused variable
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,104 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
type JWTService interface {
|
||||
GenerateAccessToken(userId string, role string) string
|
||||
GenerateRefreshToken() (string, time.Time)
|
||||
ValidateToken(token string) (*jwt.Token, error)
|
||||
GetUserIDByToken(token string) (string, error)
|
||||
}
|
||||
|
||||
type jwtCustomClaim struct {
|
||||
UserID string `json:"user_id"`
|
||||
Role string `json:"role"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
type jwtService struct {
|
||||
secretKey string
|
||||
issuer string
|
||||
accessExpiry time.Duration
|
||||
refreshExpiry time.Duration
|
||||
}
|
||||
|
||||
func NewJWTService() JWTService {
|
||||
return &jwtService{
|
||||
secretKey: getSecretKey(),
|
||||
issuer: "Template",
|
||||
accessExpiry: time.Minute * 15,
|
||||
refreshExpiry: time.Hour * 24 * 7,
|
||||
}
|
||||
}
|
||||
|
||||
func getSecretKey() string {
|
||||
secretKey := os.Getenv("JWT_SECRET")
|
||||
if secretKey == "" {
|
||||
secretKey = "Template"
|
||||
}
|
||||
return secretKey
|
||||
}
|
||||
|
||||
func (j *jwtService) GenerateAccessToken(userId string, role string) string {
|
||||
claims := jwtCustomClaim{
|
||||
userId,
|
||||
role,
|
||||
jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(j.accessExpiry)),
|
||||
Issuer: j.issuer,
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
},
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
tx, err := token.SignedString([]byte(j.secretKey))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
return tx
|
||||
}
|
||||
|
||||
func (j *jwtService) GenerateRefreshToken() (string, time.Time) {
|
||||
b := make([]byte, 32)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return "", time.Time{}
|
||||
}
|
||||
|
||||
refreshToken := base64.StdEncoding.EncodeToString(b)
|
||||
expiresAt := time.Now().Add(j.refreshExpiry)
|
||||
|
||||
return refreshToken, expiresAt
|
||||
}
|
||||
|
||||
func (j *jwtService) parseToken(t_ *jwt.Token) (any, error) {
|
||||
if _, ok := t_.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method %v", t_.Header["alg"])
|
||||
}
|
||||
return []byte(j.secretKey), nil
|
||||
}
|
||||
|
||||
func (j *jwtService) ValidateToken(token string) (*jwt.Token, error) {
|
||||
return jwt.Parse(token, j.parseToken)
|
||||
}
|
||||
|
||||
func (j *jwtService) GetUserIDByToken(token string) (string, error) {
|
||||
tToken, err := j.ValidateToken(token)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
claims := tToken.Claims.(jwt.MapClaims)
|
||||
id := fmt.Sprintf("%v", claims["user_id"])
|
||||
return id, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,203 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
authDto "github.com/Caknoooo/go-gin-clean-starter/modules/auth/dto"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/dto"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/query"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/service"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
||||
"github.com/Caknoooo/go-pagination"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/samber/do"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type (
|
||||
UserController interface {
|
||||
Register(ctx *gin.Context)
|
||||
Login(ctx *gin.Context)
|
||||
Me(ctx *gin.Context)
|
||||
Refresh(ctx *gin.Context)
|
||||
GetAllUser(ctx *gin.Context)
|
||||
SendVerificationEmail(ctx *gin.Context)
|
||||
VerifyEmail(ctx *gin.Context)
|
||||
Update(ctx *gin.Context)
|
||||
Delete(ctx *gin.Context)
|
||||
}
|
||||
|
||||
userController struct {
|
||||
userService service.UserService
|
||||
db *gorm.DB
|
||||
}
|
||||
)
|
||||
|
||||
func NewUserController(injector *do.Injector, us service.UserService) UserController {
|
||||
db := do.MustInvokeNamed[*gorm.DB](injector, constants.DB)
|
||||
return &userController{
|
||||
userService: us,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *userController) Register(ctx *gin.Context) {
|
||||
var user dto.UserCreateRequest
|
||||
if err := ctx.ShouldBind(&user); err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.userService.Register(ctx.Request.Context(), user)
|
||||
if err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_REGISTER_USER, err.Error(), nil)
|
||||
ctx.JSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_REGISTER_USER, result)
|
||||
ctx.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
||||
func (c *userController) GetAllUser(ctx *gin.Context) {
|
||||
var filter = &query.UserFilter{}
|
||||
filter.BindPagination(ctx)
|
||||
|
||||
ctx.ShouldBindQuery(filter)
|
||||
|
||||
users, total, err := pagination.PaginatedQueryWithIncludable[query.User](c.db, filter)
|
||||
if err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_USER, err.Error(), nil)
|
||||
ctx.JSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
paginationResponse := pagination.CalculatePagination(filter.Pagination, total)
|
||||
response := pagination.NewPaginatedResponse(http.StatusOK, dto.MESSAGE_SUCCESS_GET_LIST_USER, users, paginationResponse)
|
||||
ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (c *userController) Me(ctx *gin.Context) {
|
||||
userId := ctx.MustGet("user_id").(string)
|
||||
|
||||
result, err := c.userService.GetUserById(ctx.Request.Context(), userId)
|
||||
if err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_USER, err.Error(), nil)
|
||||
ctx.JSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_USER, result)
|
||||
ctx.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
||||
func (c *userController) Login(ctx *gin.Context) {
|
||||
var req dto.UserLoginRequest
|
||||
if err := ctx.ShouldBind(&req); err != nil {
|
||||
response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, response)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.userService.Verify(ctx.Request.Context(), req)
|
||||
if err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_LOGIN, err.Error(), nil)
|
||||
ctx.JSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_LOGIN, result)
|
||||
ctx.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
||||
func (c *userController) SendVerificationEmail(ctx *gin.Context) {
|
||||
var req dto.SendVerificationEmailRequest
|
||||
if err := ctx.ShouldBind(&req); err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
err := c.userService.SendVerificationEmail(ctx.Request.Context(), req)
|
||||
if err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, err.Error(), nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
res := utils.BuildResponseSuccess(dto.MESSAGE_SEND_VERIFICATION_EMAIL_SUCCESS, nil)
|
||||
ctx.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
||||
func (c *userController) VerifyEmail(ctx *gin.Context) {
|
||||
var req dto.VerifyEmailRequest
|
||||
if err := ctx.ShouldBind(&req); err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.userService.VerifyEmail(ctx.Request.Context(), req)
|
||||
if err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_VERIFY_EMAIL, err.Error(), nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_VERIFY_EMAIL, result)
|
||||
ctx.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
||||
func (c *userController) Update(ctx *gin.Context) {
|
||||
var req dto.UserUpdateRequest
|
||||
if err := ctx.ShouldBind(&req); err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
userId := ctx.MustGet("user_id").(string)
|
||||
result, err := c.userService.Update(ctx.Request.Context(), req, userId)
|
||||
if err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_USER, err.Error(), nil)
|
||||
ctx.JSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_USER, result)
|
||||
ctx.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
||||
func (c *userController) Delete(ctx *gin.Context) {
|
||||
userId := ctx.MustGet("user_id").(string)
|
||||
|
||||
if err := c.userService.Delete(ctx.Request.Context(), userId); err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_USER, err.Error(), nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_USER, nil)
|
||||
ctx.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
||||
func (c *userController) Refresh(ctx *gin.Context) {
|
||||
var req authDto.RefreshTokenRequest
|
||||
if err := ctx.ShouldBind(&req); err != nil {
|
||||
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := c.userService.RefreshToken(ctx.Request.Context(), req)
|
||||
if err != nil {
|
||||
res := utils.BuildResponseFailed(authDto.MESSAGE_FAILED_REFRESH_TOKEN, err.Error(), nil)
|
||||
ctx.JSON(http.StatusUnauthorized, res)
|
||||
return
|
||||
}
|
||||
|
||||
res := utils.BuildResponseSuccess(authDto.MESSAGE_SUCCESS_REFRESH_TOKEN, result)
|
||||
ctx.JSON(http.StatusOK, res)
|
||||
}
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
package dto
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"mime/multipart"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
||||
)
|
||||
|
||||
const (
|
||||
// Failed
|
||||
MESSAGE_FAILED_GET_DATA_FROM_BODY = "failed get data from body"
|
||||
MESSAGE_FAILED_REGISTER_USER = "failed create user"
|
||||
MESSAGE_FAILED_GET_LIST_USER = "failed get list user"
|
||||
MESSAGE_FAILED_TOKEN_NOT_VALID = "token not valid"
|
||||
MESSAGE_FAILED_TOKEN_NOT_FOUND = "token not found"
|
||||
MESSAGE_FAILED_GET_USER = "failed get user"
|
||||
MESSAGE_FAILED_LOGIN = "failed login"
|
||||
MESSAGE_FAILED_UPDATE_USER = "failed update user"
|
||||
MESSAGE_FAILED_DELETE_USER = "failed delete user"
|
||||
MESSAGE_FAILED_PROSES_REQUEST = "failed proses request"
|
||||
MESSAGE_FAILED_DENIED_ACCESS = "denied access"
|
||||
MESSAGE_FAILED_VERIFY_EMAIL = "failed verify email"
|
||||
|
||||
// Success
|
||||
MESSAGE_SUCCESS_REGISTER_USER = "success create user"
|
||||
MESSAGE_SUCCESS_GET_LIST_USER = "success get list user"
|
||||
MESSAGE_SUCCESS_GET_USER = "success get user"
|
||||
MESSAGE_SUCCESS_LOGIN = "success login"
|
||||
MESSAGE_SUCCESS_UPDATE_USER = "success update user"
|
||||
MESSAGE_SUCCESS_DELETE_USER = "success delete user"
|
||||
MESSAGE_SEND_VERIFICATION_EMAIL_SUCCESS = "success send verification email"
|
||||
MESSAGE_SUCCESS_VERIFY_EMAIL = "success verify email"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCreateUser = errors.New("failed to create user")
|
||||
ErrGetUserById = errors.New("failed to get user by id")
|
||||
ErrGetUserByEmail = errors.New("failed to get user by email")
|
||||
ErrEmailAlreadyExists = errors.New("email already exist")
|
||||
ErrUpdateUser = errors.New("failed to update user")
|
||||
ErrUserNotFound = errors.New("user not found")
|
||||
ErrEmailNotFound = errors.New("email not found")
|
||||
ErrDeleteUser = errors.New("failed to delete user")
|
||||
ErrTokenInvalid = errors.New("token invalid")
|
||||
ErrTokenExpired = errors.New("token expired")
|
||||
ErrAccountAlreadyVerified = errors.New("account already verified")
|
||||
)
|
||||
|
||||
type (
|
||||
UserCreateRequest struct {
|
||||
Name string `json:"name" form:"name" binding:"required,min=2,max=100"`
|
||||
TelpNumber string `json:"telp_number" form:"telp_number" binding:"omitempty,min=8,max=20"`
|
||||
Email string `json:"email" form:"email" binding:"required,email"`
|
||||
Password string `json:"password" form:"password" binding:"required,min=8"`
|
||||
Image *multipart.FileHeader `json:"image" form:"image"`
|
||||
}
|
||||
|
||||
UserResponse struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
TelpNumber string `json:"telp_number"`
|
||||
Role string `json:"role"`
|
||||
ImageUrl string `json:"image_url"`
|
||||
IsVerified bool `json:"is_verified"`
|
||||
}
|
||||
|
||||
UserPaginationResponse struct {
|
||||
Data []UserResponse `json:"data"`
|
||||
dto.PaginationResponse
|
||||
}
|
||||
|
||||
GetAllUserRepositoryResponse struct {
|
||||
Users []entities.User `json:"users"`
|
||||
dto.PaginationResponse
|
||||
}
|
||||
|
||||
UserUpdateRequest struct {
|
||||
Name string `json:"name" form:"name" binding:"omitempty,min=2,max=100"`
|
||||
TelpNumber string `json:"telp_number" form:"telp_number" binding:"omitempty,min=8,max=20"`
|
||||
Email string `json:"email" form:"email" binding:"omitempty,email"`
|
||||
}
|
||||
|
||||
UserUpdateResponse struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
TelpNumber string `json:"telp_number"`
|
||||
Role string `json:"role"`
|
||||
Email string `json:"email"`
|
||||
IsVerified bool `json:"is_verified"`
|
||||
}
|
||||
|
||||
SendVerificationEmailRequest struct {
|
||||
Email string `json:"email" form:"email" binding:"required"`
|
||||
}
|
||||
|
||||
VerifyEmailRequest struct {
|
||||
Token string `json:"token" form:"token" binding:"required"`
|
||||
}
|
||||
|
||||
VerifyEmailResponse struct {
|
||||
Email string `json:"email"`
|
||||
IsVerified bool `json:"is_verified"`
|
||||
}
|
||||
|
||||
UserLoginRequest struct {
|
||||
Email string `json:"email" form:"email" binding:"required"`
|
||||
Password string `json:"password" form:"password" binding:"required"`
|
||||
}
|
||||
)
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
package query
|
||||
|
||||
import (
|
||||
"github.com/Caknoooo/go-pagination"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
TelpNumber string `json:"telp_number"`
|
||||
Role string `json:"role"`
|
||||
ImageUrl string `json:"image_url"`
|
||||
IsVerified bool `json:"is_verified"`
|
||||
}
|
||||
|
||||
type UserFilter struct {
|
||||
pagination.BaseFilter
|
||||
}
|
||||
|
||||
func (f *UserFilter) ApplyFilters(query *gorm.DB) *gorm.DB {
|
||||
// Apply your filters here
|
||||
return query
|
||||
}
|
||||
|
||||
func (f *UserFilter) GetTableName() string {
|
||||
return "users"
|
||||
}
|
||||
|
||||
func (f *UserFilter) GetSearchFields() []string {
|
||||
return []string{"name"}
|
||||
}
|
||||
|
||||
func (f *UserFilter) GetDefaultSort() string {
|
||||
return "id asc"
|
||||
}
|
||||
|
||||
func (f *UserFilter) GetIncludes() []string {
|
||||
return f.Includes
|
||||
}
|
||||
|
||||
func (f *UserFilter) GetPagination() pagination.PaginationRequest {
|
||||
return f.Pagination
|
||||
}
|
||||
|
||||
func (f *UserFilter) Validate() {
|
||||
var validIncludes []string
|
||||
allowedIncludes := f.GetAllowedIncludes()
|
||||
for _, include := range f.Includes {
|
||||
if allowedIncludes[include] {
|
||||
validIncludes = append(validIncludes, include)
|
||||
}
|
||||
}
|
||||
f.Includes = validIncludes
|
||||
}
|
||||
|
||||
func (f *UserFilter) GetAllowedIncludes() map[string]bool {
|
||||
return map[string]bool{}
|
||||
}
|
||||
|
|
@ -0,0 +1,104 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type (
|
||||
UserRepository interface {
|
||||
Register(ctx context.Context, tx *gorm.DB, user entities.User) (entities.User, error)
|
||||
GetUserById(ctx context.Context, tx *gorm.DB, userId string) (entities.User, error)
|
||||
GetUserByEmail(ctx context.Context, tx *gorm.DB, email string) (entities.User, error)
|
||||
CheckEmail(ctx context.Context, tx *gorm.DB, email string) (entities.User, bool, error)
|
||||
Update(ctx context.Context, tx *gorm.DB, user entities.User) (entities.User, error)
|
||||
Delete(ctx context.Context, tx *gorm.DB, userId string) error
|
||||
}
|
||||
|
||||
userRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
)
|
||||
|
||||
func NewUserRepository(db *gorm.DB) UserRepository {
|
||||
return &userRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *userRepository) Register(ctx context.Context, tx *gorm.DB, user entities.User) (entities.User, error) {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
|
||||
if err := tx.WithContext(ctx).Create(&user).Error; err != nil {
|
||||
return entities.User{}, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) GetUserById(ctx context.Context, tx *gorm.DB, userId string) (entities.User, error) {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
|
||||
var user entities.User
|
||||
if err := tx.WithContext(ctx).Where("id = ?", userId).Take(&user).Error; err != nil {
|
||||
return entities.User{}, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) GetUserByEmail(ctx context.Context, tx *gorm.DB, email string) (entities.User, error) {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
|
||||
var user entities.User
|
||||
if err := tx.WithContext(ctx).Where("email = ?", email).Take(&user).Error; err != nil {
|
||||
return entities.User{}, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) CheckEmail(ctx context.Context, tx *gorm.DB, email string) (entities.User, bool, error) {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
|
||||
var user entities.User
|
||||
if err := tx.WithContext(ctx).Where("email = ?", email).Take(&user).Error; err != nil {
|
||||
return entities.User{}, false, err
|
||||
}
|
||||
|
||||
return user, true, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) Update(ctx context.Context, tx *gorm.DB, user entities.User) (entities.User, error) {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
|
||||
if err := tx.WithContext(ctx).Updates(&user).Error; err != nil {
|
||||
return entities.User{}, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) Delete(ctx context.Context, tx *gorm.DB, userId string) error {
|
||||
if tx == nil {
|
||||
tx = r.db
|
||||
}
|
||||
|
||||
if err := tx.WithContext(ctx).Delete(&entities.User{}, "id = ?", userId).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"github.com/Caknoooo/go-gin-clean-starter/middlewares"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/controller"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/samber/do"
|
||||
)
|
||||
|
||||
func RegisterRoutes(server *gin.Engine, injector *do.Injector) {
|
||||
userController := do.MustInvoke[controller.UserController](injector)
|
||||
jwtService := do.MustInvokeNamed[service.JWTService](injector, constants.JWTService)
|
||||
|
||||
userRoutes := server.Group("/api/user")
|
||||
{
|
||||
userRoutes.POST("", userController.Register)
|
||||
userRoutes.POST("/login", userController.Login)
|
||||
userRoutes.GET("", userController.GetAllUser)
|
||||
userRoutes.GET("/me", middlewares.Authenticate(jwtService), userController.Me)
|
||||
userRoutes.PUT("/:id", middlewares.Authenticate(jwtService), userController.Update)
|
||||
userRoutes.DELETE("/:id", middlewares.Authenticate(jwtService), userController.Delete)
|
||||
userRoutes.POST("/send-verification-email", userController.SendVerificationEmail)
|
||||
userRoutes.POST("/verify-email", userController.VerifyEmail)
|
||||
userRoutes.POST("/refresh", middlewares.Authenticate(jwtService), userController.Refresh)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,248 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
authDto "github.com/Caknoooo/go-gin-clean-starter/modules/auth/dto"
|
||||
authRepo "github.com/Caknoooo/go-gin-clean-starter/modules/auth/repository"
|
||||
authService "github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/dto"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/repository"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/helpers"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserService interface {
|
||||
Register(ctx context.Context, req dto.UserCreateRequest) (dto.UserResponse, error)
|
||||
GetUserById(ctx context.Context, userId string) (dto.UserResponse, error)
|
||||
Verify(ctx context.Context, req dto.UserLoginRequest) (authDto.TokenResponse, error)
|
||||
SendVerificationEmail(ctx context.Context, req dto.SendVerificationEmailRequest) error
|
||||
VerifyEmail(ctx context.Context, req dto.VerifyEmailRequest) (dto.VerifyEmailResponse, error)
|
||||
Update(ctx context.Context, req dto.UserUpdateRequest, userId string) (dto.UserUpdateResponse, error)
|
||||
Delete(ctx context.Context, userId string) error
|
||||
RefreshToken(ctx context.Context, req authDto.RefreshTokenRequest) (authDto.TokenResponse, error)
|
||||
}
|
||||
|
||||
type userService struct {
|
||||
userRepository repository.UserRepository
|
||||
refreshTokenRepository authRepo.RefreshTokenRepository
|
||||
jwtService authService.JWTService
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewUserService(
|
||||
userRepo repository.UserRepository,
|
||||
refreshTokenRepo authRepo.RefreshTokenRepository,
|
||||
jwtService authService.JWTService,
|
||||
db *gorm.DB,
|
||||
) UserService {
|
||||
return &userService{
|
||||
userRepository: userRepo,
|
||||
refreshTokenRepository: refreshTokenRepo,
|
||||
jwtService: jwtService,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *userService) Register(ctx context.Context, req dto.UserCreateRequest) (dto.UserResponse, error) {
|
||||
_, exists, err := s.userRepository.CheckEmail(ctx, s.db, req.Email)
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
return dto.UserResponse{}, err
|
||||
}
|
||||
if exists {
|
||||
return dto.UserResponse{}, dto.ErrEmailAlreadyExists
|
||||
}
|
||||
|
||||
user := entities.User{
|
||||
ID: uuid.New(),
|
||||
Name: req.Name,
|
||||
Email: req.Email,
|
||||
TelpNumber: req.TelpNumber,
|
||||
Password: req.Password,
|
||||
Role: constants.ENUM_ROLE_USER,
|
||||
IsVerified: false,
|
||||
}
|
||||
|
||||
createdUser, err := s.userRepository.Register(ctx, s.db, user)
|
||||
if err != nil {
|
||||
return dto.UserResponse{}, err
|
||||
}
|
||||
|
||||
return dto.UserResponse{
|
||||
ID: createdUser.ID.String(),
|
||||
Name: createdUser.Name,
|
||||
Email: createdUser.Email,
|
||||
TelpNumber: createdUser.TelpNumber,
|
||||
Role: createdUser.Role,
|
||||
ImageUrl: createdUser.ImageUrl,
|
||||
IsVerified: createdUser.IsVerified,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *userService) GetUserById(ctx context.Context, userId string) (dto.UserResponse, error) {
|
||||
user, err := s.userRepository.GetUserById(ctx, s.db, userId)
|
||||
if err != nil {
|
||||
return dto.UserResponse{}, err
|
||||
}
|
||||
|
||||
return dto.UserResponse{
|
||||
ID: user.ID.String(),
|
||||
Name: user.Name,
|
||||
Email: user.Email,
|
||||
TelpNumber: user.TelpNumber,
|
||||
Role: user.Role,
|
||||
ImageUrl: user.ImageUrl,
|
||||
IsVerified: user.IsVerified,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *userService) Verify(ctx context.Context, req dto.UserLoginRequest) (authDto.TokenResponse, error) {
|
||||
user, err := s.userRepository.GetUserByEmail(ctx, s.db, req.Email)
|
||||
if err != nil {
|
||||
return authDto.TokenResponse{}, dto.ErrEmailNotFound
|
||||
}
|
||||
|
||||
isValid, err := helpers.CheckPassword(user.Password, []byte(req.Password))
|
||||
if err != nil || !isValid {
|
||||
return authDto.TokenResponse{}, dto.ErrUserNotFound
|
||||
}
|
||||
|
||||
accessToken := s.jwtService.GenerateAccessToken(user.ID.String(), user.Role)
|
||||
refreshTokenString, expiresAt := s.jwtService.GenerateRefreshToken()
|
||||
|
||||
refreshToken := entities.RefreshToken{
|
||||
ID: uuid.New(),
|
||||
UserID: user.ID,
|
||||
Token: refreshTokenString,
|
||||
ExpiresAt: expiresAt,
|
||||
}
|
||||
|
||||
_, err = s.refreshTokenRepository.Create(ctx, s.db, refreshToken)
|
||||
if err != nil {
|
||||
return authDto.TokenResponse{}, err
|
||||
}
|
||||
|
||||
return authDto.TokenResponse{
|
||||
AccessToken: accessToken,
|
||||
RefreshToken: refreshTokenString,
|
||||
Role: user.Role,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *userService) SendVerificationEmail(ctx context.Context, req dto.SendVerificationEmailRequest) error {
|
||||
user, err := s.userRepository.GetUserByEmail(ctx, s.db, req.Email)
|
||||
if err != nil {
|
||||
return dto.ErrEmailNotFound
|
||||
}
|
||||
|
||||
if user.IsVerified {
|
||||
return dto.ErrAccountAlreadyVerified
|
||||
}
|
||||
|
||||
verificationToken := s.jwtService.GenerateAccessToken(user.ID.String(), "verification")
|
||||
|
||||
subject := "Email Verification"
|
||||
body := "Please verify your email using this token: " + verificationToken
|
||||
|
||||
return utils.SendMail(user.Email, subject, body)
|
||||
}
|
||||
|
||||
func (s *userService) VerifyEmail(ctx context.Context, req dto.VerifyEmailRequest) (dto.VerifyEmailResponse, error) {
|
||||
token, err := s.jwtService.ValidateToken(req.Token)
|
||||
if err != nil || !token.Valid {
|
||||
return dto.VerifyEmailResponse{}, dto.ErrTokenInvalid
|
||||
}
|
||||
|
||||
userId, err := s.jwtService.GetUserIDByToken(req.Token)
|
||||
if err != nil {
|
||||
return dto.VerifyEmailResponse{}, dto.ErrTokenInvalid
|
||||
}
|
||||
|
||||
user, err := s.userRepository.GetUserById(ctx, s.db, userId)
|
||||
if err != nil {
|
||||
return dto.VerifyEmailResponse{}, dto.ErrUserNotFound
|
||||
}
|
||||
|
||||
user.IsVerified = true
|
||||
updatedUser, err := s.userRepository.Update(ctx, s.db, user)
|
||||
if err != nil {
|
||||
return dto.VerifyEmailResponse{}, err
|
||||
}
|
||||
|
||||
return dto.VerifyEmailResponse{
|
||||
Email: updatedUser.Email,
|
||||
IsVerified: updatedUser.IsVerified,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *userService) Update(ctx context.Context, req dto.UserUpdateRequest, userId string) (dto.UserUpdateResponse, error) {
|
||||
user, err := s.userRepository.GetUserById(ctx, s.db, userId)
|
||||
if err != nil {
|
||||
return dto.UserUpdateResponse{}, dto.ErrUserNotFound
|
||||
}
|
||||
|
||||
if req.Name != "" {
|
||||
user.Name = req.Name
|
||||
}
|
||||
if req.Email != "" {
|
||||
user.Email = req.Email
|
||||
}
|
||||
if req.TelpNumber != "" {
|
||||
user.TelpNumber = req.TelpNumber
|
||||
}
|
||||
|
||||
updatedUser, err := s.userRepository.Update(ctx, s.db, user)
|
||||
if err != nil {
|
||||
return dto.UserUpdateResponse{}, err
|
||||
}
|
||||
|
||||
return dto.UserUpdateResponse{
|
||||
ID: updatedUser.ID.String(),
|
||||
Name: updatedUser.Name,
|
||||
TelpNumber: updatedUser.TelpNumber,
|
||||
Role: updatedUser.Role,
|
||||
Email: updatedUser.Email,
|
||||
IsVerified: updatedUser.IsVerified,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *userService) Delete(ctx context.Context, userId string) error {
|
||||
return s.userRepository.Delete(ctx, s.db, userId)
|
||||
}
|
||||
|
||||
func (s *userService) RefreshToken(ctx context.Context, req authDto.RefreshTokenRequest) (authDto.TokenResponse, error) {
|
||||
refreshToken, err := s.refreshTokenRepository.FindByToken(ctx, s.db, req.RefreshToken)
|
||||
if err != nil {
|
||||
return authDto.TokenResponse{}, err
|
||||
}
|
||||
|
||||
accessToken := s.jwtService.GenerateAccessToken(refreshToken.UserID.String(), refreshToken.User.Role)
|
||||
newRefreshTokenString, expiresAt := s.jwtService.GenerateRefreshToken()
|
||||
|
||||
err = s.refreshTokenRepository.DeleteByToken(ctx, s.db, req.RefreshToken)
|
||||
if err != nil {
|
||||
return authDto.TokenResponse{}, err
|
||||
}
|
||||
|
||||
newRefreshToken := entities.RefreshToken{
|
||||
ID: uuid.New(),
|
||||
UserID: refreshToken.UserID,
|
||||
Token: newRefreshTokenString,
|
||||
ExpiresAt: expiresAt,
|
||||
}
|
||||
|
||||
_, err = s.refreshTokenRepository.Create(ctx, s.db, newRefreshToken)
|
||||
if err != nil {
|
||||
return authDto.TokenResponse{}, err
|
||||
}
|
||||
|
||||
return authDto.TokenResponse{
|
||||
AccessToken: accessToken,
|
||||
RefreshToken: newRefreshTokenString,
|
||||
Role: refreshToken.User.Role,
|
||||
}, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package constants
|
||||
|
||||
const (
|
||||
ENUM_ROLE_ADMIN = "admin"
|
||||
ENUM_ROLE_USER = "user"
|
||||
|
||||
ENUM_RUN_PRODUCTION = "production"
|
||||
ENUM_RUN_TESTING = "testing"
|
||||
|
||||
ENUM_PAGINATION_PER_PAGE = 10
|
||||
ENUM_PAGINATION_PAGE = 1
|
||||
|
||||
DB = "db"
|
||||
JWTService = "JWTService"
|
||||
)
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
package dto
|
||||
|
||||
type (
|
||||
PaginationRequest struct {
|
||||
Search string `form:"search"`
|
||||
Page int `form:"page"`
|
||||
PerPage int `form:"per_page"`
|
||||
}
|
||||
|
||||
PaginationResponse struct {
|
||||
Page int `json:"page"`
|
||||
PerPage int `json:"per_page"`
|
||||
MaxPage int64 `json:"max_page"`
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
)
|
||||
|
||||
func (p *PaginationRequest) GetOffset() int {
|
||||
return (p.Page - 1) * p.PerPage
|
||||
}
|
||||
|
||||
func (p *PaginationRequest) GetLimit() int {
|
||||
return p.PerPage
|
||||
}
|
||||
|
||||
func (p *PaginationRequest) GetPage() int {
|
||||
return p.Page
|
||||
}
|
||||
|
||||
func (p *PaginationRequest) Default() {
|
||||
if p.Page == 0 {
|
||||
p.Page = 1
|
||||
}
|
||||
|
||||
if p.PerPage == 0 {
|
||||
p.PerPage = 10
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package helpers
|
||||
|
||||
import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 4)
|
||||
return string(bytes), err
|
||||
}
|
||||
|
||||
func CheckPassword(hashPassword string, plainPassword []byte) (bool, error) {
|
||||
hashPW := []byte(hashPassword)
|
||||
if err := bcrypt.CompareHashAndPassword(hashPW, plainPassword); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
const (
|
||||
// todo
|
||||
KEY = "8e71bbce7451ba2835de5aea73e4f3f96821455240823d2fd8174975b8321bfc!"
|
||||
)
|
||||
|
||||
// https://www.melvinvivas.com/how-to-encrypt-and-decrypt-data-using-aes
|
||||
|
||||
func AESEncrypt(stringToEncrypt string) (encryptedString string, err error) {
|
||||
//Since the key is in string, we need to convert decode it to bytes
|
||||
key, err := hex.DecodeString(KEY)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
plaintext := []byte(stringToEncrypt)
|
||||
|
||||
//Create a new Cipher Block from the key
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
//Create a new GCM - https://en.wikipedia.org/wiki/Galois/Counter_Mode
|
||||
//https://golang.org/pkg/crypto/cipher/#NewGCM
|
||||
aesGCM, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
//Create a nonce. Nonce should be from GCM
|
||||
nonce := make([]byte, aesGCM.NonceSize())
|
||||
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
//Encrypt the data using aesGCM.Seal
|
||||
//Since we don't want to save the nonce somewhere else in this case, we add it as a prefix to the encrypted data. The first nonce argument in Seal is the prefix.
|
||||
ciphertext := aesGCM.Seal(nonce, nonce, plaintext, nil)
|
||||
return fmt.Sprintf("%x", ciphertext), nil
|
||||
}
|
||||
|
||||
func AESDecrypt(encryptedString string) (decryptedString string, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
decryptedString = ""
|
||||
err = errors.New("error in decrypting")
|
||||
}
|
||||
}()
|
||||
|
||||
key, err := hex.DecodeString(KEY)
|
||||
if err != nil {
|
||||
return "", errors.New("error in decoding key")
|
||||
}
|
||||
|
||||
enc, err := hex.DecodeString(encryptedString)
|
||||
if err != nil {
|
||||
return "", errors.New("error in decoding encrypted string")
|
||||
}
|
||||
|
||||
//Create a new Cipher Block from the key
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
//Create a new GCM
|
||||
aesGCM, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
//Get the nonce size
|
||||
nonceSize := aesGCM.NonceSize()
|
||||
|
||||
//Extract the nonce from the encrypted data
|
||||
nonce, ciphertext := enc[:nonceSize], enc[nonceSize:]
|
||||
|
||||
//Decrypt the data
|
||||
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return string(plaintext), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Verify Your Account</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f2f2f2;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
.container {
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
background-color: #ffffff;
|
||||
box-shadow: 0 0 10px rgba(226, 55, 55, 0.1);
|
||||
border-radius: 5px;
|
||||
}
|
||||
h1 {
|
||||
color: #333;
|
||||
font-size: 24px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
p {
|
||||
color: #666;
|
||||
font-size: 16px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
a {
|
||||
color: #007bff;
|
||||
text-decoration: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Verify Your Account</h1>
|
||||
<p>Hello, {{ .Email }}</p>
|
||||
<p>
|
||||
Thank you for using my template! To complete your registration and
|
||||
activate your account, please click the link below:
|
||||
</p>
|
||||
<div align="center">
|
||||
<a
|
||||
href="{{ .Verify }}"
|
||||
style="
|
||||
color: #333 !important;
|
||||
text-decoration: none;
|
||||
padding: 10px 20px;
|
||||
background-color: #007bff;
|
||||
border-radius: 5px;
|
||||
display: inline-block;
|
||||
"
|
||||
>Verify My Account</a
|
||||
>
|
||||
</div>
|
||||
<p>
|
||||
If you are unable to click the link above, please copy and paste the
|
||||
following URL into your web browser:
|
||||
</p>
|
||||
<p>{{ .Verify }}</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"github.com/Caknoooo/go-gin-clean-starter/config"
|
||||
|
||||
"gopkg.in/gomail.v2"
|
||||
)
|
||||
|
||||
func SendMail(toEmail string, subject string, body string) error {
|
||||
emailConfig, err := config.NewEmailConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mailer := gomail.NewMessage()
|
||||
mailer.SetHeader("From", emailConfig.AuthEmail)
|
||||
mailer.SetHeader("To", toEmail)
|
||||
mailer.SetHeader("Subject", subject)
|
||||
mailer.SetBody("text/html", body)
|
||||
|
||||
dialer := gomail.NewDialer(
|
||||
emailConfig.Host,
|
||||
emailConfig.Port,
|
||||
emailConfig.AuthEmail,
|
||||
emailConfig.AuthPassword,
|
||||
)
|
||||
|
||||
err = dialer.DialAndSend(mailer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const PATH = "assets"
|
||||
|
||||
func UploadFile(file *multipart.FileHeader, path string) error {
|
||||
parts := strings.Split(path, "/")
|
||||
fileID := parts[1]
|
||||
dirPath := fmt.Sprintf("%s/%s", PATH, parts[0])
|
||||
|
||||
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(dirPath, 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
filePath := fmt.Sprintf("%s/%s", dirPath, fileID)
|
||||
|
||||
uploadedFile, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer uploadedFile.Close()
|
||||
|
||||
// Using os.Create to open the file with appropriate permissions
|
||||
targetFile, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer targetFile.Close()
|
||||
|
||||
// Copy file contents from uploadedFile to targetFile
|
||||
_, err = io.Copy(targetFile, uploadedFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetExtensions(filename string) string {
|
||||
return strings.Split(filename, ".")[len(strings.Split(filename, "."))-1]
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package utils
|
||||
|
||||
type Response struct {
|
||||
Status bool `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Error any `json:"error,omitempty"`
|
||||
Data any `json:"data,omitempty"`
|
||||
Meta any `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
type EmptyObj struct{}
|
||||
|
||||
func BuildResponseSuccess(message string, data any) Response {
|
||||
res := Response{
|
||||
Status: true,
|
||||
Message: message,
|
||||
Data: data,
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func BuildResponseFailed(message string, err string, data any) Response {
|
||||
res := Response{
|
||||
Status: false,
|
||||
Message: message,
|
||||
Error: err,
|
||||
Data: data,
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
package providers
|
||||
|
||||
import (
|
||||
"github.com/Caknoooo/go-gin-clean-starter/config"
|
||||
authRepo "github.com/Caknoooo/go-gin-clean-starter/modules/auth/repository"
|
||||
userService "github.com/Caknoooo/go-gin-clean-starter/modules/user/service"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/controller"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/repository"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||
"github.com/samber/do"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func InitDatabase(injector *do.Injector) {
|
||||
do.ProvideNamed(injector, constants.DB, func(i *do.Injector) (*gorm.DB, error) {
|
||||
return config.SetUpDatabaseConnection(), nil
|
||||
})
|
||||
}
|
||||
|
||||
func RegisterDependencies(injector *do.Injector) {
|
||||
InitDatabase(injector)
|
||||
|
||||
do.ProvideNamed(injector, constants.JWTService, func(i *do.Injector) (service.JWTService, error) {
|
||||
return service.NewJWTService(), nil
|
||||
})
|
||||
|
||||
// Initialize
|
||||
db := do.MustInvokeNamed[*gorm.DB](injector, constants.DB)
|
||||
jwtService := do.MustInvokeNamed[service.JWTService](injector, constants.JWTService)
|
||||
|
||||
// Repository
|
||||
userRepository := repository.NewUserRepository(db)
|
||||
refreshTokenRepository := authRepo.NewRefreshTokenRepository(db)
|
||||
|
||||
// Service
|
||||
userService := userService.NewUserService(userRepository, refreshTokenRepository, jwtService, db)
|
||||
|
||||
// Controller
|
||||
do.Provide(
|
||||
injector, func(i *do.Injector) (controller.UserController, error) {
|
||||
return controller.NewUserController(i, userService), nil
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package script
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||
"github.com/samber/do"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Commands(injector *do.Injector) bool {
|
||||
db := do.MustInvokeNamed[*gorm.DB](injector, constants.DB)
|
||||
|
||||
var scriptName string
|
||||
|
||||
migrate := false
|
||||
seed := false
|
||||
run := false
|
||||
scriptFlag := false
|
||||
|
||||
for _, arg := range os.Args[1:] {
|
||||
if arg == "--migrate" {
|
||||
migrate = true
|
||||
}
|
||||
if arg == "--seed" {
|
||||
seed = true
|
||||
}
|
||||
if arg == "--run" {
|
||||
run = true
|
||||
}
|
||||
if strings.HasPrefix(arg, "--script:") {
|
||||
scriptFlag = true
|
||||
scriptName = strings.TrimPrefix(arg, "--script:")
|
||||
}
|
||||
}
|
||||
|
||||
if migrate {
|
||||
if err := database.Migrate(db); err != nil {
|
||||
log.Fatalf("error migration: %v", err)
|
||||
}
|
||||
log.Println("migration completed successfully")
|
||||
}
|
||||
|
||||
if seed {
|
||||
if err := database.Seeder(db); err != nil {
|
||||
log.Fatalf("error migration seeder: %v", err)
|
||||
}
|
||||
log.Println("seeder completed successfully")
|
||||
}
|
||||
|
||||
if scriptFlag {
|
||||
if err := Script(scriptName, db); err != nil {
|
||||
log.Fatalf("error script: %v", err)
|
||||
}
|
||||
log.Println("script run successfully")
|
||||
}
|
||||
|
||||
if run {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package script
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type (
|
||||
ExampleScript struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
)
|
||||
|
||||
func NewExampleScript(db *gorm.DB) *ExampleScript {
|
||||
return &ExampleScript{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ExampleScript) Run() error {
|
||||
// your script here
|
||||
fmt.Println("example script running")
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package script
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Script(scriptName string, db *gorm.DB) error {
|
||||
switch scriptName {
|
||||
case "example_script":
|
||||
exampleScript := NewExampleScript(db)
|
||||
return exampleScript.Run()
|
||||
default:
|
||||
return errors.New("script not found")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func SetUpDatabaseConnection() *gorm.DB {
|
||||
var (
|
||||
dbUser, dbPass, dbHost, dbName, dbPort string
|
||||
getenv = os.Getenv
|
||||
godotenv = godotenv.Load
|
||||
)
|
||||
|
||||
if getenv("APP_ENV") != constants.ENUM_RUN_PRODUCTION {
|
||||
err := godotenv("../.env")
|
||||
if err != nil {
|
||||
panic("Error loading .env file: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
dbUser = getenv("DB_USER")
|
||||
dbPass = getenv("DB_PASS")
|
||||
dbHost = getenv("DB_HOST")
|
||||
dbName = getenv("DB_NAME")
|
||||
dbPort = getenv("DB_PORT")
|
||||
|
||||
if dbUser == "" || dbPass == "" || dbHost == "" || dbName == "" || dbPort == "" {
|
||||
panic("Missing required environment variables")
|
||||
}
|
||||
|
||||
dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v TimeZone=Asia/Jakarta", dbHost, dbUser, dbPass, dbName, dbPort)
|
||||
|
||||
db, err := gorm.Open(postgres.New(postgres.Config{
|
||||
DSN: dsn,
|
||||
PreferSimpleProtocol: true,
|
||||
}), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("Failed to connect to database: " + err.Error())
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func Test_DBConnection(t *testing.T) {
|
||||
db := SetUpDatabaseConnection()
|
||||
assert.NoError(t, db.Error, "Expected no error during database connection")
|
||||
assert.NotNil(t, db, "Expected a non-nil database connection")
|
||||
}
|
||||
|
|
@ -0,0 +1,351 @@
|
|||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
authRepo "github.com/Caknoooo/go-gin-clean-starter/modules/auth/repository"
|
||||
authService "github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/controller"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/dto"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/repository"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/modules/user/service"
|
||||
"github.com/Caknoooo/go-gin-clean-starter/providers"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/samber/do"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func SetUpRoutes() *gin.Engine {
|
||||
r := gin.Default()
|
||||
return r
|
||||
}
|
||||
|
||||
func SetUpInjector() *do.Injector {
|
||||
injector := do.New()
|
||||
providers.RegisterDependencies(injector)
|
||||
return injector
|
||||
}
|
||||
|
||||
func SetupControllerUser() controller.UserController {
|
||||
var (
|
||||
db = SetUpDatabaseConnection()
|
||||
injector = SetUpInjector()
|
||||
userRepo = repository.NewUserRepository(db)
|
||||
jwtService = authService.NewJWTService()
|
||||
refreshTokenRepo = authRepo.NewRefreshTokenRepository(db)
|
||||
userService = service.NewUserService(userRepo, refreshTokenRepo, jwtService, db)
|
||||
userController = controller.NewUserController(injector, userService)
|
||||
)
|
||||
|
||||
return userController
|
||||
}
|
||||
|
||||
func InsertTestUser() ([]entities.User, error) {
|
||||
db := SetUpDatabaseConnection()
|
||||
users := []entities.User{
|
||||
{
|
||||
Name: "admin",
|
||||
Email: "admin1234@gmail.com",
|
||||
},
|
||||
{
|
||||
Name: "user",
|
||||
Email: "user1234@gmail.com",
|
||||
},
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func Test_GetAllUser_BadRequest(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
uc := SetupControllerUser()
|
||||
r.GET("/api/user", uc.GetAllUser)
|
||||
|
||||
// missing query params triggers binding error
|
||||
req, _ := http.NewRequest(http.MethodGet, "/api/user", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func Test_Register_OK(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
uc := SetupControllerUser()
|
||||
r.POST("/api/user", uc.Register)
|
||||
|
||||
payload := dto.UserCreateRequest{Name: "testuser", TelpNumber: "12345678", Email: "test@example.com", Password: "password123"}
|
||||
b, _ := json.Marshal(payload)
|
||||
req, _ := http.NewRequest(http.MethodPost, "/api/user", bytes.NewBuffer(b))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
var out struct {
|
||||
Data entities.User `json:"data"`
|
||||
}
|
||||
json.Unmarshal(w.Body.Bytes(), &out)
|
||||
assert.Equal(t, payload.Email, out.Data.Email)
|
||||
}
|
||||
|
||||
func Test_Register_BadRequest(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
uc := SetupControllerUser()
|
||||
r.POST("/api/user", uc.Register)
|
||||
|
||||
// empty body
|
||||
req, _ := http.NewRequest(http.MethodPost, "/api/user", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func Test_Login_BadRequest(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
uc := SetupControllerUser()
|
||||
r.POST("/api/user/login", uc.Login)
|
||||
|
||||
req, _ := http.NewRequest(http.MethodPost, "/api/user/login", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func Test_SendVerificationEmail_OK(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
uc := SetupControllerUser()
|
||||
r.POST("/api/user/send_verification_email", uc.SendVerificationEmail)
|
||||
|
||||
users, _ := InsertTestUser()
|
||||
reqBody, _ := json.Marshal(dto.SendVerificationEmailRequest{Email: users[0].Email})
|
||||
req, _ := http.NewRequest(http.MethodPost, "/api/user/send_verification_email", bytes.NewBuffer(reqBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
func Test_SendVerificationEmail_BadRequest(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
uc := SetupControllerUser()
|
||||
r.POST("/api/user/send_verification_email", uc.SendVerificationEmail)
|
||||
|
||||
// missing email
|
||||
req, _ := http.NewRequest(http.MethodPost, "/api/user/send_verification_email", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
func Test_VerifyEmail_BadRequest(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
uc := SetupControllerUser()
|
||||
r.POST("/api/user/verify_email", uc.VerifyEmail)
|
||||
|
||||
// missing token
|
||||
req, _ := http.NewRequest(http.MethodPost, "/api/user/verify_email", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
}
|
||||
|
||||
// Note: VerifyEmail_OK requires a valid token setup; implement when token creation is available.
|
||||
func Test_VerifyEmail_OK(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
uc := SetupControllerUser()
|
||||
r.POST("/api/user/verify_email", uc.VerifyEmail)
|
||||
|
||||
// TODO: insert valid verification token into DB and use here
|
||||
validToken := "valid-token-placeholder"
|
||||
reqBody, _ := json.Marshal(dto.VerifyEmailRequest{Token: validToken})
|
||||
req, _ := http.NewRequest(http.MethodPost, "/api/user/verify_email", bytes.NewBuffer(reqBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
// expecting BadRequest until token logic is implemented
|
||||
assert.NotEqual(t, http.StatusInternalServerError, w.Code)
|
||||
}
|
||||
|
||||
func Test_GetAllUser_OK(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
userController := SetupControllerUser()
|
||||
r.GET("/api/user", userController.GetAllUser)
|
||||
|
||||
expectedUsers, err := InsertTestUser()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to insert test users: %v", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/user", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create request: %v", err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
type Response struct {
|
||||
Data []entities.User `json:"data"`
|
||||
}
|
||||
|
||||
var response Response
|
||||
err = json.Unmarshal(w.Body.Bytes(), &response)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to unmarshal response body: %v", err)
|
||||
}
|
||||
|
||||
actualUsers := response.Data
|
||||
|
||||
for _, expectedUser := range expectedUsers {
|
||||
found := false
|
||||
for _, actualUser := range actualUsers {
|
||||
if expectedUser.Name == actualUser.Name && expectedUser.Email == actualUser.Email {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.True(t, found, "Expected user not found in actual users: %v", expectedUser)
|
||||
}
|
||||
}
|
||||
|
||||
// Test Login
|
||||
func Test_Login_OK(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
userController := SetupControllerUser()
|
||||
// first register
|
||||
r.POST("/api/user", userController.Register)
|
||||
r.POST("/api/user/login", userController.Login)
|
||||
|
||||
payload := dto.UserLoginRequest{
|
||||
Email: "loginuser@example.com",
|
||||
Password: "securepass",
|
||||
}
|
||||
// register user
|
||||
regBody, _ := json.Marshal(dto.UserCreateRequest{
|
||||
Name: "loginuser",
|
||||
Email: payload.Email,
|
||||
Password: payload.Password,
|
||||
})
|
||||
reqReg, _ := http.NewRequest(http.MethodPost, "/api/user", bytes.NewBuffer(regBody))
|
||||
reqReg.Header.Set("Content-Type", "application/json")
|
||||
httptest.NewRecorder() // ignore
|
||||
|
||||
// login
|
||||
body, _ := json.Marshal(payload)
|
||||
req, _ := http.NewRequest(http.MethodPost, "/api/user/login", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
type Resp struct {
|
||||
Data struct {
|
||||
Token string `json:"token"`
|
||||
Role string `json:"role"`
|
||||
} `json:"data"`
|
||||
}
|
||||
var resp Resp
|
||||
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
assert.Nil(t, err)
|
||||
assert.NotEmpty(t, resp.Data.Token)
|
||||
}
|
||||
|
||||
// Test Me
|
||||
func Test_Me_OK(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
userController := SetupControllerUser()
|
||||
r.GET("/api/user/me", func(c *gin.Context) {
|
||||
// insert and set user_id
|
||||
users, _ := InsertTestUser()
|
||||
c.Set("user_id", users[0].ID)
|
||||
userController.Me(c)
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, "/api/user/me", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
type Resp struct {
|
||||
Data entities.User `json:"data"`
|
||||
}
|
||||
var resp Resp
|
||||
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "admin", resp.Data.Name)
|
||||
}
|
||||
|
||||
// Test Update
|
||||
func Test_Update_OK(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
userController := SetupControllerUser()
|
||||
r.PUT("/api/user", func(c *gin.Context) {
|
||||
users, _ := InsertTestUser()
|
||||
c.Set("user_id", users[1].ID)
|
||||
userController.Update(c)
|
||||
})
|
||||
|
||||
update := dto.UserUpdateRequest{
|
||||
Name: "updatedName",
|
||||
TelpNumber: "87654321",
|
||||
}
|
||||
body, _ := json.Marshal(update)
|
||||
req, _ := http.NewRequest(http.MethodPut, "/api/user", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
type Resp struct {
|
||||
Data entities.User `json:"data"`
|
||||
}
|
||||
var resp Resp
|
||||
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, update.Name, resp.Data.Name)
|
||||
}
|
||||
|
||||
// Test Delete
|
||||
func Test_Delete_OK(t *testing.T) {
|
||||
r := SetUpRoutes()
|
||||
userController := SetupControllerUser()
|
||||
r.DELETE("/api/user", func(c *gin.Context) {
|
||||
users, _ := InsertTestUser()
|
||||
c.Set("user_id", users[0].ID)
|
||||
userController.Delete(c)
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest(http.MethodDelete, "/api/user", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
}
|
||||
Loading…
Reference in New Issue