golangtest/main.go

69 lines
1.5 KiB
Go

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 = "C:/Fotos" //dir
router := mux.NewRouter()
router.HandleFunc("/folder/{foldername:.*}", GetFolderContent).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))
}
func GetFolderContent(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
pFolder := params["foldername"]
fmt.Println(path.Join(StartUpPath, pFolder))
files, err := ioutil.ReadDir(path.Join(StartUpPath, pFolder))
if err != nil {
log.Fatal(err)
}
var dateien []string
for _, f := range files {
dateien = append(dateien, f.Name())
}
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)
}