merge
This commit is contained in:
parent
9e8efa4cfc
commit
d737eed480
5 changed files with 143 additions and 81 deletions
134
server/server.go
134
server/server.go
|
|
@ -23,15 +23,21 @@ import (
|
|||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/cors"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
var (
|
||||
Log = logger.GetInstance()
|
||||
|
||||
Log = logger.GetInstance()
|
||||
python_api_lock sync.Mutex
|
||||
|
||||
upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
)
|
||||
|
||||
func loadConfig(path string) (*config.ServerConfig, error) {
|
||||
|
|
@ -336,60 +342,98 @@ func (s *Server) createHandler() {
|
|||
// json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
|
||||
// })
|
||||
|
||||
r.Get("/listFileInDir/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Get("/listFileInDir", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// target_dir, err := os.ReadDir("cofffeemachineConfig")
|
||||
// socket
|
||||
socket, err := upgrader.Upgrade(w, r, nil)
|
||||
|
||||
if err != nil {
|
||||
Log.Error("Error while trying to upgrade socket: ", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Change to websocket for better performance
|
||||
for {
|
||||
msg_type, msg, err := socket.ReadMessage()
|
||||
|
||||
if err != nil {
|
||||
Log.Warn("Idle ", zap.Any("status", err))
|
||||
return
|
||||
}
|
||||
|
||||
var msgPayload map[string]interface{}
|
||||
|
||||
err = json.Unmarshal(msg, &msgPayload)
|
||||
if err != nil {
|
||||
Log.Error("Error while trying to unmarshal message: ", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
// topic := msgPayload["topic"].(string)
|
||||
|
||||
Log.Info("Receive message: ", zap.Any("msg_type", msg_type), zap.Any("msg", msgPayload))
|
||||
|
||||
response, err := json.Marshal(map[string]interface{}{
|
||||
"message": "ok",
|
||||
})
|
||||
if err != nil {
|
||||
Log.Error("Error at marshalling response: ", zap.Error(err))
|
||||
return
|
||||
}
|
||||
// respons to client
|
||||
socket.WriteMessage(msg_type, response)
|
||||
}
|
||||
|
||||
// spl
|
||||
spl_path := strings.Split(r.RequestURI, "/")
|
||||
if len(spl_path) > 3 {
|
||||
Log.Warn("Unexpected depth: ",
|
||||
zap.String("path", r.RequestURI),
|
||||
zap.String("depth", fmt.Sprintf("%d", len(spl_path))))
|
||||
}
|
||||
// spl_path := strings.Split(r.RequestURI, "/")
|
||||
// if len(spl_path) > 3 {
|
||||
// Log.Warn("Unexpected depth: ",
|
||||
// zap.String("path", r.RequestURI),
|
||||
// zap.String("depth", fmt.Sprintf("%d", len(spl_path))))
|
||||
// }
|
||||
|
||||
if spl_path[2] == "" {
|
||||
Log.Error("Empty target dir", zap.String("path", r.RequestURI))
|
||||
}
|
||||
// if spl_path[2] == "" {
|
||||
// Log.Error("Empty target dir", zap.String("path", r.RequestURI))
|
||||
// }
|
||||
|
||||
Log.Debug("Split path = ", zap.Any("paths", spl_path))
|
||||
// Log.Debug("Split path = ", zap.Any("paths", spl_path))
|
||||
|
||||
// Log.Info("Target dir: ", zap.String("dir", "cofffeemachineConfig"))
|
||||
// // Log.Info("Target dir: ", zap.String("dir", "cofffeemachineConfig"))
|
||||
|
||||
main_folder := "cofffeemachineConfig"
|
||||
target_path := main_folder + "/" + spl_path[2]
|
||||
// main_folder := "cofffeemachineConfig"
|
||||
// target_path := main_folder + "/" + spl_path[2]
|
||||
|
||||
dir, err := os.ReadDir(target_path)
|
||||
if err != nil {
|
||||
Log.Error("Error while trying to read dir: ", zap.String("dir", target_path), zap.Error(err))
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
// dir, err := os.ReadDir(target_path)
|
||||
// if err != nil {
|
||||
// Log.Error("Error while trying to read dir: ", zap.String("dir", target_path), zap.Error(err))
|
||||
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
// }
|
||||
|
||||
// File ext
|
||||
file_ext := ".json"
|
||||
switch spl_path[2] {
|
||||
case "changelog":
|
||||
file_ext = ".html"
|
||||
break
|
||||
case "merge":
|
||||
file_ext = ".json"
|
||||
break
|
||||
}
|
||||
Log.Debug("Set file ext = ", zap.String("file_ext", file_ext))
|
||||
// // File ext
|
||||
// file_ext := ".json"
|
||||
// switch spl_path[2] {
|
||||
// case "changelog":
|
||||
// file_ext = ".html"
|
||||
// break
|
||||
// case "merge":
|
||||
// file_ext = ".json"
|
||||
// break
|
||||
// }
|
||||
// Log.Debug("Set file ext = ", zap.String("file_ext", file_ext))
|
||||
|
||||
displayable := make([]string, 0)
|
||||
for _, file := range dir {
|
||||
if strings.Contains(file.Name(), file_ext) {
|
||||
Log.Debug("Found file: ", zap.String("file", file.Name()))
|
||||
displayable = append(displayable, file.Name()[:len(file.Name())-len(filepath.Ext(file.Name()))])
|
||||
}
|
||||
}
|
||||
// displayable := make([]string, 0)
|
||||
// for _, file := range dir {
|
||||
// if strings.Contains(file.Name(), file_ext) {
|
||||
// Log.Debug("Found file: ", zap.String("file", file.Name()))
|
||||
// displayable = append(displayable, file.Name()[:len(file.Name())-len(filepath.Ext(file.Name()))])
|
||||
// }
|
||||
// }
|
||||
|
||||
// send back
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
|
||||
Log.Debug("Scan dir completed < ", zap.String("path", r.RequestURI))
|
||||
// // send back
|
||||
// w.Header().Set("Content-Type", "application/json")
|
||||
// w.WriteHeader(http.StatusOK)
|
||||
// json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
|
||||
// Log.Debug("Scan dir completed < ", zap.String("path", r.RequestURI))
|
||||
})
|
||||
|
||||
// Recipe Router
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue