primeira versão do e-li-nps construido com IA
This commit is contained in:
commit
06950d6e2c
34 changed files with 2524 additions and 0 deletions
51
internal/elinps/painel_handlers.go
Normal file
51
internal/elinps/painel_handlers.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue