REST-API Test mit Jquery Client

This commit is contained in:
Sven.Schmalle 2019-11-18 23:10:50 +01:00
parent 9717d75e88
commit 8e42684a57
4 changed files with 96 additions and 0 deletions

5
.gitignore vendored
View File

@ -28,3 +28,8 @@ _testmain.go
*.test
*.prof
.vscode/*
.history
Fotos/*
debug

42
main.go Normal file
View File

@ -0,0 +1,42 @@
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)
}

43
web/index.html Normal file
View File

@ -0,0 +1,43 @@
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
</head>
<body>
<div id="Ordner"></div>
<script src="/misc/jquery.min.js"></script>
<script type="text/javascript">
var ServerAPI="http://127.0.0.1:8000";
$(document).ready(function() {
$.ajax({
method: "GET",
contentType:'application/json; charset=utf-8',
url: ServerAPI+'/folder/2019-11-15',
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("<li>"+
"<img src=\""+ServerAPI+"/img/2019-11-15/"+ObjInhalt+"\" width=100 height=100>"+
"<a href=\"#\" onclick=\"ShowFolder('"+ObjInhalt+"')\">"+
ObjInhalt+"</a></li>");
}
}
});
});
</script>
</body>
</html>

6
web/misc/jquery.min.js vendored Normal file

File diff suppressed because one or more lines are too long