add py api lock & fix file path bug
This commit is contained in:
parent
899b888d82
commit
3f399dda0e
1 changed files with 58 additions and 17 deletions
|
|
@ -18,6 +18,8 @@ import (
|
||||||
"recipe-manager/services/logger"
|
"recipe-manager/services/logger"
|
||||||
"recipe-manager/services/oauth"
|
"recipe-manager/services/oauth"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/cors"
|
"github.com/go-chi/cors"
|
||||||
|
|
@ -28,6 +30,8 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Log = logger.GetInstance()
|
Log = logger.GetInstance()
|
||||||
|
|
||||||
|
python_api_lock sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadConfig(path string) (*config.ServerConfig, error) {
|
func loadConfig(path string) (*config.ServerConfig, error) {
|
||||||
|
|
@ -155,6 +159,15 @@ func (s *Server) createHandler() {
|
||||||
|
|
||||||
r.Post("/merge", func(w http.ResponseWriter, r *http.Request) {
|
r.Post("/merge", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
// locking
|
||||||
|
if !pyAPIhandler(w, r) {
|
||||||
|
Log.Warn("Merge - user tried to access while another user is requesting merge",
|
||||||
|
zap.String("user", r.Context().Value("user").(*models.User).Name))
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
Log.Debug("Merge - user has access", zap.String("user", r.Context().Value("user").(*models.User).Name))
|
||||||
|
}
|
||||||
|
|
||||||
var targetMap map[string]interface{}
|
var targetMap map[string]interface{}
|
||||||
err := json.NewDecoder(r.Body).Decode(&targetMap)
|
err := json.NewDecoder(r.Body).Decode(&targetMap)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -230,6 +243,7 @@ func (s *Server) createHandler() {
|
||||||
Log.Debug("Query param = ", zap.String("query", r.URL.Query().Get("query")))
|
Log.Debug("Query param = ", zap.String("query", r.URL.Query().Get("query")))
|
||||||
// param
|
// param
|
||||||
param := r.URL.Query().Get("query")
|
param := r.URL.Query().Get("query")
|
||||||
|
Log.Debug("Param = ", zap.String("param", param))
|
||||||
|
|
||||||
var postRequest map[string]interface{}
|
var postRequest map[string]interface{}
|
||||||
err := json.NewDecoder(r.Body).Decode(&postRequest)
|
err := json.NewDecoder(r.Body).Decode(&postRequest)
|
||||||
|
|
@ -261,9 +275,11 @@ func (s *Server) createHandler() {
|
||||||
|
|
||||||
// log.Println("Log file ext: ", file_ext)
|
// log.Println("Log file ext: ", file_ext)
|
||||||
default_changelog_path := "cofffeemachineConfig/" + param + "/"
|
default_changelog_path := "cofffeemachineConfig/" + param + "/"
|
||||||
|
Log.Debug("Default changelog path: ", zap.String("default_changelog_path", default_changelog_path))
|
||||||
|
|
||||||
changelog_path := default_changelog_path + log_name + file_ext
|
changelog_path := default_changelog_path + log_name + file_ext
|
||||||
if strings.Contains(log_name, default_changelog_path) && strings.Contains(log_name, ".json") {
|
Log.Debug("Changelog path: ", zap.String("changelog_path", changelog_path))
|
||||||
|
if strings.Contains(log_name, "cofffeemachineConfig") && strings.Contains(log_name, ".json") {
|
||||||
changelog_path = log_name
|
changelog_path = log_name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -301,24 +317,24 @@ func (s *Server) createHandler() {
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Get("/mergelogList", func(w http.ResponseWriter, r *http.Request) {
|
// r.Get("/mergelogList", func(w http.ResponseWriter, r *http.Request) {
|
||||||
ch_dir, err := os.ReadDir("cofffeemachineConfig/changelog")
|
// ch_dir, err := os.ReadDir("cofffeemachineConfig/changelog")
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
Log.Error("Error while trying to read dir: ", zap.String("dir", "cofffeemachineConfig/changelog"), zap.Error(err))
|
// Log.Error("Error while trying to read dir: ", zap.String("dir", "cofffeemachineConfig/changelog"), zap.Error(err))
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
// http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
// }
|
||||||
displayable := make([]string, 0)
|
// displayable := make([]string, 0)
|
||||||
|
|
||||||
for _, file := range ch_dir {
|
// for _, file := range ch_dir {
|
||||||
if strings.Contains(file.Name(), ".html") {
|
// if strings.Contains(file.Name(), ".html") {
|
||||||
|
|
||||||
displayable = append(displayable, file.Name()[:len(file.Name())-len(filepath.Ext(file.Name()))])
|
// displayable = append(displayable, file.Name()[:len(file.Name())-len(filepath.Ext(file.Name()))])
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
w.Header().Set("Content-Type", "application/json")
|
// w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
// w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(map[string][]string{"dirs": displayable})
|
// 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) {
|
||||||
|
|
||||||
|
|
@ -394,3 +410,28 @@ func (s *Server) createHandler() {
|
||||||
func (s *Server) Shutdown(ctx context.Context) error {
|
func (s *Server) Shutdown(ctx context.Context) error {
|
||||||
return s.server.Shutdown(ctx)
|
return s.server.Shutdown(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pyAPIhandler(w http.ResponseWriter, r *http.Request) bool {
|
||||||
|
timeout := 10 * time.Second
|
||||||
|
|
||||||
|
if !lockThenTimeout(&python_api_lock, timeout) {
|
||||||
|
http.Error(w, "API is busy", http.StatusServiceUnavailable)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer python_api_lock.Unlock()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func lockThenTimeout(mutex *sync.Mutex, timeout time.Duration) bool {
|
||||||
|
ch := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
mutex.Lock()
|
||||||
|
close(ch)
|
||||||
|
}()
|
||||||
|
select {
|
||||||
|
case <-ch:
|
||||||
|
return true
|
||||||
|
case <-time.After(timeout):
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue