Move qr-code generation to the API
This commit is contained in:
parent
7be8b428e4
commit
10dc5c0bd7
7 changed files with 107 additions and 95 deletions
|
|
@ -47,6 +47,11 @@ func AddRoutes(r *mux.Router) {
|
|||
r.Handle("/mailer/{id}", authenticatedHandlerJSONPost(mailer.ByIDGet)).Methods("GET").Name("mailer.ByIDGet")
|
||||
r.Handle("/mosquito-source", auth.NewEnsureAuth(apiMosquitoSource)).Methods("GET")
|
||||
|
||||
qrcode := resource.QRCode(router)
|
||||
r.Handle("/qr-code/mailer/{code}", handlerBasic(qrcode.Mailer)).Methods("GET")
|
||||
r.Handle("/qr-code/marketing", handlerBasic(qrcode.Marketing)).Methods("GET")
|
||||
r.Handle("/qr-code/report/{code}", handlerBasic(qrcode.Report)).Methods("GET")
|
||||
|
||||
r.Handle("/publicreport/invalid", authenticatedHandlerJSONPost(postPublicreportInvalid)).Methods("POST")
|
||||
r.Handle("/publicreport/signal", authenticatedHandlerJSONPost(postPublicreportSignal)).Methods("POST")
|
||||
r.Handle("/publicreport/message", authenticatedHandlerJSONPost(postPublicreportMessage)).Methods("POST")
|
||||
|
|
|
|||
|
|
@ -163,7 +163,7 @@
|
|||
<h5>IMPORTANT NOTICE</h5>
|
||||
<p>We visited regarding a potential mosquito breeding site.</p>
|
||||
|
||||
<img src="/qr-code/report/t78fd3" width="256" height="256" />
|
||||
<img src="/api/qr-code/report/t78fd3" width="256" height="256" />
|
||||
|
||||
<p>
|
||||
<strong>Scan this code</strong> with your phone camera to report
|
||||
|
|
|
|||
97
resource/qrcode.go
Normal file
97
resource/qrcode.go
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/skip2/go-qrcode"
|
||||
)
|
||||
|
||||
type qrcodeR struct {
|
||||
router *router
|
||||
}
|
||||
|
||||
func QRCode(r *router) *qrcodeR {
|
||||
return &qrcodeR{
|
||||
router: r,
|
||||
}
|
||||
}
|
||||
|
||||
func (res *qrcodeR) Mailer(ctx context.Context, w http.ResponseWriter, r *http.Request) *nhttp.ErrorWithStatus {
|
||||
vars := mux.Vars(r)
|
||||
code := vars["code"]
|
||||
if code == "" {
|
||||
return nhttp.NewBadRequest("There should always be a id")
|
||||
}
|
||||
content := config.MakeURLReport("/mailer/%s", code)
|
||||
return writeQRCode(w, r, content)
|
||||
}
|
||||
func (res *qrcodeR) Marketing(w http.ResponseWriter, r *http.Request) *nhttp.ErrorWithStatus {
|
||||
content := "https://nidus.cloud"
|
||||
return writeQRCode(w, r, content)
|
||||
}
|
||||
|
||||
func (res *qrcodeR) Report(w http.ResponseWriter, r *http.Request) *nhttp.ErrorWithStatus {
|
||||
vars := mux.Vars(r)
|
||||
code := vars["code"]
|
||||
if code == "" {
|
||||
return nhttp.NewBadRequest("There should always be a code")
|
||||
}
|
||||
content := config.MakeURLNidus("/report/%s", code)
|
||||
return writeQRCode(w, r, content)
|
||||
}
|
||||
func writeQRCode(w http.ResponseWriter, r *http.Request, content string) *nhttp.ErrorWithStatus {
|
||||
// Get optional size parameter (default to 256)
|
||||
size := 256
|
||||
if sizeStr := r.URL.Query().Get("size"); sizeStr != "" {
|
||||
var err error
|
||||
size, err = strconv.Atoi(sizeStr)
|
||||
if err != nil {
|
||||
return nhttp.NewBadRequest("Invalid 'size' parameter, must be an integer")
|
||||
}
|
||||
}
|
||||
|
||||
// Get optional error correction level (default to Medium)
|
||||
level := qrcode.Medium
|
||||
if levelStr := r.URL.Query().Get("level"); levelStr != "" {
|
||||
switch levelStr {
|
||||
case "L", "l":
|
||||
level = qrcode.Low
|
||||
case "M", "m":
|
||||
level = qrcode.Medium
|
||||
case "Q", "q":
|
||||
level = qrcode.High
|
||||
case "H", "h":
|
||||
level = qrcode.Highest
|
||||
default:
|
||||
return nhttp.NewBadRequest("Invalid 'level' parameter, must be L, M, Q, or H")
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the QR code
|
||||
var qr *qrcode.QRCode
|
||||
var err error
|
||||
qr, err = qrcode.New(content, level)
|
||||
if err != nil {
|
||||
return nhttp.NewError("Error generating QR code: %w", err)
|
||||
}
|
||||
|
||||
// Set the appropriate content type
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
|
||||
// Generate PNG and write directly to the response writer
|
||||
png, err := qr.PNG(size)
|
||||
if err != nil {
|
||||
return nhttp.NewError("Error encoding QR code to PNG: %w", err)
|
||||
}
|
||||
|
||||
_, err = w.Write(png)
|
||||
if err != nil {
|
||||
return nhttp.NewError("Error writing response: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -98,7 +98,7 @@ func getMailer2Preview(w http.ResponseWriter, r *http.Request) {
|
|||
LogoURL: config.MakeURLNidus("/api/district/delta-mvcd/logo"),
|
||||
Organization: org,
|
||||
PoolImageURL: config.MakeURLNidus("/mailer/pool/random"),
|
||||
QRCodeURL: config.MakeURLNidus("/qr-code/marketing"),
|
||||
QRCodeURL: config.MakeURLNidus("/api/qr-code/marketing"),
|
||||
ReportURL: "https://nidus.cloud",
|
||||
})
|
||||
}
|
||||
|
|
@ -133,7 +133,7 @@ func getMailer3Preview(w http.ResponseWriter, r *http.Request) {
|
|||
LogoURL: config.MakeURLNidus("/api/district/%s/logo", org.Slug.GetOr("unset")),
|
||||
Organization: org,
|
||||
PoolImageURL: config.MakeURLNidus("/api/compliance-request/image/pool/%s", code),
|
||||
QRCodeURL: config.MakeURLNidus("/qr-code/mailer/%s", code),
|
||||
QRCodeURL: config.MakeURLNidus("/api/qr-code/mailer/%s", code),
|
||||
ReportURL: config.MakeURLReport("/mailer/%s", code),
|
||||
})
|
||||
}
|
||||
|
|
|
|||
87
sync/qr.go
87
sync/qr.go
|
|
@ -1,88 +1 @@
|
|||
package sync
|
||||
|
||||
import (
|
||||
"github.com/skip2/go-qrcode"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/Gleipnir-Technology/nidus-sync/config"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func getQRCodeMailer(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
code := vars["code"]
|
||||
if code == "" {
|
||||
respondError(w, "There should always be a id", nil, http.StatusBadRequest)
|
||||
}
|
||||
content := config.MakeURLReport("/mailer/%s", code)
|
||||
writeQRCode(w, r, content)
|
||||
}
|
||||
func getQRCodeMarketing(w http.ResponseWriter, r *http.Request) {
|
||||
content := "https://nidus.cloud"
|
||||
writeQRCode(w, r, content)
|
||||
}
|
||||
|
||||
func getQRCodeReport(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
code := vars["code"]
|
||||
if code == "" {
|
||||
respondError(w, "There should always be a code", nil, http.StatusBadRequest)
|
||||
}
|
||||
content := config.MakeURLNidus("/report/%s", code)
|
||||
writeQRCode(w, r, content)
|
||||
}
|
||||
func writeQRCode(w http.ResponseWriter, r *http.Request, content string) {
|
||||
// Get optional size parameter (default to 256)
|
||||
size := 256
|
||||
if sizeStr := r.URL.Query().Get("size"); sizeStr != "" {
|
||||
var err error
|
||||
size, err = strconv.Atoi(sizeStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid 'size' parameter, must be an integer", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Get optional error correction level (default to Medium)
|
||||
level := qrcode.Medium
|
||||
if levelStr := r.URL.Query().Get("level"); levelStr != "" {
|
||||
switch levelStr {
|
||||
case "L", "l":
|
||||
level = qrcode.Low
|
||||
case "M", "m":
|
||||
level = qrcode.Medium
|
||||
case "Q", "q":
|
||||
level = qrcode.High
|
||||
case "H", "h":
|
||||
level = qrcode.Highest
|
||||
default:
|
||||
respondError(w, "Invalid 'level' parameter, must be L, M, Q, or H", nil, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the QR code
|
||||
var qr *qrcode.QRCode
|
||||
var err error
|
||||
qr, err = qrcode.New(content, level)
|
||||
if err != nil {
|
||||
respondError(w, "Error generating QR code", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Set the appropriate content type
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
|
||||
// Generate PNG and write directly to the response writer
|
||||
png, err := qr.PNG(size)
|
||||
if err != nil {
|
||||
respondError(w, "Error encoding QR code to PNG", err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = w.Write(png)
|
||||
if err != nil {
|
||||
respondError(w, "Error writing response", err, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,9 +19,6 @@ func Router(r *mux.Router) {
|
|||
|
||||
// Utility endpoints
|
||||
r.HandleFunc("/privacy", getPrivacy)
|
||||
r.HandleFunc("/qr-code/marketing", getQRCodeMarketing)
|
||||
r.HandleFunc("/qr-code/report/{code}", getQRCodeReport)
|
||||
r.HandleFunc("/qr-code/mailer/{code}", getQRCodeMailer)
|
||||
r.HandleFunc("/template-test", getTemplateTest)
|
||||
|
||||
//r.HandleFunc("/", getRoot)
|
||||
|
|
|
|||
|
|
@ -77,11 +77,11 @@ export default defineConfig({
|
|||
target: "http://127.0.0.1:9003",
|
||||
changeOrigin: false,
|
||||
},
|
||||
"/oauth": {
|
||||
"/mock": {
|
||||
target: "http://127.0.0.1:9003",
|
||||
changeOrigin: false,
|
||||
},
|
||||
"/qr-code": {
|
||||
"/oauth": {
|
||||
target: "http://127.0.0.1:9003",
|
||||
changeOrigin: false,
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue