e-li-nps/internal/elinps/painel_handlers.go

51 lines
1.2 KiB
Go

package elinps
import (
"crypto/rand"
"encoding/hex"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
// PainelHandlers expõe o painel de exploração em /painel.
//
// O painel é protegido por senha via SENHA_PAINEL.
// A sessão é um cookie simples com token gerado a cada inicialização.
type PainelHandlers struct {
auth AuthPainel
store *Store
}
func NewPainelHandlers(pool *pgxpool.Pool, senha string) *PainelHandlers {
token := gerarTokenPainel()
return &PainelHandlers{
auth: AuthPainel{Senha: senha, Token: token},
store: NewStore(pool),
}
}
// Router monta as rotas do painel.
func (p *PainelHandlers) Router() http.Handler {
r := chi.NewRouter()
// Login
r.Get("/login", p.auth.handlerLoginGet)
r.Post("/login", p.auth.handlerLoginPost)
// Dashboard
r.With(func(next http.Handler) http.Handler { return p.auth.middleware(next) }).Get("/", func(w http.ResponseWriter, r *http.Request) {
p.auth.handlerPainel(w, r, p.store)
})
return r
}
func gerarTokenPainel() string {
// Token aleatório para o cookie do painel.
// Importante: muda a cada boot (ao reiniciar o servidor, precisa logar de novo).
b := make([]byte, 32)
_, _ = rand.Read(b)
return hex.EncodeToString(b)
}