package main import ( "context" "encoding/json" "fmt" "github.com/jmoiron/sqlx" "io" "log" "net/http" "os" "os/exec" "path/filepath" "recipe-manager/config" "recipe-manager/data" "recipe-manager/enums/permissions" "recipe-manager/models" "recipe-manager/routers" "recipe-manager/services/logger" "recipe-manager/services/oauth" "recipe-manager/services/recipe" "recipe-manager/services/sheet" "recipe-manager/services/user" "strings" "sync" "time" "github.com/go-chi/chi/v5" "github.com/go-chi/cors" "github.com/spf13/viper" "go.uber.org/zap" "golang.org/x/oauth2" ) var ( pythonApiLock sync.Mutex ) func loadConfig(path string) (*config.ServerConfig, error) { viper.AddConfigPath(path) viper.SetConfigName("app") viper.SetConfigType("env") viper.AutomaticEnv() var serverConfig config.ServerConfig err := viper.ReadInConfig() if err != nil { return nil, err } err = viper.Unmarshal(&serverConfig) if err != nil { return nil, err } return &serverConfig, nil } type Server struct { server *http.Server data *data.Data database *sqlx.DB cfg *config.ServerConfig oauth oauth.OAuthService taoLogger *logger.TaoLogger } func NewServer() *Server { serverCfg, err := loadConfig(".") if err != nil { log.Fatal(err) } taoLogger := logger.NewTaoLogger(serverCfg) return &Server{ server: &http.Server{Addr: fmt.Sprintf(":%d", serverCfg.ServerPort)}, data: data.NewData(taoLogger), database: data.NewSqliteDatabase(), cfg: serverCfg, oauth: oauth.NewOAuthService(serverCfg), taoLogger: taoLogger, } } func (s *Server) Run() error { //go cli.CommandLineListener() s.createHandler() // log.Printf("Server running on %s", s.server.Addr) s.taoLogger.Log.Info("Server running", zap.String("addr", s.server.Addr)) defer func(Log *zap.Logger) { err := Log.Sync() if err != nil { log.Fatal(err) } }(s.taoLogger.Log) return s.server.ListenAndServe() } func (s *Server) createHandler() { r := chi.NewRouter() r.Use(cors.Handler(cors.Options{ AllowedOrigins: strings.Split(s.cfg.AllowedOrigins, ","), AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, AllowCredentials: true, ExposedHeaders: []string{"Content-Type"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"}, })) // Recipe Service recipeService := recipe.NewRecipeService(s.data) // User Service userService := user.NewUserService(s.cfg, s.database, s.taoLogger) // Seed _ = userService.CreateNewUser(context.WithValue(context.Background(), "user", &models.User{Email: "system"}), "kenta420", "poomipat.c@forth.co.th", "", permissions.SuperAdmin) // Auth Router r.Group(func(r chi.Router) { ar := routers.NewAuthRouter(s.cfg, s.oauth, userService, s.taoLogger) ar.Route(r) }) // Protected 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) { token := &oauth2.Token{} if cookie, err := r.Cookie("access_token"); err == nil { token.AccessToken = cookie.Value } userInfo, err := s.oauth.GetUserInfo(r.Context(), token) if err != nil { // if have refresh token, set refresh token to token if cookie, err := r.Cookie("refresh_token"); err == nil { token.RefreshToken = cookie.Value } newToken, err := s.oauth.RefreshToken(r.Context(), token) if err != nil { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } userInfo, err = s.oauth.GetUserInfo(r.Context(), newToken) if err != nil { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } // set new token to cookie w.Header().Add("set-cookie", fmt.Sprintf("access_token=%s; Path=/; HttpOnly; SameSite=None; Secure; Max-Age=3600", newToken.AccessToken)) } if userInfo != nil { userFromDB, err := userService.GetUserByEmail(r.Context(), userInfo.Email) if err != nil { if err != nil { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } } if userFromDB != nil { userInfo.ID = userFromDB.ID userInfo.Name = userFromDB.Name if userFromDB.Picture != "" { userInfo.Picture = userFromDB.Picture } userInfo.Permissions = userFromDB.Permissions } } ctx := context.WithValue(r.Context(), "user", userInfo) next.ServeHTTP(w, r.WithContext(ctx)) }) }) r.Post("/merge", func(w http.ResponseWriter, r *http.Request) { // locking if !pyAPIhandler(w, r) { s.taoLogger.Log.Warn("Merge - u tried to access while another u is requesting merge", zap.String("u", r.Context().Value("u").(*models.User).Name)) return } else { s.taoLogger.Log.Debug("Merge - u has access", zap.String("u", r.Context().Value("u").(*models.User).Name)) } var targetMap map[string]interface{} err := json.NewDecoder(r.Body).Decode(&targetMap) if err != nil { w.WriteHeader(http.StatusBadRequest) s.taoLogger.Log.Fatal("Merge request failed", zap.Error(err)) return } 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(repo_path + master_version + ".json"); err != nil { w.WriteHeader(http.StatusBadRequest) s.taoLogger.Log.Fatal("Merge request failed. Master file not found: ", zap.Error(err)) return } if _, err := os.Stat(repo_path + dev_version + ".json"); err != nil { w.WriteHeader(http.StatusBadRequest) s.taoLogger.Log.Fatal("Merge request failed. Dev file not found: ", zap.Error(err)) return } master_path := repo_path + master_version + ".json" dev_path := repo_path + dev_version + ".json" // Get who's requesting u := r.Context().Value("u").(*models.User) s.taoLogger.Log.Info("Request merge by", zap.String("u", u.Name)) // lookup for python exec pyExec, err := exec.LookPath("python") if err != nil { s.taoLogger.Log.Fatal("Python error: ", zap.Error(err)) } else { pyExec, err = filepath.Abs(pyExec) } s.taoLogger.Log.Info("Found python exec: ", zap.String("PythonPath", pyExec)) // target api file mergeApi, api_err := os.Open("./python_api/merge_recipe.py") if api_err != nil { s.taoLogger.Log.Fatal("Merge request failed. Python api error: ", zap.String("ApiErr", api_err.Error())) } defer mergeApi.Close() // log.Println("Locate python api", merge_api.Name()) s.taoLogger.Log.Info("Locate python api", zap.String("ApiName", mergeApi.Name())) cmd := exec.Command(pyExec, mergeApi.Name(), "merge", master_path, dev_path, output_path, changelog_path, "", u.Name) // 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.Debug("Run merge command", zap.String("Command", cmd.String())) out, err := cmd.CombinedOutput() // log.Println(string(out)) s.taoLogger.Log.Debug("Merge output", zap.String("Output", string(out))) if err != nil { // log.Fatalln("Merge request failed. Python merge failed: ", err) s.taoLogger.Log.Fatal("Merge request failed. Python merge failed", zap.Error(err)) } w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]string{"message": "Merge success"}) s.taoLogger.Log.Info("Merge success", zap.String("output", "merge success")) }) r.Post("/dllog", func(w http.ResponseWriter, r *http.Request) { s.taoLogger.Log.Debug("Request uri = ", zap.String("uri", r.RequestURI)) s.taoLogger.Log.Debug("Query param = ", zap.String("query", r.URL.Query().Get("query"))) // param param := r.URL.Query().Get("query") s.taoLogger.Log.Debug("Param = ", zap.String("param", param)) var postRequest map[string]interface{} err := json.NewDecoder(r.Body).Decode(&postRequest) s.taoLogger.Log.Debug("Log request: ", zap.String("postRequest", fmt.Sprintf("%+v", postRequest))) if err != nil { w.WriteHeader(http.StatusBadRequest) s.taoLogger.Log.Fatal("Decode in request failed: ", zap.Error(err)) return } file_ext := ".html" if rb, ok := postRequest["htmlfile"].(bool); ok { if rj, ok := postRequest["requestJson"].(bool); ok { if rj { file_ext = ".json" } else if !rb && !rj { file_ext = ".log" } } } log_name := postRequest["filename"].(string) s.taoLogger.Log.Warn("Log file name: ", zap.String("filename", log_name)) if log_name == "" { s.taoLogger.Log.Fatal("Empty log file name") } // log.Println("Log file ext: ", file_ext) default_changelog_path := "cofffeemachineConfig/" + param + "/" s.taoLogger.Log.Debug("Default changelog path: ", zap.String("default_changelog_path", default_changelog_path)) changelog_path := default_changelog_path + log_name + file_ext s.taoLogger.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 } logFile, err := os.Open(changelog_path) if err != nil { s.taoLogger.Log.Fatal("Log request failed: ", zap.Error(err)) http.Error(w, err.Error(), http.StatusInternalServerError) } defer logFile.Close() if file_ext == ".json" { var logFileJson map[string]interface{} err = json.NewDecoder(logFile).Decode(&logFileJson) if err != nil { s.taoLogger.Log.Fatal("Error when decode log file: ", zap.Error(err)) } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(logFileJson) s.taoLogger.Log.Info("Log file: ", zap.String("filename", log_name)) } else { w.Header().Set("Content-Disposition", "attachment; filename=logfile"+file_ext) w.Header().Set("Content-Type", "application/octet-stream") _, err = io.Copy(w, logFile) if err != nil { s.taoLogger.Log.Fatal("Could not send blob", zap.Error(err)) http.Error(w, err.Error(), http.StatusInternalServerError) return } } }) r.Get("/listFileInDir/*", func(w http.ResponseWriter, r *http.Request) { //spl spl_path := strings.Split(r.RequestURI, "/") if len(spl_path) > 3 { s.taoLogger.Log.Warn("Unexpected depth: ", zap.String("path", r.RequestURI), zap.String("depth", fmt.Sprintf("%d", len(spl_path)))) } if spl_path[2] == "" { s.taoLogger.Log.Error("Empty target dir", zap.String("path", r.RequestURI)) } s.taoLogger.Log.Debug("Split path = ", zap.Any("paths", spl_path)) // s.taoLogger.Log.Info("Target dir: ", zap.String("dir", "cofffeemachineConfig")) main_folder := "cofffeemachineConfig" target_path := main_folder + "/" + spl_path[2] dir, err := os.ReadDir(target_path) if err != nil { s.taoLogger.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 } s.taoLogger.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) { s.taoLogger.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}) s.taoLogger.Log.Debug("Scan dir completed < ", zap.String("path", r.RequestURI)) }) r.Get("/get_log_relation", func(w http.ResponseWriter, r *http.Request) { // Python looker py_exec, err := exec.LookPath("python") if err != nil { s.taoLogger.Log.Error("Error while trying to find python: ", zap.Error(err)) http.Error(w, err.Error(), http.StatusInternalServerError) } merge_timeline, api_err := os.Open("./python_api/merge_timeline.py") if api_err != nil { s.taoLogger.Log.Error("Error while trying to open merge_timeline.json: ", zap.Error(api_err)) http.Error(w, api_err.Error(), http.StatusInternalServerError) } defer merge_timeline.Close() cmd := exec.Command(py_exec, merge_timeline.Name(), "get_relate") s.taoLogger.Log.Debug("Command: ", zap.String("command", cmd.String())) out, err := cmd.CombinedOutput() s.taoLogger.Log.Debug("Output: ", zap.String("output", string(out))) if err != nil { s.taoLogger.Log.Error("Error while trying to run python: ", zap.Error(err)) http.Error(w, err.Error(), http.StatusInternalServerError) } // clean up output clean1 := strings.ReplaceAll(string(out), "\n", "") clean2 := strings.ReplaceAll(clean1, "\r", "") w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(map[string]interface{}{ "message": clean2, }) }) r.Post("/diffpy/*", func(w http.ResponseWriter, r *http.Request) { s.taoLogger.Log.Debug("Diffpy: ", zap.String("path", r.RequestURI)) // TODO: add command exec `python_Exec` `merge_recipe.py` `diff` `master_version` `version-version-version` `debug?` `flatten={true|false}` `out={true|false}` }) sheetService, err := sheet.NewSheetService(context.Background(), s.cfg) if err != nil { s.taoLogger.Log.Fatal("Error while trying to create sheet service: ", zap.Error(err)) return } // Recipe Router rr := routers.NewRecipeRouter(s.data, recipeService, sheetService, s.taoLogger) rr.Route(r) // Material Router mr := routers.NewMaterialRouter(s.data, s.taoLogger) mr.Route(r) // User Router ur := routers.NewUserRouter(s.taoLogger, userService) ur.Route(r) }) r.NotFound(func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusNotFound) json.NewEncoder(w).Encode(map[string]string{"message": fmt.Sprintf("path %s are not exits", r.RequestURI)}) }) s.server.Handler = r } func (s *Server) Shutdown(ctx context.Context) error { return s.server.Shutdown(ctx) } func pyAPIhandler(w http.ResponseWriter, r *http.Request) bool { timeout := 10 * time.Second if !lockThenTimeout(&pythonApiLock, timeout) { http.Error(w, "API is busy", http.StatusServiceUnavailable) return false } defer pythonApiLock.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 } }