Repository navigation
Expand file tree
/
Copy pathcertificates.go
More file actions
58 lines (49 loc) · 1.52 KB
/
Copy pathcertificates.go
File metadata and controls
58 lines (49 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
oicana "go.oicana.com/oicana"
)
type certificateHandlers struct {
templates *templateRegistry
}
func (h certificateHandlers) register(mux *http.ServeMux) {
mux.HandleFunc("POST /certificates", h.create)
}
type createCertificate struct {
// Name to create the certificate for
Name string `json:"name" example:"Jane Doe"`
} //@name CreateCertificate
// create godoc
//
// @Description Create a certificate
// @Tags certificates
// @Accept json
// @Produce application/pdf
// @Param payload body createCertificate true "Certificate details"
// @Success 200 {file} binary "The compiled PDF certificate"
// @Failure 400 {object} errorResponse
// @Router /certificates [post]
func (h certificateHandlers) create(w http.ResponseWriter, r *http.Request) {
template, ok := h.templates.get("certificate")
if !ok {
slog.Error("Certificate template not found!")
writeError(w, http.StatusInternalServerError, "Certificate template not found!")
return
}
var request createCertificate
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
writeError(w, http.StatusBadRequest, fmt.Sprintf("Invalid request body: %v", err))
return
}
pdf, err := template.ExportPdf(oicana.Inputs{JSON: map[string]any{"certificate": request}})
if err != nil {
writeOicanaError(w, "certificate", err)
return
}
w.Header().Set("Content-Type", "application/pdf")
w.Header().Set("Content-Disposition", `attachment; filename="certificate.pdf"`)
w.Write(pdf)
}