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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.env
.docker_build/
docker-compose.override.yml
tmp/
.theia/
vendor/
config/database.yml
Expand Down
23 changes: 19 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
FROM golang:1.12 as builder
# Builder Stage
FROM golang:1.23 as builder

WORKDIR /usr/app

# Proje dosyalarını kopyala
COPY . .

# Uygulamayı derle
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o ibanim .

# Migrations'ı derle
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o migrate ./migrations/

# Multi stage
# Multi-stage build

# Alpine Stage (minimal imaj)
FROM alpine:latest

RUN apk --no-cache add ca-certificates
Expand All @@ -18,10 +23,20 @@ ENV PATH /root

WORKDIR /root/

# Builder aşamasından derlenmiş ikili dosyaları al
COPY --from=builder /usr/app/ibanim .

COPY --from=builder /usr/app/migrate .

# Konfigürasyon ve şablon dosyalarını kopyala
COPY --from=builder /usr/app/config /root/config
COPY --from=builder /usr/app/templates /root/templates

# Örnek konfigürasyon dosyalarını kopyala
COPY --from=builder /usr/app/config/application.yml /root/config/application.yml
COPY --from=builder /usr/app/config/smtp.yml /root/config/smtp.yml
COPY --from=builder /usr/app/config/database.yml /root/config/database.yml

EXPOSE 8080

CMD ["./ibanim"]
# Başlatma komutunu belirt
CMD ["./ibanim"]
4 changes: 0 additions & 4 deletions config/application.example.yml

This file was deleted.

5 changes: 0 additions & 5 deletions config/database.example.yml

This file was deleted.

64 changes: 33 additions & 31 deletions config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,65 @@ package config
import (
"errors"
"fmt"
"log"
"os"
"time"

"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/mattn/go-sqlite3"
"github.com/monopayments/iban.im/model"
"github.com/qor/validations"

// _ "github.com/jinzhu/gorm/dialects/sqlite" TODO - disabled for compile time issue
"os"
"time"
)

// global DB variable => TODO repository pattern
var DB *gorm.DB

func init() {
func InitDB() {
var err error
if Config.App.Env == "gitpod" && Config.Db.Adapter == "postgres" {
if err = os.Unsetenv("PGHOSTADDR"); err != nil {
panic(err)
}

adapter := os.Getenv("DB_ADAPTER")
if adapter == "" {
adapter = "postgres"
}

var connStr string
adapter := Config.Db.Adapter
if adapter == "mysql" {
connStr = fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?parseTime=True&loc=Local", Config.Db.User, Config.Db.Password, Config.Db.Host, Config.Db.Port, Config.Db.Name)
connStr = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=True&loc=Local",
os.Getenv("DB_USER"),
os.Getenv("DB_PASSWORD"),
os.Getenv("DB_HOST"),
os.Getenv("DB_PORT"),
os.Getenv("DB_NAME"),
)
} else if adapter == "postgres" {
connStr = fmt.Sprintf("postgres://%v:%v@%v/%v?sslmode=disable", Config.Db.User, Config.Db.Password, Config.Db.Host, Config.Db.Name)
} else if adapter == "sqlite3" || adapter == "sqlite" {
connStr = Config.Db.Name
connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
os.Getenv("DB_USER"),
os.Getenv("DB_PASSWORD"),
os.Getenv("DB_HOST"),
os.Getenv("DB_PORT"),
os.Getenv("DB_NAME"),
)
} else {
panic(errors.New("your database is not supported"))
panic(errors.New("unsupported database adapter"))
}

DB, err = gorm.Open(adapter, connStr)
if err != nil {
panic(err)
log.Fatal("Database connection failed:", err)
}

// Veritabanı bağlantısını kontrol etme
if err := DB.DB().Ping(); err != nil {
log.Fatalf("Error connecting to DB: %v", err)
} else {
log.Println("Successfully connected to DB")
}

validations.RegisterCallbacks(DB)
DB.LogMode(Config.App.Debug)
DB.LogMode(true)
DB.DB().SetMaxIdleConns(10)
DB.DB().SetMaxOpenConns(30)
DB.DB().SetConnMaxLifetime(time.Second * 60)

DB.AutoMigrate(&model.User{}, &model.Iban{}, &model.Group{})

// TODO ping control for mysql
if adapter == "mysql" {
go checkPing()
}
}

func checkPing() {
for {
time.Sleep(time.Second * 15)
DB.DB().Ping()
}
}
5 changes: 0 additions & 5 deletions config/smtp.example.yml

This file was deleted.

16 changes: 12 additions & 4 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package db

