Add more error handler
This commit is contained in:
parent
b311a41dc7
commit
9b49c6da05
2 changed files with 100 additions and 82 deletions
|
|
@ -2,91 +2,109 @@ package routers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"go.uber.org/zap"
|
||||||
"net/http"
|
"net/http"
|
||||||
"recipe-manager/data"
|
"recipe-manager/data"
|
||||||
"recipe-manager/models"
|
"recipe-manager/models"
|
||||||
|
"recipe-manager/services/logger"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type MaterialRouter struct {
|
type MaterialRouter struct {
|
||||||
data *data.Data
|
data *data.Data
|
||||||
|
taoLogger *logger.TaoLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMaterialRouter(data *data.Data) *MaterialRouter {
|
func NewMaterialRouter(data *data.Data, taoLogger *logger.TaoLogger) *MaterialRouter {
|
||||||
return &MaterialRouter{
|
return &MaterialRouter{
|
||||||
data: data,
|
data: data,
|
||||||
|
taoLogger: taoLogger,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mr *MaterialRouter) Route(r chi.Router) {
|
func (mr *MaterialRouter) Route(r chi.Router) {
|
||||||
r.Route("/materials", func(r chi.Router) {
|
r.Route("/materials", func(r chi.Router) {
|
||||||
r.Get("/code", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/code", mr.getMaterialCode)
|
||||||
w.Header().Add("Content-Type", "application/json")
|
|
||||||
|
|
||||||
filename := r.URL.Query().Get("filename")
|
r.Get("/setting/{mat_id}", mr.getMaterialSettingByMatID)
|
||||||
country := r.URL.Query().Get("country")
|
|
||||||
|
|
||||||
matIDs := r.URL.Query().Get("mat_ids")
|
|
||||||
|
|
||||||
var matIDsUint []uint64
|
|
||||||
for _, v := range strings.Split(matIDs, ",") {
|
|
||||||
matIDUint, err := strconv.ParseUint(v, 10, 64)
|
|
||||||
|
|
||||||
if err != nil || matIDUint == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
matIDsUint = append(matIDsUint, matIDUint)
|
|
||||||
}
|
|
||||||
|
|
||||||
countryID, err := mr.data.GetCountryIDByName(country)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Country not found", http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
material := mr.data.GetMaterialCode(matIDsUint, countryID, filename)
|
|
||||||
|
|
||||||
json.NewEncoder(w).Encode(material)
|
|
||||||
})
|
|
||||||
|
|
||||||
r.Get("/setting/{mat_id}", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Add("Content-Type", "application/json")
|
|
||||||
|
|
||||||
filename := r.URL.Query().Get("filename")
|
|
||||||
country := r.URL.Query().Get("country")
|
|
||||||
|
|
||||||
countryID, err := mr.data.GetCountryIDByName(country)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Country not found", http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
material := mr.data.GetMaterialSetting(countryID, filename)
|
|
||||||
|
|
||||||
matID := chi.URLParam(r, "mat_id")
|
|
||||||
|
|
||||||
matIDuint, err := strconv.ParseUint(matID, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Invalid material id", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var matSetting models.MaterialSetting
|
|
||||||
|
|
||||||
for _, mat := range material {
|
|
||||||
if mat.ID == matIDuint {
|
|
||||||
matSetting = mat
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
json.NewEncoder(w).Encode(matSetting)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mr *MaterialRouter) getMaterialCode(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Add("Content-Type", "application/json")
|
||||||
|
|
||||||
|
filename := r.URL.Query().Get("filename")
|
||||||
|
country := r.URL.Query().Get("country")
|
||||||
|
|
||||||
|
matIDs := r.URL.Query().Get("mat_ids")
|
||||||
|
|
||||||
|
var matIDsUint []uint64
|
||||||
|
for _, v := range strings.Split(matIDs, ",") {
|
||||||
|
matIDUint, err := strconv.ParseUint(v, 10, 64)
|
||||||
|
|
||||||
|
if err != nil || matIDUint == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
matIDsUint = append(matIDsUint, matIDUint)
|
||||||
|
}
|
||||||
|
|
||||||
|
countryID, err := mr.data.GetCountryIDByName(country)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
mr.taoLogger.Log.Error("MaterialRouter.GetMaterialCode", zap.Error(err))
|
||||||
|
http.Error(w, "Country not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
material := mr.data.GetMaterialCode(matIDsUint, countryID, filename)
|
||||||
|
|
||||||
|
if err := json.NewEncoder(w).Encode(material); err != nil {
|
||||||
|
mr.taoLogger.Log.Error("MaterialRouter.GetMaterialCode", zap.Error(err))
|
||||||
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mr *MaterialRouter) getMaterialSettingByMatID(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Add("Content-Type", "application/json")
|
||||||
|
|
||||||
|
filename := r.URL.Query().Get("filename")
|
||||||
|
country := r.URL.Query().Get("country")
|
||||||
|
|
||||||
|
countryID, err := mr.data.GetCountryIDByName(country)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
|
||||||
|
http.Error(w, "Country not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
material := mr.data.GetMaterialSetting(countryID, filename)
|
||||||
|
|
||||||
|
matID := chi.URLParam(r, "mat_id")
|
||||||
|
|
||||||
|
matIDuint, err := strconv.ParseUint(matID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
|
||||||
|
http.Error(w, "Invalid material id", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var matSetting models.MaterialSetting
|
||||||
|
|
||||||
|
for _, mat := range material {
|
||||||
|
if mat.ID == matIDuint {
|
||||||
|
matSetting = mat
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewEncoder(w).Encode(matSetting); err != nil {
|
||||||
|
mr.taoLogger.Log.Error("MaterialRouter.GetMaterialSettingByMatID", zap.Error(err))
|
||||||
|
http.Error(w, "Internal Error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -197,11 +197,11 @@ func (s *Server) createHandler() {
|
||||||
|
|
||||||
// locking
|
// locking
|
||||||
if !pyAPIhandler(w, r) {
|
if !pyAPIhandler(w, r) {
|
||||||
s.taoLogger.Log.Warn("Merge - user tried to access while another user is requesting merge",
|
s.taoLogger.Log.Warn("Merge - u tried to access while another u is requesting merge",
|
||||||
zap.String("user", r.Context().Value("user").(*models.User).Name))
|
zap.String("u", r.Context().Value("u").(*models.User).Name))
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
s.taoLogger.Log.Debug("Merge - user has access", zap.String("user", r.Context().Value("user").(*models.User).Name))
|
s.taoLogger.Log.Debug("Merge - u has access", zap.String("u", r.Context().Value("u").(*models.User).Name))
|
||||||
}
|
}
|
||||||
|
|
||||||
var targetMap map[string]interface{}
|
var targetMap map[string]interface{}
|
||||||
|
|
@ -234,27 +234,27 @@ func (s *Server) createHandler() {
|
||||||
dev_path := repo_path + dev_version + ".json"
|
dev_path := repo_path + dev_version + ".json"
|
||||||
|
|
||||||
// Get who's requesting
|
// Get who's requesting
|
||||||
user := r.Context().Value("user").(*models.User)
|
u := r.Context().Value("u").(*models.User)
|
||||||
s.taoLogger.Log.Info("Request merge by", zap.String("user", user.Name))
|
s.taoLogger.Log.Info("Request merge by", zap.String("u", u.Name))
|
||||||
|
|
||||||
// lookup for python exec
|
// lookup for python exec
|
||||||
py_exec, err := exec.LookPath("python")
|
pyExec, err := exec.LookPath("python")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.taoLogger.Log.Fatal("Python error: ", zap.Error(err))
|
s.taoLogger.Log.Fatal("Python error: ", zap.Error(err))
|
||||||
} else {
|
} else {
|
||||||
py_exec, err = filepath.Abs(py_exec)
|
pyExec, err = filepath.Abs(pyExec)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.taoLogger.Log.Info("Found python exec: ", zap.String("PythonPath", py_exec))
|
s.taoLogger.Log.Info("Found python exec: ", zap.String("PythonPath", pyExec))
|
||||||
// target api file
|
// target api file
|
||||||
merge_api, api_err := os.Open("./python_api/merge_recipe.py")
|
mergeApi, api_err := os.Open("./python_api/merge_recipe.py")
|
||||||
if api_err != nil {
|
if api_err != nil {
|
||||||
s.taoLogger.Log.Fatal("Merge request failed. Python api error: ", zap.String("ApiErr", api_err.Error()))
|
s.taoLogger.Log.Fatal("Merge request failed. Python api error: ", zap.String("ApiErr", api_err.Error()))
|
||||||
}
|
}
|
||||||
defer merge_api.Close()
|
defer mergeApi.Close()
|
||||||
// log.Println("Locate python api", merge_api.Name())
|
// log.Println("Locate python api", merge_api.Name())
|
||||||
s.taoLogger.Log.Info("Locate python api", zap.String("ApiName", merge_api.Name()))
|
s.taoLogger.Log.Info("Locate python api", zap.String("ApiName", mergeApi.Name()))
|
||||||
cmd := exec.Command(py_exec, merge_api.Name(), "merge", master_path, dev_path, output_path, changelog_path, "", user.Name)
|
cmd := exec.Command(pyExec, mergeApi.Name(), "merge", master_path, dev_path, output_path, changelog_path, "", u.Name)
|
||||||
|
|
||||||
// log.Println("Run merge command", cmd)
|
// log.Println("Run merge command", cmd)
|
||||||
s.taoLogger.Log.Info("Merge", zap.String("master", master_path), zap.String("dev", dev_path), zap.String("output", output_path))
|
s.taoLogger.Log.Info("Merge", zap.String("master", master_path), zap.String("dev", dev_path), zap.String("output", output_path))
|
||||||
|
|
@ -464,7 +464,7 @@ func (s *Server) createHandler() {
|
||||||
rr.Route(r)
|
rr.Route(r)
|
||||||
|
|
||||||
// Material Router
|
// Material Router
|
||||||
mr := routers.NewMaterialRouter(s.data)
|
mr := routers.NewMaterialRouter(s.data, s.taoLogger)
|
||||||
mr.Route(r)
|
mr.Route(r)
|
||||||
|
|
||||||
// User Router
|
// User Router
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue