mirror of
https://github.com/Macrame-App/Macrame
synced 2025-12-29 07:19:26 +00:00
WIP: panel functions added to the api.
This commit is contained in:
parent
2f391bce4e
commit
022b11c81f
4 changed files with 111 additions and 0 deletions
|
|
@ -85,6 +85,10 @@ func ApiPost(w http.ResponseWriter, r *http.Request) {
|
||||||
RemoveLink("", w, r)
|
RemoveLink("", w, r)
|
||||||
case "/device/handshake":
|
case "/device/handshake":
|
||||||
Handshake(w, r)
|
Handshake(w, r)
|
||||||
|
case "/panel/list":
|
||||||
|
PanelList(w, r)
|
||||||
|
case "/panel/get":
|
||||||
|
GetPanel(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
89
be/app/panel.go
Normal file
89
be/app/panel.go
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"be/app/structs"
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PanelList(w http.ResponseWriter, r *http.Request) {
|
||||||
|
panelDirs, err := os.ReadDir("../panels")
|
||||||
|
if err != nil {
|
||||||
|
json.NewEncoder(w).Encode(false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var panelList []interface{}
|
||||||
|
|
||||||
|
for _, panelDir := range panelDirs {
|
||||||
|
if panelDir.IsDir() {
|
||||||
|
// log.Println(getPanelInfo(panelDir.Name()))
|
||||||
|
panelList = append(panelList, getPanelInfo(panelDir.Name()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(panelList) == 0 {
|
||||||
|
json.NewEncoder(w).Encode(false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
json.NewEncoder(w).Encode(panelList)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPanelInfo(dirname string) structs.PanelInfo {
|
||||||
|
var panelInfo structs.PanelInfo
|
||||||
|
|
||||||
|
jsonFile, err := os.ReadFile("../panels/" + dirname + "/panel.json")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panelInfo.Name = strings.Replace(dirname, "_", " ", -1)
|
||||||
|
panelInfo.Description = "null"
|
||||||
|
panelInfo.AspectRatio = "null"
|
||||||
|
panelInfo.Macros = make(map[string]string)
|
||||||
|
} else {
|
||||||
|
err = json.Unmarshal(jsonFile, &panelInfo)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
thumb := getPanelThumb(dirname)
|
||||||
|
panelInfo.Thumb = thumb
|
||||||
|
|
||||||
|
return panelInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPanelThumb(dirname string) string {
|
||||||
|
re := regexp.MustCompile(`^thumb\.(jpg|jpeg|png)$`)
|
||||||
|
files, _ := os.ReadDir("../panels/" + dirname)
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
if file.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if re.MatchString(filepath.Base(file.Name())) {
|
||||||
|
return filepath.Base(file.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPanel(w http.ResponseWriter, r *http.Request) {
|
||||||
|
html, _ := os.ReadFile("../panels/test_panel/index.html")
|
||||||
|
css, _ := os.ReadFile("../panels/test_panel/output.css")
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Html string `json:"html"`
|
||||||
|
Css string `json:"css"`
|
||||||
|
}
|
||||||
|
|
||||||
|
resp := Response{Html: string(html), Css: string(css)}
|
||||||
|
json.NewEncoder(w).Encode(resp)
|
||||||
|
}
|
||||||
|
|
@ -20,6 +20,8 @@ var Endpoints = Allowed{
|
||||||
"/device/link/poll",
|
"/device/link/poll",
|
||||||
"/device/link/remove",
|
"/device/link/remove",
|
||||||
"/device/handshake",
|
"/device/handshake",
|
||||||
|
"/panel/get",
|
||||||
|
"/panel/list",
|
||||||
},
|
},
|
||||||
Remote: []string{
|
Remote: []string{
|
||||||
"/macro/list",
|
"/macro/list",
|
||||||
|
|
|
||||||
16
be/app/structs/panel-struct.go
Normal file
16
be/app/structs/panel-struct.go
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
package structs
|
||||||
|
|
||||||
|
type PanelJSON struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
AspectRatio string `json:"aspectRatio"`
|
||||||
|
Macros map[string]string `json:"macros"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PanelInfo struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
AspectRatio string `json:"aspectRatio"`
|
||||||
|
Macros map[string]string `json:"macros"`
|
||||||
|
Thumb string `json:"thumb"`
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue