Skip to content

Commit

Permalink
QR Code page
Browse files Browse the repository at this point in the history
  • Loading branch information
grellyd committed Aug 29, 2023
1 parent aef2ad6 commit 745fc45
Show file tree
Hide file tree
Showing 31 changed files with 2,755 additions and 40 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ define deploy-dev
cp -rf ./public /var/http && \
go build grellyddotcom.go && \
cp grellyddotcom /var/http/ && \
cp ./pages/templates /var/http/ && \
cp -r ./pages/templates /var/http/ && \
ls /var/http/ && \
go version && \
systemctl restart grellyddotcom.service && \
systemctl status grellyddotcom.service
endef
Expand Down
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
module github.com/grellyd/grellyddotcom

go 1.14
go 1.19

require (
github.com/google/uuid v1.3.1
github.com/grellyd/filelogging v0.0.0-20180915010018-b2a208a9a587
github.com/grellyd/qrgen v0.0.0-20201216011122-e729d155aeef
github.com/pkg/errors v0.9.1
golang.org/x/crypto v0.0.0-20201124201722-c8d3bf9c5392
)

require (
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 // indirect
golang.org/x/text v0.3.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grellyd/filelogging v0.0.0-20180915010018-b2a208a9a587 h1:cr5+7q79KwmiI1xut+glbx3CkTBzllmepAc5ig55dkY=
github.com/grellyd/filelogging v0.0.0-20180915010018-b2a208a9a587/go.mod h1:UBvU1eltqysKHO7Qk2pReOgIzvTJfVaPR6WikncY34w=
github.com/grellyd/qrgen v0.0.0-20201216011122-e729d155aeef h1:q/0wMnBHSCzH/e446gUALXjX0jOuE8D7gSV+5tD4HMI=
github.com/grellyd/qrgen v0.0.0-20201216011122-e729d155aeef/go.mod h1:cIONnJbrw5tRvH85Wsqu447VDm0lP/QFoVQiD0kIEnM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
72 changes: 36 additions & 36 deletions grellyddotcom.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,42 +123,42 @@ func registerRoutes() (*router.Router, error) {
if err != nil {
return nil, fmt.Errorf("unable to register qrgen: %w", err)
}
// err = r.Register("/", "(^/$)|(^/(status|about|quote|xmas)$)", handlers.File)
// if err != nil {
// return nil, fmt.Errorf("unable to register static: %w", err)
// }
// err = r.Register("/writing/", "^/writing/([a-zA-Z0-9]*)$", handlers.File)
// if err != nil {
// return nil, fmt.Errorf("unable to register writing: %w", err)
// }
// err = r.Register("/css/", "^/css/([a-zA-Z0-9_]*).css$", handlers.File)
// if err != nil {
// return nil, fmt.Errorf("unable to register css: %w", err)
// }
// err = r.Register("/js/", "^/js/([a-zA-Z0-9_]*).js$", handlers.File)
// if err != nil {
// return nil, fmt.Errorf("unable to register js: %w", err)
// }
// err = r.Register("/images/", "^/images/(([a-zA-Z0-9_]*)/)*.(jpg|png)$", handlers.File)
// if err != nil {
// return nil, fmt.Errorf("unable to register images: %w", err)
// }
// err = r.Register("/videos/", "^/videos/(([a-zA-Z0-9_]*)/)*.mp4$", handlers.File)
// if err != nil {
// return nil, fmt.Errorf("unable to register videos: %w", err)
// }
// err = r.Register("/files/", "^/files/(([a-zA-Z0-9_]*)/)*.jpg$", handlers.File)
// if err != nil {
// return nil, fmt.Errorf("unable to register files: %w", err)
// }
// err = r.Register("/xmas/", "^/xmas/([a-zA-Z0-9_]*).html$", handlers.File)
// if err != nil {
// return nil, fmt.Errorf("unable to register xmas: %w", err)
// }
// err = r.Register("/email/", "^/email/([a-zA-Z0-9_]*).html$", handlers.File)
// if err != nil {
// return nil, fmt.Errorf("unable to register email: %w", err)
// }
err = r.Register("/", "(^/$)|(^/(status|about|quote|xmas)$)", handlers.File)
if err != nil {
return nil, fmt.Errorf("unable to register static: %w", err)
}
err = r.Register("/writing/", "^/writing/([a-zA-Z0-9]*)$", handlers.File)
if err != nil {
return nil, fmt.Errorf("unable to register writing: %w", err)
}
err = r.Register("/css/", "^/css/([a-zA-Z0-9_]*).css$", handlers.File)
if err != nil {
return nil, fmt.Errorf("unable to register css: %w", err)
}
err = r.Register("/js/", "^/js/([a-zA-Z0-9_]*).js$", handlers.File)
if err != nil {
return nil, fmt.Errorf("unable to register js: %w", err)
}
err = r.Register("/images/", "^/images/(([a-zA-Z0-9_]*)/)*.(jpg|png)$", handlers.File)
if err != nil {
return nil, fmt.Errorf("unable to register images: %w", err)
}
err = r.Register("/videos/", "^/videos/(([a-zA-Z0-9_]*)/)*.mp4$", handlers.File)
if err != nil {
return nil, fmt.Errorf("unable to register videos: %w", err)
}
err = r.Register("/files/", "^/files/(([a-zA-Z0-9_]*)/)*.jpg$", handlers.File)
if err != nil {
return nil, fmt.Errorf("unable to register files: %w", err)
}
err = r.Register("/xmas/", "^/xmas/([a-zA-Z0-9_]*).html$", handlers.File)
if err != nil {
return nil, fmt.Errorf("unable to register xmas: %w", err)
}
err = r.Register("/email/", "^/email/([a-zA-Z0-9_]*).html$", handlers.File)
if err != nil {
return nil, fmt.Errorf("unable to register email: %w", err)
}
err = r.Register("/certs/", "^/certs/([a-zA-Z0-9_]*).html$", handlers.File)
if err != nil {
return nil, fmt.Errorf("unable to register certs: %w", err)
Expand Down
82 changes: 81 additions & 1 deletion handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@ package handlers
import (
"fmt"
"html/template"
"image"
"image/png"
"net/http"
"os"
"strings"

"github.com/google/uuid"

"github.com/grellyd/filelogging/globallogger"
"github.com/grellyd/grellyddotcom/pages"
"github.com/grellyd/qrgen/qrlib"
)

func QRGen(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, fmt.Sprintln("Wrong method"), http.StatusBadRequest)
return
}
fmt.Printf("r.RequestURI: %v\n", r.RequestURI)

path := "./templates/qrgen.tmpl"
Expand All @@ -21,22 +30,93 @@ func QRGen(w http.ResponseWriter, r *http.Request) {
b, err := os.ReadFile(path)
if err != nil {
http.Error(w, fmt.Sprintf("failed to readfile %s: %s\n", path, err.Error()), http.StatusInternalServerError)
return
}

t, err := template.New("qrgen").Parse(string(b))
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse %s: %s\n", string(b), err.Error()), http.StatusInternalServerError)
return
}

err = t.Execute(w, nil)
if err != nil {
http.Error(w, fmt.Sprintf("failed to execute '%s' %s\n", string(b), err.Error()), http.StatusInternalServerError)
return
}

}

func generateQRCode(link string, scaleFactor int) (*image.Gray, error) {
qr, err := qrlib.Generate(link, qrlib.ECLH)
if err != nil {
return nil, fmt.Errorf("unable to generate: %s", err.Error())
}

i, err := qrlib.BuildImage(qr, scaleFactor)
if err != nil {
return nil, fmt.Errorf("unable to build an output image: %s", err.Error())
}

return i, nil
}

func QRCode(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, fmt.Sprintln("Wrong method"), http.StatusBadRequest)
return
}
globallogger.Info("in qrcode")
fmt.Printf("r.RequestURI: %v\n", r.RequestURI)

