478 lines
13 KiB
Go
478 lines
13 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"recipe-manager/config"
|
|
"recipe-manager/data"
|
|
"recipe-manager/models"
|
|
"recipe-manager/routers"
|
|
"recipe-manager/services/logger"
|
|
"recipe-manager/services/oauth"
|
|
"recipe-manager/services/sheet"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"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()
|
|
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) {
|
|
viper.AddConfigPath(path)
|
|
viper.SetConfigName("app")
|
|
viper.SetConfigType("env")
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
var config config.ServerConfig
|
|
err := viper.ReadInConfig()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = viper.Unmarshal(&config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &config, nil
|
|
}
|
|
|
|
type Server struct {
|
|
server *http.Server
|
|
data *data.Data
|
|
cfg *config.ServerConfig
|
|
oauth oauth.OAuthService
|
|
}
|
|
|
|
func NewServer() *Server {
|
|
|
|
serverCfg, err := loadConfig(".")
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return &Server{
|
|
server: &http.Server{Addr: fmt.Sprintf(":%d", serverCfg.ServerPort)},
|
|
data: data.NewData(),
|
|
cfg: serverCfg,
|
|
oauth: oauth.NewOAuthService(serverCfg),
|
|
}
|
|
}
|
|
|
|
func (s *Server) Run() error {
|
|
|
|
// logger
|
|
// defer log_inst.Sync()
|
|
|
|
if s.cfg.Debug {
|
|
// logger.SetLevel("DEBUG")
|
|
Log.Debug("Debug mode", zap.Bool("enable", s.cfg.Debug))
|
|
logger.EnableDebug(s.cfg.Debug)
|
|
}
|
|
|
|
//go cli.CommandLineListener()
|
|
|
|
s.createHandler()
|
|
// log.Printf("Server running on %s", s.server.Addr)
|
|
Log.Info("Server running", zap.String("addr", s.server.Addr))
|
|
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"},
|
|
}))
|
|
|
|
database := data.NewData()
|
|
|
|
// Auth Router
|
|
r.Group(func(r chi.Router) {
|
|
ar := routers.NewAuthRouter(s.cfg, s.oauth)
|
|
ar.Route(r)
|
|
})
|
|
|
|
// 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) {
|
|
token := &oauth2.Token{}
|
|
|
|
if cookie, err := r.Cookie("access_token"); err == nil {
|
|
token.AccessToken = cookie.Value
|
|
}
|
|
|
|
user, 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
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
ctx := context.WithValue(r.Context(), "user", user)
|
|
if user == nil {
|
|
Log.Error("User is not authenticated or timed out", zap.Any("requester", user))
|
|
} else {
|
|
Log.Info("User is authenticated", zap.String("user", user.Name))
|
|
}
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
})
|
|
})
|
|
|
|
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{}
|
|
err := json.NewDecoder(r.Body).Decode(&targetMap)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
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)
|
|
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)
|
|
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
|
|
user := r.Context().Value("user").(*models.User)
|
|
Log.Info("Request merge by", zap.String("user", user.Name))
|
|
|
|
// lookup for python exec
|
|
py_exec, err := exec.LookPath("python")
|
|
if err != nil {
|
|
Log.Fatal("Python error: ", zap.Error(err))
|
|
} else {
|
|
py_exec, err = filepath.Abs(py_exec)
|
|
}
|
|
|
|
Log.Info("Found python exec: ", zap.String("PythonPath", py_exec))
|
|
// target api file
|
|
merge_api, api_err := os.Open("./python_api/merge_recipe.py")
|
|
if api_err != nil {
|
|
Log.Fatal("Merge request failed. Python api error: ", zap.String("ApiErr", api_err.Error()))
|
|
}
|
|
defer merge_api.Close()
|
|
// log.Println("Locate python api", merge_api.Name())
|
|
Log.Info("Locate python api", zap.String("ApiName", merge_api.Name()))
|
|
cmd := exec.Command(py_exec, merge_api.Name(), "merge", master_path, dev_path, output_path, changelog_path, "", user.Name)
|
|
|
|
// log.Println("Run merge command", cmd)
|
|
Log.Info("Merge", zap.String("master", master_path), zap.String("dev", dev_path), zap.String("output", output_path))
|
|
Log.Debug("Run merge command", zap.String("Command", cmd.String()))
|
|
out, err := cmd.CombinedOutput()
|
|
// log.Println(string(out))
|
|
Log.Debug("Merge output", zap.String("Output", string(out)))
|
|
if err != nil {
|
|
// log.Fatalln("Merge request failed. Python merge failed: ", err)
|
|
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"})
|
|
Log.Info("Merge success", zap.String("output", "merge success"))
|
|
})
|
|
|
|
r.Post("/dllog", func(w http.ResponseWriter, r *http.Request) {
|
|
Log.Debug("Request uri = ", zap.String("uri", r.RequestURI))
|
|
|
|
Log.Debug("Query param = ", zap.String("query", r.URL.Query().Get("query")))
|
|
// param
|
|
param := r.URL.Query().Get("query")
|
|
Log.Debug("Param = ", zap.String("param", param))
|
|
|
|
var postRequest map[string]interface{}
|
|
err := json.NewDecoder(r.Body).Decode(&postRequest)
|
|
Log.Debug("Log request: ", zap.String("postRequest", fmt.Sprintf("%+v", postRequest)))
|
|
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
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)
|
|
Log.Warn("Log file name: ", zap.String("filename", log_name))
|
|
|
|
if log_name == "" {
|
|
Log.Fatal("Empty log file name")
|
|
}
|
|
|
|
// log.Println("Log file ext: ", file_ext)
|
|
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
|
|
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 {
|
|
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 {
|
|
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)
|
|
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 {
|
|
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 {
|
|
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))
|
|
}
|
|
|
|
Log.Debug("Split path = ", zap.Any("paths", spl_path))
|
|
|
|
// 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 {
|
|
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))
|
|
|
|
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))
|
|
})
|
|
|
|
r.Get("/get_log_relation", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Python looker
|
|
py_exec, err := exec.LookPath("python")
|
|
if err != nil {
|
|
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 {
|
|
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")
|
|
|
|
Log.Debug("Command: ", zap.String("command", cmd.String()))
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
Log.Debug("Output: ", zap.String("output", string(out)))
|
|
|
|
if err != nil {
|
|
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) {
|
|
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 {
|
|
Log.Fatal("Error while trying to create sheet service: ", zap.Error(err))
|
|
return
|
|
}
|
|
|
|
// Recipe Router
|
|
rr := routers.NewRecipeRouter(database, sheetService)
|
|
rr.Route(r)
|
|
|
|
// Material Router
|
|
mr := routers.NewMaterialRouter(database)
|
|
mr.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(&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
|
|
}
|
|
}
|