This is the piece that turns a Pre-Publish-Prüfung into an actual
archived, provable record instead of a one-off form response.
extract.Client.Extract now returns Result{Facts, RawJSON} instead of
just Facts — RawJSON is the model's exact, unmodified JSON, which is
what belongs in extraction.payload (the audit trail), not a re-encoded
view through our own Facts struct. extract.ParsePayload reconstructs
Facts from a stored payload later, reusing the same parsing/validation
path Extract uses (including the enum guard), so a previously-saved
extraction can be read back exactly as it would have been the first
time.
internal/dossier.BuildContent no longer requires AssetHash: most checks
right now are caption-only (no image/video upload wired yet), and
inventing a placeholder hash for a nonexistent asset would itself be an
integrity problem in an evidence tool. Content shows "kein Asset
hinterlegt" instead.
internal/web gains a narrow Store interface (mirroring the Extractor
pattern — only the methods these handlers use, not the full
*store.Store) so its test suite stays network/DB-free via an in-memory
fake:
- POST /pruefen persists submission + extraction + findings and marks
the submission "checked". A needsClarification result persists the
extraction (there's something worth keeping) but no findings and no
status change, and the template omits the archive option entirely.
- POST /veroeffentlichen re-derives Facts from the stored payload, hashes
the canonical submission+facts+findings metadata, gets an RFC-3161
timestamp, generates the PDF dossier to disk, and persists the
evidence_package row before marking the submission "published".
- GET /dossier/{id} serves the generated PDF.
Tested end-to-end offline: a fake Timestamper builds a real, structurally
valid self-signed RFC-3161 response so the full check→archive→download
flow runs against an in-memory store, verifying the downloaded bytes are
an actual PDF and the dossier file lands on disk — without hitting a
real database, TSA, or the Claude API.
cmd/deklarix/main.go now wires store.Store, evidence.NewHTTPTimestamper
(TSA_URL, default FreeTSA), and DOSSIER_DIR (default "dossiers") into
web.NewServer alongside the extractor and rule set.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
257 lines
8.3 KiB
Go
257 lines
8.3 KiB
Go
package web
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/netcell-it/deklarix/internal/dossier"
|
|
"github.com/netcell-it/deklarix/internal/evidence"
|
|
"github.com/netcell-it/deklarix/internal/extract"
|
|
"github.com/netcell-it/deklarix/internal/rules"
|
|
)
|
|
|
|
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
fmt.Fprint(w, `{"ok":true}`)
|
|
}
|
|
|
|
type indexData struct {
|
|
Title string
|
|
}
|
|
|
|
func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) {
|
|
if err := s.templates.ExecuteTemplate(w, "layout", indexData{Title: "Pre-Publish-Prüfung"}); err != nil {
|
|
http.Error(w, "Seite konnte nicht gerendert werden", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
type findingView struct {
|
|
RuleID string
|
|
Version int
|
|
Severity string
|
|
Title string
|
|
Fix string
|
|
Sources []string
|
|
}
|
|
|
|
type resultData struct {
|
|
SubmissionID string
|
|
NeedsClarification bool
|
|
CanArchive bool
|
|
Findings []findingView
|
|
}
|
|
|
|
// handleCheck führt die Pre-Publish-Prüfung aus: Extraktion (Stufe 1),
|
|
// Regelauswertung (Stufe 2), und speichert Submission + Extraction +
|
|
// Findings. Die eigentliche Archivierung (Hash, Zeitstempel, Dossier)
|
|
// passiert erst in handleArchive, wenn der Beitrag tatsächlich
|
|
// veröffentlicht wurde — das sind unterschiedliche Zeitpunkte im
|
|
// Lebenszyklus (siehe CLAUDE.md, Funktion 1 vs. 2).
|
|
func (s *Server) handleCheck(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, "ungültiges Formular", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
platform := r.FormValue("platform")
|
|
postType := r.FormValue("post_type")
|
|
caption := r.FormValue("caption")
|
|
if platform == "" || postType == "" || caption == "" {
|
|
http.Error(w, "Plattform, Beitragstyp und Caption sind Pflichtfelder", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
ctx := r.Context()
|
|
|
|
result, err := s.extractor.Extract(ctx, extract.Input{
|
|
Platform: platform,
|
|
Jurisdiction: "DE",
|
|
Caption: caption,
|
|
})
|
|
if err != nil {
|
|
http.Error(w, "Extraktion fehlgeschlagen: "+err.Error(), http.StatusBadGateway)
|
|
return
|
|
}
|
|
|
|
sub, err := s.store.CreateSubmission(ctx, platform, postType, caption)
|
|
if err != nil {
|
|
http.Error(w, "Beitrag konnte nicht gespeichert werden: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
ext, err := s.store.CreateExtraction(ctx, sub.ID, result.RawJSON, s.extractor.ModelVersion(), extract.PromptVersion)
|
|
if err != nil {
|
|
http.Error(w, "Extraktion konnte nicht gespeichert werden: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
findings, needsClarification := rules.Evaluate(s.ruleSet, result.Facts)
|
|
|
|
data := resultData{SubmissionID: sub.ID, NeedsClarification: needsClarification}
|
|
if !needsClarification {
|
|
extractionID := ext.ID
|
|
for _, f := range findings {
|
|
if _, err := s.store.CreateFinding(ctx, sub.ID, &extractionID, f.RuleID, f.RuleVersion, string(f.Severity), f.Title, f.Fix, f.Sources); err != nil {
|
|
http.Error(w, "Finding konnte nicht gespeichert werden: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data.Findings = append(data.Findings, findingView{
|
|
RuleID: f.RuleID, Version: f.RuleVersion, Severity: string(f.Severity),
|
|
Title: f.Title, Fix: f.Fix, Sources: f.Sources,
|
|
})
|
|
}
|
|
if err := s.store.SetSubmissionStatus(ctx, sub.ID, "checked"); err != nil {
|
|
http.Error(w, "Status konnte nicht aktualisiert werden: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data.CanArchive = true
|
|
}
|
|
|
|
if err := s.templates.ExecuteTemplate(w, "result", data); err != nil {
|
|
http.Error(w, "Ergebnis konnte nicht gerendert werden", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
type archivedData struct {
|
|
SubmissionID string
|
|
DossierURL string
|
|
TimestampedAt string
|
|
}
|
|
|
|
// handleArchive markiert einen geprüften Beitrag als veröffentlicht und
|
|
// archiviert ihn: Hash der Metadaten, RFC-3161-Zeitstempel, PDF-Dossier.
|
|
// Setzt eine vorherige Prüfung voraus (Submission + Extraction +
|
|
// Findings müssen bereits existieren).
|
|
func (s *Server) handleArchive(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, "ungültiges Formular", http.StatusBadRequest)
|
|
return
|
|
}
|
|
submissionID := r.FormValue("submission_id")
|
|
if submissionID == "" {
|
|
http.Error(w, "submission_id ist Pflicht", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
ctx := r.Context()
|
|
|
|
sub, err := s.store.GetSubmission(ctx, submissionID)
|
|
if err != nil {
|
|
http.Error(w, "Beitrag nicht gefunden: "+err.Error(), http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
ext, err := s.store.GetLatestExtraction(ctx, submissionID)
|
|
if err != nil {
|
|
http.Error(w, "Extraktion nicht gefunden: "+err.Error(), http.StatusNotFound)
|
|
return
|
|
}
|
|
facts, err := extract.ParsePayload(ext.Payload, sub.Platform, "DE")
|
|
if err != nil {
|
|
http.Error(w, "gespeicherte Extraktion konnte nicht gelesen werden: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
storeFindings, err := s.store.ListCurrentFindings(ctx, submissionID)
|
|
if err != nil {
|
|
http.Error(w, "Findings konnten nicht gelesen werden: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
dossierFindings := make([]rules.Finding, len(storeFindings))
|
|
for i, f := range storeFindings {
|
|
dossierFindings[i] = rules.Finding{
|
|
RuleID: f.RuleID, RuleVersion: f.RuleVersion, Severity: rules.Severity(f.Severity),
|
|
Title: f.Title, Fix: f.Fix, Sources: f.Sources,
|
|
}
|
|
}
|
|
|
|
metadataHash, err := evidence.HashMetadata(struct {
|
|
SubmissionID string
|
|
Platform string
|
|
PostType string
|
|
Caption string
|
|
Facts rules.Facts
|
|
Findings []rules.Finding
|
|
}{sub.ID, sub.Platform, sub.PostType, sub.Caption, facts, dossierFindings})
|
|
if err != nil {
|
|
http.Error(w, "Metadaten-Hash fehlgeschlagen: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
timestampToken, err := s.timestamper.Timestamp(ctx, metadataHash)
|
|
if err != nil {
|
|
http.Error(w, "Zeitstempel fehlgeschlagen: "+err.Error(), http.StatusBadGateway)
|
|
return
|
|
}
|
|
|
|
if err := os.MkdirAll(s.dossierDir, 0o750); err != nil {
|
|
http.Error(w, "Dossier-Verzeichnis konnte nicht angelegt werden: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
dossierPath := filepath.Join(s.dossierDir, submissionID+".pdf")
|
|
dossierFile, err := os.Create(dossierPath)
|
|
if err != nil {
|
|
http.Error(w, "Dossier-Datei konnte nicht angelegt werden: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer dossierFile.Close()
|
|
|
|
genErr := dossier.Generate(dossierFile, dossier.Data{
|
|
Submission: dossier.Submission{
|
|
Platform: sub.Platform, PostType: sub.PostType, Caption: sub.Caption, CreatedAt: sub.CreatedAt,
|
|
},
|
|
Facts: facts,
|
|
Findings: dossierFindings,
|
|
MetadataHash: metadataHash,
|
|
TimestampToken: timestampToken,
|
|
GeneratedAt: time.Now(),
|
|
})
|
|
if genErr != nil {
|
|
http.Error(w, "Dossier konnte nicht erzeugt werden: "+genErr.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if _, err := s.store.CreateEvidencePackage(ctx, submissionID, dossierPath, hex.EncodeToString(metadataHash), timestampToken); err != nil {
|
|
http.Error(w, "Evidence-Package konnte nicht gespeichert werden: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if err := s.store.SetSubmissionStatus(ctx, submissionID, "published"); err != nil {
|
|
http.Error(w, "Status konnte nicht aktualisiert werden: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
timestampedAt, err := evidence.TimestampTime(timestampToken)
|
|
if err != nil {
|
|
http.Error(w, "Zeitstempel konnte nicht gelesen werden: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
data := archivedData{
|
|
SubmissionID: submissionID,
|
|
DossierURL: "/dossier/" + submissionID,
|
|
TimestampedAt: timestampedAt.Format("02.01.2006 15:04:05 MST"),
|
|
}
|
|
if err := s.templates.ExecuteTemplate(w, "archived", data); err != nil {
|
|
http.Error(w, "Ergebnis konnte nicht gerendert werden", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
// handleDossierDownload liefert das erzeugte PDF-Dossier eines
|
|
// archivierten Beitrags aus.
|
|
func (s *Server) handleDossierDownload(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
|
|
pkg, err := s.store.GetLatestEvidencePackage(r.Context(), id)
|
|
if err != nil {
|
|
http.Error(w, "Kein Dossier für diesen Beitrag gefunden", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/pdf")
|
|
http.ServeFile(w, r, pkg.DossierPath)
|
|
}
|