Ovaj commit hvata radnu kopiju sa 151 kakva je zatecena 2026-07-25 (nije pravljena u ovoj sesiji) - funkcionalnost prijave administratora preko admin_users tabele (username/password_hash umesto jednog ADMIN_PASSWORD), plus logo/favicon i go 1.24.0 + golang.org/x/crypto zavisnost. Binarni fajl koji trenutno radi na produkciji je bas iz ovog stanja. Zarobljeno kao osnova pre preimenovanja dal-license-server -> licence-server. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
128 lines
3.1 KiB
Go
128 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"dal-license-server/internal/config"
|
|
"dal-license-server/internal/handler"
|
|
"dal-license-server/internal/model"
|
|
"dal-license-server/internal/repository"
|
|
"dal-license-server/internal/router"
|
|
"dal-license-server/internal/service"
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
func main() {
|
|
cfg := config.Load()
|
|
|
|
// Connect to MySQL
|
|
db, err := sql.Open("mysql", cfg.DSN())
|
|
if err != nil {
|
|
log.Fatal("DB open: ", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
if err := db.Ping(); err != nil {
|
|
log.Fatal("DB ping: ", err)
|
|
}
|
|
|
|
// Run migrations
|
|
runMigrations(db)
|
|
|
|
// Crypto service
|
|
cryptoSvc, err := service.NewCryptoService(cfg.RSAPrivateKey)
|
|
if err != nil {
|
|
log.Fatal("Crypto: ", err)
|
|
}
|
|
|
|
// Repositories
|
|
licenseRepo := repository.NewLicenseRepo(db)
|
|
activationRepo := repository.NewActivationRepo(db)
|
|
auditRepo := repository.NewAuditRepo(db)
|
|
userRepo := repository.NewUserRepo(db)
|
|
|
|
// Seed default admin user
|
|
seedDefaultAdmin(userRepo, cfg.AdminPassword)
|
|
|
|
// Services
|
|
licenseSvc := service.NewLicenseService(licenseRepo, auditRepo)
|
|
activationSvc := service.NewActivationService(activationRepo, licenseRepo, auditRepo, cryptoSvc, licenseSvc)
|
|
|
|
// Handlers
|
|
clientHandler := handler.NewClientHandler(activationSvc)
|
|
adminHandler := handler.NewAdminHandler(licenseSvc, activationSvc, auditRepo)
|
|
dashboardHandler := handler.NewDashboardHandler(licenseSvc, activationSvc, auditRepo, userRepo, "templates")
|
|
|
|
// Rate limits
|
|
rlActivate, _ := strconv.Atoi(cfg.RateLimitActivate)
|
|
if rlActivate == 0 {
|
|
rlActivate = 10
|
|
}
|
|
rlValidate, _ := strconv.Atoi(cfg.RateLimitValidate)
|
|
if rlValidate == 0 {
|
|
rlValidate = 60
|
|
}
|
|
|
|
// Router
|
|
mux := router.Setup(clientHandler, adminHandler, dashboardHandler, cfg.AdminAPIKey, rlActivate, rlValidate)
|
|
|
|
addr := ":" + cfg.Port
|
|
fmt.Printf("DAL License Server pokrenut na http://localhost%s\n", addr)
|
|
if err := http.ListenAndServe(addr, mux); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func runMigrations(db *sql.DB) {
|
|
files := []string{
|
|
"migrations/001_create_tables.sql",
|
|
"migrations/002_seed_products.sql",
|
|
"migrations/003_create_admin_users.sql",
|
|
}
|
|
for _, f := range files {
|
|
data, err := os.ReadFile(f)
|
|
if err != nil {
|
|
log.Printf("Migration %s: %v", f, err)
|
|
continue
|
|
}
|
|
_, err = db.Exec(string(data))
|
|
if err != nil {
|
|
log.Printf("Migration %s: %v (may already exist)", f, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func seedDefaultAdmin(userRepo *repository.UserRepo, adminPassword string) {
|
|
count, err := userRepo.Count()
|
|
if err != nil {
|
|
log.Printf("Seed admin: count error: %v", err)
|
|
return
|
|
}
|
|
if count > 0 {
|
|
return
|
|
}
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(adminPassword), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
log.Fatal("Seed admin: bcrypt error: ", err)
|
|
}
|
|
|
|
user := &model.AdminUser{
|
|
Username: "admin",
|
|
PasswordHash: string(hash),
|
|
FullName: "Administrator",
|
|
Active: true,
|
|
}
|
|
if err := userRepo.Create(user); err != nil {
|
|
log.Printf("Seed admin: %v (may already exist)", err)
|
|
} else {
|
|
log.Println("Default admin korisnik kreiran (username: admin)")
|
|
}
|
|
}
|