mirror of
https://github.com/Macrame-App/Macrame
synced 2025-12-29 07:19:26 +00:00
Basic panel functions added to backend
This commit is contained in:
parent
8ba7689c39
commit
6767f77fcf
3 changed files with 85 additions and 20 deletions
|
|
@ -15,7 +15,6 @@ func ApiCORS(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173")
|
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173")
|
||||||
|
|
||||||
if strings.HasPrefix(r.Host, "192.168.") {
|
if strings.HasPrefix(r.Host, "192.168.") {
|
||||||
log.Println("lan device")
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,7 +46,6 @@ func ApiGet(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApiPost(w http.ResponseWriter, r *http.Request) {
|
func ApiPost(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
access, data := helper.EndpointAccess(w, r)
|
access, data := helper.EndpointAccess(w, r)
|
||||||
|
|
||||||
if !access {
|
if !access {
|
||||||
|
|
@ -84,11 +82,14 @@ func ApiPost(w http.ResponseWriter, r *http.Request) {
|
||||||
case "/device/link/remove":
|
case "/device/link/remove":
|
||||||
RemoveLink("", w, r)
|
RemoveLink("", w, r)
|
||||||
case "/device/handshake":
|
case "/device/handshake":
|
||||||
|
log.Println("handshake")
|
||||||
Handshake(w, r)
|
Handshake(w, r)
|
||||||
case "/panel/list":
|
case "/panel/list":
|
||||||
PanelList(w, r)
|
PanelList(w, r)
|
||||||
case "/panel/get":
|
case "/panel/get":
|
||||||
GetPanel(w, r)
|
GetPanel("", w, r)
|
||||||
|
case "/panel/save/json":
|
||||||
|
SavePanelJSON(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -99,5 +100,7 @@ func ApiAuth(data string, w http.ResponseWriter, r *http.Request) {
|
||||||
PlayMacro(data, w, r)
|
PlayMacro(data, w, r)
|
||||||
case "/device/link/remove":
|
case "/device/link/remove":
|
||||||
RemoveLink(data, w, r)
|
RemoveLink(data, w, r)
|
||||||
|
case "/panel/get":
|
||||||
|
GetPanel(data, w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,8 @@ func decryptAuth(r *http.Request) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println("data", req.Data)
|
||||||
|
|
||||||
deviceKey, errKey := GetKeyByUuid(req.Uuid)
|
deviceKey, errKey := GetKeyByUuid(req.Uuid)
|
||||||
decryptData, errDec := DecryptAES(deviceKey, req.Data)
|
decryptData, errDec := DecryptAES(deviceKey, req.Data)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"be/app/helper"
|
||||||
"be/app/structs"
|
"be/app/structs"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -52,6 +52,8 @@ func getPanelInfo(dirname string) structs.PanelInfo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
panelInfo.Dir = dirname
|
||||||
|
|
||||||
thumb := getPanelThumb(dirname)
|
thumb := getPanelThumb(dirname)
|
||||||
panelInfo.Thumb = thumb
|
panelInfo.Thumb = thumb
|
||||||
|
|
||||||
|
|
@ -59,31 +61,89 @@ func getPanelInfo(dirname string) structs.PanelInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPanelThumb(dirname string) string {
|
func getPanelThumb(dirname string) string {
|
||||||
re := regexp.MustCompile(`^thumb\.(jpg|jpeg|png)$`)
|
extensions := []string{".jpg", ".jpeg", ".png", ".webp"}
|
||||||
files, _ := os.ReadDir("../panels/" + dirname)
|
|
||||||
|
|
||||||
for _, file := range files {
|
for _, ext := range extensions {
|
||||||
if file.IsDir() {
|
filename := "thumbnail" + ext
|
||||||
|
file, err := os.Open("../panels/" + dirname + "/" + filename)
|
||||||
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
if re.MatchString(filepath.Base(file.Name())) {
|
return encodeImg(file)
|
||||||
return filepath.Base(file.Name())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPanel(w http.ResponseWriter, r *http.Request) {
|
func getPanelCode(dirname string) (html string, css string) {
|
||||||
html, _ := os.ReadFile("../panels/test_panel/index.html")
|
htmlBytes, _ := os.ReadFile("../panels/" + dirname + "/index.html")
|
||||||
css, _ := os.ReadFile("../panels/test_panel/output.css")
|
cssBytes, _ := os.ReadFile("../panels/" + dirname + "/output.css")
|
||||||
|
|
||||||
type Response struct {
|
return string(htmlBytes), string(cssBytes)
|
||||||
Html string `json:"html"`
|
}
|
||||||
Css string `json:"css"`
|
|
||||||
|
func encodeImg(file *os.File) string {
|
||||||
|
contents, err := os.ReadFile(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := Response{Html: string(html), Css: string(css)}
|
encoded := base64.StdEncoding.EncodeToString(contents)
|
||||||
json.NewEncoder(w).Encode(resp)
|
return encoded
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPanel(data string, w http.ResponseWriter, r *http.Request) {
|
||||||
|
req := &structs.PanelRequest{}
|
||||||
|
|
||||||
|
_, err := helper.ParseRequest(req, data, r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = structs.PanelResponse{}
|
||||||
|
// dirname := req.Dir
|
||||||
|
panelInfo := getPanelInfo(req.Dir)
|
||||||
|
panelHtml, panelCss := getPanelCode(req.Dir)
|
||||||
|
|
||||||
|
response.Dir = panelInfo.Dir
|
||||||
|
response.Name = panelInfo.Name
|
||||||
|
response.Description = panelInfo.Description
|
||||||
|
response.AspectRatio = panelInfo.AspectRatio
|
||||||
|
response.Macros = panelInfo.Macros
|
||||||
|
response.Thumb = panelInfo.Thumb
|
||||||
|
response.HTML = panelHtml
|
||||||
|
response.Style = panelCss
|
||||||
|
|
||||||
|
json.NewEncoder(w).Encode(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SavePanelJSON(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var req structs.PanelSaveJSON
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&req)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filePath := "../panels/" + req.Dir + "/panel.json"
|
||||||
|
|
||||||
|
req.Dir = ""
|
||||||
|
|
||||||
|
// Marshal the data to JSON without the dir field
|
||||||
|
jsonData, err := json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(filePath, jsonData, 0644)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue