Add py api, WIP dl log

This commit is contained in:
pakintada@gmail.com 2023-09-20 13:35:36 +07:00
parent 35b8b3be57
commit b49b2235db
7 changed files with 159 additions and 111 deletions

View file

@ -9,6 +9,7 @@ import (
"net/url"
"os"
"os/exec"
"path/filepath"
"recipe-manager/config"
"recipe-manager/data"
"recipe-manager/routers"
@ -77,9 +78,8 @@ func (s *Server) createHandler() {
AllowedOrigins: strings.Split(s.cfg.AllowedOrigins, ","),
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowCredentials: true,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
MaxAge: 300, // Maximum value not ignored by any of major browsers
// Debug: true,
ExposedHeaders: []string{"Content-Type"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
}))
database := data.NewData()
@ -92,6 +92,7 @@ func (s *Server) createHandler() {
// Protect Group
r.Group(func(r chi.Router) {
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -121,11 +122,8 @@ func (s *Server) createHandler() {
})
})
// Recipe Router
rr := routers.NewRecipeRouter(database)
rr.Route(r)
r.Post("/merge", func(w http.ResponseWriter, r *http.Request) {
var targetMap map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&targetMap)
if err != nil {
@ -133,38 +131,72 @@ func (s *Server) createHandler() {
log.Fatalln("Merge request failed: ", err)
return
}
log.Println(targetMap)
repo_path := "cofffeemachineConfig/coffeethai02_"
master_version := targetMap["master"].(string)
dev_version := targetMap["dev"].(string)
output_path := targetMap["output"].(string)
changelog_path := targetMap["changelog"].(string)
// find target file in the cofffeemachineConfig
if _, err := os.Stat("coffeemachineConfig/coffeethai02_" + master_version + ".json"); err != nil {
if _, err := os.Stat(repo_path + master_version + ".json"); err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Fatalln("Merge request failed. Master file not found: ", err)
return
}
if _, err := os.Stat("coffeemachineConfig/coffeethai02_" + dev_version + ".json"); err != nil {
if _, err := os.Stat(repo_path + dev_version + ".json"); err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Fatalln("Merge request failed. Dev file not found: ", err)
return
}
repo_path := "coffeemachineConfig/coffeethai02_"
master_path := repo_path + master_version + ".json"
dev_path := repo_path + dev_version + ".json"
// output path
output_path := ""
// // output path
// output_path := ""
// changelog path
changelog_path := ""
// // changelog path
// changelog_path := ""
// TODO: Call merge api if found
err = exec.Command("python", "merge", master_path, dev_path, output_path, changelog_path).Run()
// lookup for python exec
py_exec, err := exec.LookPath("python")
if err != nil {
log.Fatalln("Python error: ", err)
} else {
py_exec, err = filepath.Abs(py_exec)
}
log.Println("Found python exec: ", py_exec)
// target api file
merge_api, api_err := os.Open("./python_api/merge_recipe.py")
if api_err != nil {
log.Fatalln("Merge request failed. Python api error: ", api_err)
}
log.Println("Locate python api", merge_api.Name())
cmd := exec.Command(py_exec, merge_api.Name(), "merge", master_path, dev_path, output_path, changelog_path, "debug")
log.Println("Run merge command", cmd)
err = cmd.Run()
if err != nil {
log.Fatalln("Merge request failed. Python merge failed: ", err)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"message": "Merge success"})
})
r.Get("/dllog", func(w http.ResponseWriter, r *http.Request) {
})
// Recipe Router
rr := routers.NewRecipeRouter(database)
rr.Route(r)
})
r.NotFound(func(w http.ResponseWriter, r *http.Request) {