initial commit

This commit is contained in:
Hafizh A. Y
2025-09-25 10:46:46 +07:00
parent c43544e5e8
commit 10506238ae
64 changed files with 3564 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
package config
import (
"os"
"time"
)
type AuthCfg struct {
Issuer string
JWTSecret string
AccessTTL time.Duration
RefreshTTL time.Duration
RefreshCookieName string
RefreshCookiePath string
}
func LoadAuth() AuthCfg {
return AuthCfg{
JWTSecret: getenv("JWT_SECRET", "dev-secret-change-me"),
AccessTTL: getenvDuration("ACCESS_TTL", 10*time.Minute),
RefreshTTL: getenvDuration("REFRESH_TTL", 30*24*time.Hour),
RefreshCookieName: getenv("REFRESH_COOKIE_NAME", "rt"),
RefreshCookiePath: getenv("REFRESH_COOKIE_PATH", "/api/auth"),
Issuer: getenv("ISSUER", "http://localhost:8080"),
}
}
func getenv(k, def string) string {
if v := os.Getenv(k); v != "" {
return v
}
return def
}
func getenvDuration(k string, def time.Duration) time.Duration {
if v := os.Getenv(k); v != "" {
d, err := time.ParseDuration(v)
if err == nil {
return d
}
}
return def
}
+111
View File
@@ -0,0 +1,111 @@
package config
import (
"fmt"
"github.com/hafizhproject45/Golang-Boilerplate.git/internal/utils"
"github.com/spf13/viper"
)
var (
IsProd bool
AppHost string
Version string
LogLevel string
AppPort int
DBHost string
DBUser string
DBPassword string
DBName string
DBPort int
JWTSecret string
JWTAccessExp int
JWTRefreshExp int
JWTResetPasswordExp int
JWTVerifyEmailExp int
PostgresDSN string
RedisURL string
Issuer string
SMTPHost string
SMTPPort int
SMTPUsername string
SMTPPassword string
EmailFrom string
GoogleClientID string
GoogleClientSecret string
RedirectURL string
)
func init() {
loadConfig()
// server configuration
IsProd = viper.GetString("APP_ENV") == "prod"
// AppHost = viper.GetString("APP_HOST")
// AppPort = viper.GetInt("APP_PORT")
AppHost = viper.GetString("APP_HOST")
if AppHost == "" {
AppHost = "0.0.0.0"
}
AppPort = viper.GetInt("APP_PORT")
if AppPort == 0 {
AppPort = 8080
}
Version = viper.GetString("VERSION")
LogLevel = viper.GetString("LOG_LEVEL")
// database configuration
DBHost = viper.GetString("DB_HOST")
DBUser = viper.GetString("DB_USER")
DBPassword = viper.GetString("DB_PASSWORD")
DBName = viper.GetString("DB_NAME")
DBPort = viper.GetInt("DB_PORT")
PostgresDSN = viper.GetString("POSTGRES_DSN")
if PostgresDSN == "" {
PostgresDSN = fmt.Sprintf(
"postgres://%s:%s@%s:%d/%s?sslmode=disable",
DBUser, DBPassword, DBHost, DBPort, DBName,
)
}
// jwt configuration
JWTSecret = viper.GetString("JWT_SECRET")
JWTAccessExp = viper.GetInt("JWT_ACCESS_EXP_MINUTES")
JWTRefreshExp = viper.GetInt("JWT_REFRESH_EXP_DAYS")
JWTResetPasswordExp = viper.GetInt("JWT_RESET_PASSWORD_EXP_MINUTES")
JWTVerifyEmailExp = viper.GetInt("JWT_VERIFY_EMAIL_EXP_MINUTES")
// Redis / OIDC
RedisURL = viper.GetString("REDIS_URL")
if RedisURL == "" {
RedisURL = "redis://redis:6379/0"
}
Issuer = viper.GetString("ISSUER")
if Issuer == "" {
// fallback ke SSO_ISSUER jika kamu sudah pakai itu sebelumnya
Issuer = viper.GetString("SSO_ISSUER")
}
// SMTP configuration
SMTPHost = viper.GetString("SMTP_HOST")
SMTPPort = viper.GetInt("SMTP_PORT")
SMTPUsername = viper.GetString("SMTP_USERNAME")
SMTPPassword = viper.GetString("SMTP_PASSWORD")
EmailFrom = viper.GetString("EMAIL_FROM")
// oauth2 configuration
GoogleClientID = viper.GetString("GOOGLE_CLIENT_ID")
GoogleClientSecret = viper.GetString("GOOGLE_CLIENT_SECRET")
RedirectURL = viper.GetString("REDIRECT_URL")
}
func loadConfig() {
viper.AutomaticEnv()
viper.SetConfigFile(".env")
if err := viper.ReadInConfig(); err == nil {
utils.Log.Info("Config file loaded from .env")
} else {
utils.Log.Warn("No .env file found, using environment variables only")
}
}
+20
View File
@@ -0,0 +1,20 @@
package config
import (
"github.com/hafizhproject45/Golang-Boilerplate.git/internal/utils"
"github.com/bytedance/sonic"
"github.com/gofiber/fiber/v2"
)
func FiberConfig() fiber.Config {
return fiber.Config{
Prefork: IsProd,
CaseSensitive: true,
ServerHeader: "Fiber",
AppName: "Fiber API",
ErrorHandler: utils.ErrorHandler,
JSONEncoder: sonic.Marshal,
JSONDecoder: sonic.Unmarshal,
}
}
+27
View File
@@ -0,0 +1,27 @@
package config
import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
type Config struct {
GoogleLoginConfig oauth2.Config
}
var AppConfig Config
func GoogleConfig() oauth2.Config {
AppConfig.GoogleLoginConfig = oauth2.Config{
RedirectURL: RedirectURL,
ClientID: GoogleClientID,
ClientSecret: GoogleClientSecret,
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
},
Endpoint: google.Endpoint,
}
return AppConfig.GoogleLoginConfig
}
+17
View File
@@ -0,0 +1,17 @@
package config
var allRoles = map[string][]string{
"user": {},
"admin": {"getUsers", "manageUsers"},
}
var Roles = getKeys(allRoles)
var RoleRights = allRoles
func getKeys(m map[string][]string) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
+8
View File
@@ -0,0 +1,8 @@
package config
const (
TokenTypeAccess = "access"
TokenTypeRefresh = "refresh"
TokenTypeResetPassword = "resetPassword"
TokenTypeVerifyEmail = "verifyEmail"
)