admin panel, login page with auth session.
This commit is contained in:
40
main.go
40
main.go
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"Portifolio/internal/database"
|
||||
"Portifolio/internal/middleware"
|
||||
"Portifolio/internal/service"
|
||||
"Portifolio/internal/website"
|
||||
"database/sql"
|
||||
@@ -9,6 +10,9 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
@@ -32,12 +36,32 @@ func corsMiddleware(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func EnvCheck() error {
|
||||
|
||||
if os.Getenv("username") == "" {
|
||||
return fmt.Errorf("Missing: username")
|
||||
}
|
||||
if os.Getenv("password") == "" {
|
||||
return fmt.Errorf("Missing: password")
|
||||
}
|
||||
if os.Getenv("AUTH_TOTP_SECRET") == "" {
|
||||
return fmt.Errorf("Missing: AUTH_TOTP_SECRET")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RegisterRoutes(mux *http.ServeMux, db *sql.DB) {
|
||||
svc := service.New(db) // ← create it here, once
|
||||
|
||||
// ── Website (HTMX fragments) ──────────────────────────────
|
||||
mux.HandleFunc("/", website.Getsite())
|
||||
mux.HandleFunc("/style", website.Getstylesheet())
|
||||
mux.HandleFunc("/styles/main", website.GetstylesheetMain())
|
||||
mux.HandleFunc("/styles/login", website.GetstylesheetLogin())
|
||||
mux.HandleFunc("/styles/admin", website.GetstylesheetAdmin())
|
||||
mux.Handle("/admin", middleware.RequireSession(website.GetAdmin()))
|
||||
mux.HandleFunc("/login", website.GetAdminLogin())
|
||||
mux.HandleFunc("POST /auth/login", website.LoginHandler())
|
||||
mux.HandleFunc("/health/fragment", website.HealthFragment(svc))
|
||||
mux.HandleFunc("/positions/fragment", website.PositionsFragment(svc))
|
||||
mux.HandleFunc("/company/fragment", website.CompanyFragment(svc))
|
||||
@@ -47,24 +71,19 @@ func RegisterRoutes(mux *http.ServeMux, db *sql.DB) {
|
||||
|
||||
// ── API (JSON) ────────────────────────────────────────────
|
||||
mux.HandleFunc("/health", svc.HealthHandler())
|
||||
// Trades
|
||||
mux.HandleFunc("POST /trade/add", svc.AddTradeHandler())
|
||||
mux.HandleFunc("GET /trade/list", svc.GetTradeListHandler())
|
||||
//mux.HandleFunc("GET /trade/search", svc.SearchTradeHandler(svc))
|
||||
// Positions
|
||||
mux.HandleFunc("GET /positions/list", svc.GetPositionListHandler())
|
||||
//mux.HandleFunc("GET /positions/closed/list", svc.GetClosedPositionListHandler(svc))
|
||||
//mux.HandleFunc("GET /positions/closed/search", svc.SearchClosedPositionsHandler(svc))
|
||||
// Company
|
||||
mux.HandleFunc("POST /company/add", svc.AddCompanyHandler())
|
||||
mux.HandleFunc("GET /company/list", svc.GetCompaniesHandler())
|
||||
mux.HandleFunc("GET /company/revenue/categories", svc.GetCompanyRevenueCategories())
|
||||
//mux.HandleFunc("POST /company/S-O/add", svc.AddSharesOutstandingHandler(svc))
|
||||
//mux.HandleFunc("GET /company/S-O/list", svc.GetSharesOutstandingHandler(svc))
|
||||
// Currency
|
||||
mux.HandleFunc("GET /currency/list", svc.GetCurrenciesHandler())
|
||||
mux.HandleFunc("POST /currency/add", svc.AddCurrencyHandler())
|
||||
// Revenue
|
||||
mux.HandleFunc("POST /add/revenue/entry", svc.AddRevenueEntryHandler())
|
||||
mux.HandleFunc("POST /api/v1/revenue/add", svc.AddRevenueEntryHandler())
|
||||
}
|
||||
@@ -88,6 +107,15 @@ func main() {
|
||||
log.Fatal("Failed to connect to database:", err)
|
||||
}
|
||||
|
||||
err = godotenv.Load(".env")
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
err = EnvCheck()
|
||||
if err != nil {
|
||||
log.Fatal("Env vars not found:", err)
|
||||
}
|
||||
|
||||
database.InitDB(db)
|
||||
fmt.Println("Connected to SQLite database")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user