golangtest/main.go

43 lines
981 B
Go
Raw Normal View History

2019-11-18 23:10:50 +01:00
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"path"
"github.com/gorilla/mux"
)
var StartUpPath string
func main() {
StartUpPath = "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.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)
}