Backend: working REST API.
This commit is contained in:
parent
2156d96ffe
commit
0f1325bb6f
4 changed files with 22 additions and 17 deletions
|
@ -10,11 +10,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type PictureData struct {
|
type PictureData struct {
|
||||||
name string
|
Name string `json:"name"`
|
||||||
date time.Time
|
Date time.Time `json:"date"`
|
||||||
album string
|
Album string `json:"album"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var picd []PictureData
|
||||||
|
|
||||||
func listFiles(path string, actualdir string, picd []PictureData) []PictureData {
|
func listFiles(path string, actualdir string, picd []PictureData) []PictureData {
|
||||||
entries, err := os.ReadDir(path)
|
entries, err := os.ReadDir(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -51,9 +53,9 @@ func isPicture(s string) bool {
|
||||||
func getFileInfo(info fs.FileInfo, picd []PictureData, actualdir string) []PictureData {
|
func getFileInfo(info fs.FileInfo, picd []PictureData, actualdir string) []PictureData {
|
||||||
if isPicture(info.Name()) {
|
if isPicture(info.Name()) {
|
||||||
picd = append(picd, PictureData{
|
picd = append(picd, PictureData{
|
||||||
name: info.Name(),
|
Name: info.Name(),
|
||||||
date: info.ModTime(),
|
Date: info.ModTime(),
|
||||||
album: actualdir,
|
Album: actualdir,
|
||||||
})
|
})
|
||||||
util.Logformatrn(util.INFO, "Reading %d files...", len(picd))
|
util.Logformatrn(util.INFO, "Reading %d files...", len(picd))
|
||||||
}
|
}
|
||||||
|
@ -61,16 +63,19 @@ func getFileInfo(info fs.FileInfo, picd []PictureData, actualdir string) []Pictu
|
||||||
return picd
|
return picd
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListFiles(path string) ([]PictureData, error) {
|
func ListFiles(path string) error {
|
||||||
var picd []PictureData
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
picd = listFiles(path, "", picd)
|
picd = listFiles(path, "", picd)
|
||||||
if len(picd) == 0 {
|
if len(picd) == 0 {
|
||||||
return nil, errors.New("ListFiles(): Could not gather any file")
|
return errors.New("ListFiles(): Could not gather any file")
|
||||||
} else {
|
} else {
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
print("\n")
|
print("\n")
|
||||||
util.Logformat(util.CORRECT, "Gathered %d files in %dms.\n", len(picd), elapsed.Milliseconds())
|
util.Logformat(util.CORRECT, "Gathered %d files in %dms.\n", len(picd), elapsed.Milliseconds())
|
||||||
return picd, nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetPictureData() []PictureData {
|
||||||
|
return picd
|
||||||
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ func main() {
|
||||||
|
|
||||||
// Reading files
|
// Reading files
|
||||||
util.Logformat(util.INFO, "Reading folder '%s'\n", cfg.PhotoPath)
|
util.Logformat(util.INFO, "Reading folder '%s'\n", cfg.PhotoPath)
|
||||||
_, err = core.ListFiles(cfg.PhotoPath)
|
err = core.ListFiles(cfg.PhotoPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.Logformat(util.ERROR, "%s\n", err.Error())
|
util.Logformat(util.ERROR, "%s\n", err.Error())
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"backend/core"
|
||||||
"backend/util"
|
"backend/util"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getphoto(w http.ResponseWriter, r *http.Request) {
|
func getphoto(w http.ResponseWriter, r *http.Request) {
|
||||||
idStr := r.URL.Query().Get("id")
|
if r.Method != "GET" {
|
||||||
if idStr == "" {
|
util.Logformat(util.ERROR, "%s: Only GET is accepted (%s received)", util.GetFunctionName(), r.Method)
|
||||||
util.Logformat(util.ERROR, "%s: Missing 'id' parameter.", util.GetFunctionName())
|
http.Error(w, "Only GET method is accepted", http.StatusBadRequest)
|
||||||
http.Error(w, "Missing or invalid parameters : id", http.StatusBadRequest)
|
|
||||||
} else {
|
} else {
|
||||||
sendResponse(w, 0)
|
sendResponse(w, core.GetPictureData())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
|
|
||||||
func Start(port int64) {
|
func Start(port int64) {
|
||||||
// Creating endpoints
|
// Creating endpoints
|
||||||
http.HandleFunc("/getphoto", wrapHeader(getphoto))
|
http.HandleFunc("/photo", wrapHeader(getphoto))
|
||||||
|
|
||||||
util.Logformat(util.INFO, "Starting server on port %d...\n", port)
|
util.Logformat(util.INFO, "Starting server on port %d...\n", port)
|
||||||
if err := http.ListenAndServe(":"+strconv.FormatInt(port, 10), nil); err != nil {
|
if err := http.ListenAndServe(":"+strconv.FormatInt(port, 10), nil); err != nil {
|
||||||
|
|
Loading…
Add table
Reference in a new issue