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
19 changes: 10 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ module github.com/bit-issues/backend
go 1.25.7

require (
github.com/go-core-fx/bunfx v0.0.1
github.com/go-core-fx/bunfx v0.0.2-0.20260409044649-8812f14d88ce
github.com/go-core-fx/config v0.1.0
github.com/go-core-fx/fiberfx v0.5.0
github.com/go-core-fx/goosefx v0.0.1
github.com/go-core-fx/healthfx v0.0.2-0.20260109013230-f7729a0a06bc
github.com/go-core-fx/logger v0.0.1
github.com/go-core-fx/sqlfx v0.1.0
github.com/go-core-fx/validatorfx v0.0.2
github.com/go-playground/validator/v10 v10.28.0
github.com/go-sql-driver/mysql v1.9.3
github.com/gofiber/fiber/v2 v2.52.12
github.com/golang-jwt/jwt/v5 v5.3.1
github.com/google/uuid v1.6.0
github.com/pressly/goose/v3 v3.27.0
github.com/prometheus/client_golang v1.23.2
github.com/samber/lo v1.52.0
Expand All @@ -21,6 +24,7 @@ require (
github.com/uptrace/bun/dialect/mysqldialect v1.2.18
go.uber.org/fx v1.24.0
go.uber.org/zap v1.27.1
golang.org/x/crypto v0.49.0
)

require (
Expand All @@ -43,11 +47,9 @@ require (
github.com/go-openapi/swag v0.19.15 // 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.28.0 // indirect
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/gofiber/contrib/fiberzap/v2 v2.1.6 // indirect
github.com/gofiber/swagger v1.1.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down Expand Up @@ -85,13 +87,12 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.48.0 // indirect
golang.org/x/mod v0.33.0 // indirect
golang.org/x/net v0.50.0 // indirect
golang.org/x/sync v0.19.0 // indirect
golang.org/x/sys v0.42.0 // indirect
golang.org/x/text v0.34.0 // indirect
golang.org/x/tools v0.41.0 // indirect
golang.org/x/net v0.51.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.43.0 // indirect
golang.org/x/text v0.35.0 // indirect
golang.org/x/tools v0.42.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
30 changes: 16 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/gabriel-vasile/mimetype v1.4.10 h1:zyueNbySn/z8mJZHLt6IPw0KoZsiQNszIpU+bX4+ZK0=
github.com/gabriel-vasile/mimetype v1.4.10/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
github.com/go-core-fx/bunfx v0.0.1 h1:aGUc4klPifBEQjycbnId7VoZ50UdEqTv6s5mX7F0V/s=
github.com/go-core-fx/bunfx v0.0.1/go.mod h1:spydLRsh5vQJekRAFnQLEvjuekUZOC8QTf/jkyw2Bog=
github.com/go-core-fx/bunfx v0.0.2-0.20260409044649-8812f14d88ce h1:+Pz9nIc5BQS/RFRAS4TrQxlsVQaDX4z3+d7UWE9JAMs=
github.com/go-core-fx/bunfx v0.0.2-0.20260409044649-8812f14d88ce/go.mod h1:spydLRsh5vQJekRAFnQLEvjuekUZOC8QTf/jkyw2Bog=
github.com/go-core-fx/config v0.1.0 h1:uKmo+mTt5a8Gtusb7Xf4gkrGcLIbm2doTEUMkdd6oGo=
github.com/go-core-fx/config v0.1.0/go.mod h1:gvoLaHr5fHfG5DlYYtNSPTqRlbnxWMqiWL4iWy2oezY=
github.com/go-core-fx/fiberfx v0.5.0 h1:e42gDP7R5k2T93pt1ibVS6nrnPNaGR/+6efHO90a398=
Expand Down Expand Up @@ -78,6 +78,8 @@ github.com/gofiber/fiber/v2 v2.52.12 h1:0LdToKclcPOj8PktUdIKo9BUohjjwfnQl42Dhw8/
github.com/gofiber/fiber/v2 v2.52.12/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw=
github.com/gofiber/swagger v1.1.1 h1:FZVhVQQ9s1ZKLHL/O0loLh49bYB5l1HEAgxDlcTtkRA=
github.com/gofiber/swagger v1.1.1/go.mod h1:vtvY/sQAMc/lGTUCg0lqmBL7Ht9O7uzChpbvJeJQINw=
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down Expand Up @@ -207,30 +209,30 @@ go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
golang.org/x/crypto v0.49.0 h1:+Ng2ULVvLHnJ/ZFEq4KdcDd/cfjrrjjNSXNzxg0Y4U4=
golang.org/x/crypto v0.49.0/go.mod h1:ErX4dUh2UM+CFYiXZRTcMpEcN8b/1gxEuv3nODoYtCA=
golang.org/x/exp v0.0.0-20260218203240-3dfff04db8fa h1:Zt3DZoOFFYkKhDT3v7Lm9FDMEV06GpzjG2jrqW+QTE0=
golang.org/x/exp v0.0.0-20260218203240-3dfff04db8fa/go.mod h1:K79w1Vqn7PoiZn+TkNpx3BUWUQksGO3JcVX6qIjytmA=
golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8=
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
golang.org/x/net v0.50.0 h1:ucWh9eiCGyDR3vtzso0WMQinm2Dnt8cFMuQa9K33J60=
golang.org/x/net v0.50.0/go.mod h1:UgoSli3F/pBgdJBHCTc+tp3gmrU4XswgGRgtnwWTfyM=
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo=
golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8=
golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc=
golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg=
golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k=
golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
6 changes: 4 additions & 2 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (

"github.com/bit-issues/backend/internal/config"
"github.com/bit-issues/backend/internal/db"
"github.com/bit-issues/backend/internal/example"
"github.com/bit-issues/backend/internal/jwt"
"github.com/bit-issues/backend/internal/server"
"github.com/bit-issues/backend/internal/users"
"github.com/go-core-fx/bunfx"
"github.com/go-core-fx/fiberfx"
"github.com/go-core-fx/goosefx"
Expand Down Expand Up @@ -47,7 +48,8 @@ func Run(version healthfx.Version) {
//
// BUSINESS MODULES
fx.Supply(version),
example.Module(),
jwt.Module(),
users.Module(),
//
fx.Invoke(func(lc fx.Lifecycle, logger *zap.Logger) {
lc.Append(fx.Hook{
Expand Down
20 changes: 11 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@ type databaseConfig struct {
MaxIdleConns int `koanf:"max_idle_conns"`
}

type exampleConfig struct {
Example string `koanf:"example"`
type jwtConfig struct {
Secret string `koanf:"secret"`
AccessTTL time.Duration `koanf:"access_ttl"`
Issuer string `koanf:"issuer"`
}

type Config struct {
HTTP http `koanf:"http"`
Database databaseConfig `koanf:"database"`

Example exampleConfig `koanf:"example"`
JWT jwtConfig `koanf:"jwt"`
}

func Default() Config {
//nolint:gosec // default values
//nolint:gosec,mnd // default values
return Config{
HTTP: http{
Address: "127.0.0.1:3000",
Expand All @@ -55,15 +56,16 @@ func Default() Config {
},
},
Database: databaseConfig{
URL: "mariadb://bit-issues:bit-issues@127.0.0.1:3306/bit-issues?charset=utf8mb4&parseTime=True&loc=UTC",
URL: "mariadb://bit-issues:bit-issues@127.0.0.1:3306/bit-issues?charset=utf8mb4&parseTime=True&loc=UTC&clientFoundRows=true",
ConnMaxIdleTime: 0,
ConnMaxLifetime: 0,
MaxOpenConns: 0,
MaxIdleConns: 0,
},

Example: exampleConfig{
Example: "example",
JWT: jwtConfig{
Secret: "secret",
AccessTTL: time.Minute * 15,
Comment thread
capcom6 marked this conversation as resolved.
Issuer: "bitissues.dev",
},
}
}
Expand Down
10 changes: 6 additions & 4 deletions internal/config/module.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"github.com/bit-issues/backend/internal/example"
"github.com/bit-issues/backend/internal/jwt"
"github.com/go-core-fx/fiberfx"
"github.com/go-core-fx/fiberfx/openapi"
"github.com/go-core-fx/sqlfx"
Expand Down Expand Up @@ -37,9 +37,11 @@ func Module() fx.Option {
}
},
),
fx.Provide(func(cfg Config) example.Config {
return example.Config{
Example: cfg.Example.Example,
fx.Provide(func(cfg Config) jwt.Config {
return jwt.Config{
Secret: cfg.JWT.Secret,
AccessTTL: cfg.JWT.AccessTTL,
Issuer: cfg.JWT.Issuer,
}
}),
)
Expand Down
19 changes: 19 additions & 0 deletions internal/db/migrations/20260408120000_users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS `users` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`email` VARCHAR(255) NOT NULL,
Comment thread
capcom6 marked this conversation as resolved.
`password_hash` VARCHAR(255) NOT NULL,
`role` ENUM('admin', 'user') NOT NULL DEFAULT 'user',
`status` ENUM('pending', 'active', 'blocked') NOT NULL DEFAULT 'pending',
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_users_email` (`email`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
-- +goose StatementEnd
---
Comment thread
capcom6 marked this conversation as resolved.
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS `users`;
-- +goose StatementEnd
32 changes: 32 additions & 0 deletions internal/jwt/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package jwt

import (
"fmt"
"time"
)

const (
minSecretLength = 32
)

type Config struct {
Secret string
AccessTTL time.Duration
Issuer string
}

func (c Config) Validate() error {
if c.Secret == "" {
return fmt.Errorf("%w: secret is required", ErrInvalidConfig)
}

if len(c.Secret) < minSecretLength {
return fmt.Errorf("%w: secret must be at least %d bytes", ErrInvalidConfig, minSecretLength)
}

if c.AccessTTL <= 0 {
return fmt.Errorf("%w: access ttl must be positive", ErrInvalidConfig)
}

return nil
}
8 changes: 8 additions & 0 deletions internal/jwt/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Package jwt provides JWT token generation and validation for the application.
//
// The module covers:
// - token generation with HS256 algorithm,
// - token validation and claims extraction,
// - 24-hour token expiry,
// - integration with the users domain.
package jwt
15 changes: 15 additions & 0 deletions internal/jwt/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package jwt

import (
"github.com/bit-issues/backend/internal/users"
"github.com/golang-jwt/jwt/v5"
)

// Claims represents the JWT token claims.
type Claims struct {
jwt.RegisteredClaims

UserID int64 `json:"user_id"`
Role users.Role `json:"role"`
Status users.Status `json:"status"`
}
9 changes: 9 additions & 0 deletions internal/jwt/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package jwt

import "errors"

var (
ErrInvalidConfig = errors.New("invalid config")
ErrInvalidToken = errors.New("invalid token")
ErrExpiredToken = errors.New("token has expired")
)
18 changes: 18 additions & 0 deletions internal/jwt/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package jwt

import (
"github.com/go-core-fx/logger"
"go.uber.org/fx"
)

// Module creates and returns an FX module for the jwt package.
//
// The module provides:
// - JWTService for token generation and validation
func Module() fx.Option {
return fx.Module(
"jwt",
logger.WithNamedLogger("jwt"),
fx.Provide(NewService),
)
}
Loading
Loading