l := r.PostFormValue("link")
globallogger.Info(fmt.Sprintf("link: %s\n", l))

link := "https://docs.google.com/forms/d/1UX81h0a9k1NaFY6Q09jgO4ns1iNwEOSnkw1c33YBON4/edit"
scaleFactor := 50

i, err := generateQRCode(link, scaleFactor)
if err != nil {
http.Error(w, fmt.Sprintf("failed to generateQRCode for %s: %s\n", link, err.Error()), http.StatusInternalServerError)
return
}

uuid := uuid.NewString()
path := fmt.Sprintf("/images/qrcodes/%s.png", uuid)

f, err := os.Create(fmt.Sprintf("/var/http/public/%s", path))
defer f.Close()
if err = png.Encode(f, i); err != nil {
http.Error(w, fmt.Sprintf("failed to encode png: %s\n", err.Error()), http.StatusInternalServerError)
return
}

b, err := os.ReadFile("./templates/qrcode.tmpl")
if err != nil {
http.Error(w, fmt.Sprintf("failed to readfile %s: %s\n", path, err.Error()), http.StatusInternalServerError)
return
}

t, err := template.New("qrgen").Parse(string(b))
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse %s: %s\n", string(b), err.Error()), http.StatusInternalServerError)
return
}

err = t.Execute(w, map[string]any{"image": path})
if err != nil {
http.Error(w, fmt.Sprintf("failed to execute '%s' %s\n", string(b), err.Error()), http.StatusInternalServerError)
return
}

}

// File handler for any file
// File handler for reading any file
func File(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, fmt.Sprintln("Wrong method"), http.StatusBadRequest)
return
}
globallogger.Debug(fmt.Sprintf("Handling File\n"))
sections, title, pending, err := decomposeURL(r.URL.Path)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pages/templates/qrcode.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="single">
<h1><span class="single-title">QR Code Generator</span></h1>

Your code:

<img src="{{ .imageSource }}">
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions vendor/github.com/google/uuid/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/google/uuid/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/google/uuid/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 745fc45

Please sign in to comment.