import (
"fmt"
"os"

"github.com/jinzhu/gorm"
Expand All @@ -13,10 +14,10 @@ type DB struct {
*gorm.DB
}

var connStrMap = map[string]string {
"localhost" : "host=localhost port=5432 user=ibanim dbname=ibanim password=ibanim sslmode=disable",
"docker" : "host=host.docker.internal port=5432 user=ibanim dbname=ibanim password=ibanim sslmode=disable",
"gitpod" : "host=localhost port=5432 user=gitpod dbname=ibanim sslmode=disable",
var connStrMap = map[string]string{
"localhost": "host=localhost port=5432 user=ibanim dbname=ibanim password=ibanim sslmode=disable",
"docker": "host=host.docker.internal port=5432 user=ibanim dbname=ibanim password=ibanim sslmode=disable",
"gitpod": "host=localhost port=5432 user=gitpod dbname=ibanim sslmode=disable",
}

// ConnectDB : connecting DB
Expand All @@ -30,6 +31,13 @@ func ConnectDB(env string) (*DB, error) {
if err != nil {
panic(err)
}
//Check database connection
err = db.DB().Ping()
if err != nil {
panic("Database connection failed!" + err.Error())
} else {
fmt.Println("database connection successful!")
}

return &DB{db}, nil
}
35 changes: 35 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
services:
db:
image: postgres:15
restart: always
container_name: postgres_db
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
POSTGRES_DB: mydb
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data

app:
build: .
restart: always
container_name: go_app
environment:
- DB_ADAPTER=postgres
- DB_HOST=db
- DB_PORT=5432
- DB_USER=myuser
- DB_PASSWORD=mypassword
- DB_NAME=mydb
depends_on:
- db
ports:
- "4880:8080"
volumes:
- ./templates:/usr/app/templates
- ./config:/usr/app/config

volumes:
postgres_data:
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/lib/pq v1.10.3 // indirect
github.com/mattdamon108/gqlmerge v0.2.3
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.24
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/qor/qor v1.2.0 // indirect
github.com/qor/validations v0.0.0-20171228122639-f364bca61b46
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA=
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/microcosm-cc/bluemonday v1.0.3/go.mod h1:8iwZnFn2CDDNZ0r6UXhF4xawGvzaqzCRa1n3/lO3W2w=
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=
Expand Down
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ const identityKey = "UserID"

func main() {

router := gin.New()
router := gin.Default()
router.Use(func(c *gin.Context) {
fmt.Println(">>> New request:", c.Request.Method, c.Request.URL.Path)
c.Next()
})

router.Use(gin.Logger())
router.LoadHTMLGlob("templates/*.tmpl.html")
Expand All @@ -42,7 +46,11 @@ func main() {
log.Fatal("JWT Error:" + err.Error())
}

router.POST("/api/login", authMiddleware.LoginHandler)
router.POST("/api/login", func(c *gin.Context) {
fmt.Println(">>> /api/login endpoint called") // Log ekleyelim
authMiddleware.LoginHandler(c)
})

auth := router.Group("/auth")
auth.GET("/refresh_token", authMiddleware.RefreshHandler)

Expand Down
47 changes: 22 additions & 25 deletions model/iban.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ package model

import (
"fmt"
"github.com/jinzhu/gorm"
"golang.org/x/crypto/bcrypt"
"strings"
"time"

"github.com/jinzhu/gorm"
"golang.org/x/crypto/bcrypt"
)

// Iban : Model with injected fields `ID`, `CreatedAt`, `UpdatedAt`
type Iban struct {
IbanID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
Text string `gorm:"type:varchar(100);not null"`
Description string
Password string
Handle string `gorm:"type:varchar(20);not null"`
Active bool
IsPrivate bool
OwnerID uint
OwnerType string
IbanID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
Text string `gorm:"type:varchar(100);not null"`
Description string
Password string
Handle string `gorm:"type:varchar(20);not null"`
Active bool
IsPrivate bool
OwnerID uint
OwnerType string
}

// HashPassword : hashing the password
Expand All @@ -39,17 +40,13 @@ func (iban *Iban) HashPassword() {
func (iban *Iban) ComparePassword(password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(iban.Password), []byte(password))

if err != nil {
return false
}

return true
return err == nil
}

// Check Handle before create or update = must be add as index to db
func (iban *Iban) CheckHandle(tx *gorm.DB) (exist bool) {
var ibans []Iban
tx.Where("owner_id = ? AND handle = ?",iban.OwnerID,iban.Handle).Find(&ibans)
tx.Where("owner_id = ? AND handle = ?", iban.OwnerID, iban.Handle).Find(&ibans)
for _, tmp := range ibans {
if iban.Handle == tmp.Handle && iban.IbanID != tmp.IbanID {
exist = true
Expand All @@ -59,19 +56,19 @@ func (iban *Iban) CheckHandle(tx *gorm.DB) (exist bool) {
}

// BeforeSave Callback
func (iban *Iban) BeforeSave(tx *gorm.DB) (err error) {
func (iban *Iban) BeforeSave(tx *gorm.DB) (err error) {
if iban.CheckHandle(tx) {
err = fmt.Errorf("handle already exist")
}
return
}

func (iban *Iban) Validate(db *gorm.DB) {
func (iban *Iban) Validate(db *gorm.DB) {
if strings.TrimSpace(iban.Text) == "" {
db.AddError(fmt.Errorf("you have to provide IBAN"))
}else if strings.TrimSpace(iban.Handle) == "" {
} else if strings.TrimSpace(iban.Handle) == "" {
db.AddError(fmt.Errorf("you have to provide handle"))
}else if iban.IsPrivate && strings.TrimSpace(iban.Password) == "" {
} else if iban.IsPrivate && strings.TrimSpace(iban.Password) == "" {
db.AddError(fmt.Errorf("you have to provide password"))
}
}
}
Loading
Loading