Webseite ausgebaut und QR-Codes werden für Ordner erzeugt

This commit is contained in:
Sven.Schmalle 2019-11-20 21:58:42 +01:00
parent 8e42684a57
commit 176dfefa84
4 changed files with 80 additions and 11 deletions

View File

@ -1,2 +1,9 @@
# golangtest
# Go test App
Zum Kompilieren folgende Go-Abhängigkeiten installieren:
- `go get github.com/gorilla/mux`
- `go get github.com/boombuler/barcode`
- `go get github.com/boombuler/barcode/qr`
Kompilieren mit: `go build main.go`

30
main.go
View File

@ -3,22 +3,28 @@ package main
import (
"encoding/json"
"fmt"
"image/png"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
"github.com/gorilla/mux"
)
var StartUpPath string
// our main function
func main() {
StartUpPath = "Fotos" //dir
StartUpPath = "C:/Fotos" //dir
router := mux.NewRouter()
router.HandleFunc("/folder/{foldername:.*}", GetFolderContent).Methods("GET")
router.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir(StartUpPath)))).Methods("GET")
router.HandleFunc("/qr/{qrlink:.*}", GetQRCode).Methods("GET")
router.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir("C:/Fotos")))).Methods("GET")
router.PathPrefix("/").Handler(http.FileServer(http.Dir("web"))).Methods("GET")
log.Fatal(http.ListenAndServe(":8000", router))
}
@ -40,3 +46,23 @@ func GetFolderContent(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(dateien)
}
func GetQRCode(w http.ResponseWriter, r *http.Request) {
// Variablen auswerten
params := mux.Vars(r)
pQRLink := params["qrlink"]
// QR-Code erstellen
qrCode, _ := qr.Encode(pQRLink, qr.M, qr.Auto)
// Größe festlegen
qrCode, _ = barcode.Scale(qrCode, 200, 200)
// Datei erstellen
file, _ := os.Create("qrcode.png")
defer file.Close()
// Datei ausgeben
png.Encode(w, qrCode)
}

0
qrcode.png Normal file
View File

View File

@ -17,10 +17,18 @@
$(document).ready(function() {
ShowHomePage();
});
function ShowHomePage() {
$("#Ordner").empty();
$.ajax({
method: "GET",
contentType:'application/json; charset=utf-8',
url: ServerAPI+'/folder/2019-11-15',
url: ServerAPI+'/folder/',
dataType: "json",
data: "",
success: function(content){
@ -28,15 +36,43 @@
//http://127.0.0.1:8000/img/2019-11-15/IMG_4186.JPG
for ( var i = 0, l = content.length; i < l; i++ ) {
var ObjInhalt = content[i];
$("#Ordner").append("<li>"+
"<img src=\""+ServerAPI+"/img/2019-11-15/"+ObjInhalt+"\" width=100 height=100>"+
"<a href=\"#\" onclick=\"ShowFolder('"+ObjInhalt+"')\">"+
ObjInhalt+"</a></li>");
$("#Ordner").append(""+
"<img src=\""+ServerAPI+"/qr/"+ServerAPI+"/dir/"+ObjInhalt+"\" width=150 height=150>"+
"<a href=\"#\" onclick=\"ShowFolderPics('"+ObjInhalt+"')\">"+
ObjInhalt+"</a><hr>");
}
}
});
}
function ShowFolderPics(folder) {
// Ordner div leeren
$("#Ordner").empty();
$("#Ordner").append('<a href="#" onclick=ShowHomePage()>Zurück</a><hr>');
$.ajax({
method: "GET",
contentType:'application/json; charset=utf-8',
url: ServerAPI+'/folder/'+folder,
dataType: "json",
data: "",
success: function(content){
//http://127.0.0.1:8000/img/2019-11-15/IMG_4186.JPG
for ( var i = 0, l = content.length; i < l; i++ ) {
var ObjInhalt = content[i];
$("#Ordner").append(""+
"<img src=\""+ServerAPI+"/img/"+folder+"/"+ObjInhalt+"\" width=100 height=100>"+
"<a href=\""+ServerAPI+"/img/"+folder+"/"+ObjInhalt+"\">"+
ObjInhalt+"</a><hr>");
}
}
});
}
</script>
</body>