From 5f1b23ed80dfb9ba200d765b8aa27a9cf9dd3248 Mon Sep 17 00:00:00 2001 From: mountain Date: Tue, 13 Jun 2023 11:04:30 +0900 Subject: [PATCH] =?UTF-8?q?deploy,=20download=20=EA=B2=BD=EB=A1=9C=20?= =?UTF-8?q?=EC=A7=80=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/http_api.go | 18 +++++++++--------- server/http_handler.go | 17 +++++++++-------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/server/http_api.go b/server/http_api.go index 3a58cb8..9a3c68a 100644 --- a/server/http_api.go +++ b/server/http_api.go @@ -26,7 +26,7 @@ func (h *houstonHandler) GetAgents(w http.ResponseWriter, r *http.Request) { } func (h *houstonHandler) GetDeploySources(w http.ResponseWriter, r *http.Request) { - files, err := os.ReadDir("deploys") + files, err := os.ReadDir(h.deployPath) if err != nil { if errors.Is(err, fs.ErrNotExist) { err = os.MkdirAll("deploys", 0775) @@ -41,7 +41,7 @@ func (h *houstonHandler) GetDeploySources(w http.ResponseWriter, r *http.Request getVersions := func(name string) []string { var out []string - files, _ := os.ReadDir(path.Join("deploys", name)) + files, _ := os.ReadDir(path.Join(h.deployPath, name)) for _, fd := range files { if fd.IsDir() { out = append(out, fd.Name()) @@ -90,10 +90,10 @@ func (h *houstonHandler) UploadDeploySource(w http.ResponseWriter, r *http.Reque var filename string if version == "config" { - filename = path.Join("deploys", name, version, "config.json") + filename = path.Join(h.deployPath, name, version, "config.json") } else { // deploys 폴더는 파일시스템 서비스이므로 다운로드 가능 - filename = path.Join("deploys", name, version, name+ext) + filename = path.Join(h.deployPath, name, version, name+ext) } if err = os.MkdirAll(path.Dir(filename), 0775); err != nil { @@ -125,7 +125,7 @@ func (h *houstonHandler) DeleteDeploySource(w http.ResponseWriter, r *http.Reque } // deploys 폴더는 파일시스템 서비스이므로 다운로드 가능 - targetpath := path.Join("deploys", name, version) + targetpath := path.Join(h.deployPath, name, version) if err := os.RemoveAll(targetpath); err != nil { logger.Println("deleteDeploySource failed :", err) w.WriteHeader(http.StatusInternalServerError) @@ -158,7 +158,7 @@ func (h *houstonHandler) Deploy(w http.ResponseWriter, r *http.Request) { return } - relPath := path.Join("deploys", name, version) + relPath := path.Join(h.deployPath, name, version) files, err := os.ReadDir(relPath) if err != nil { logger.Error(err) @@ -189,7 +189,7 @@ func (h *houstonHandler) Deploy(w http.ResponseWriter, r *http.Request) { Name: name, Version: version, Url: path.Join(relPath, latestFilename), - Config: path.Join("deploys", name, "config", "config.json"), + Config: path.Join(h.deployPath, name, "config", "config.json"), }, targets, )) @@ -346,10 +346,10 @@ func (h *houstonHandler) GetLogFileLinks(w http.ResponseWriter, r *http.Request) } if version == "latest" { - version, _ = shared.FindLastestVersion(path.Join("downloads", name)) + version, _ = shared.FindLastestVersion(path.Join(h.downloadPath, name)) } - root := path.Join("downloads", name, version) + root := path.Join(h.downloadPath, name, version) logfiles, err := os.ReadDir(root) if err != nil { w.WriteHeader(http.StatusBadRequest) diff --git a/server/http_handler.go b/server/http_handler.go index 0b8935e..ffba150 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -24,7 +24,9 @@ type HoustonServerWithHandler interface { type houstonHandler struct { HoustonServer - methods map[string]reflect.Method + methods map[string]reflect.Method + deployPath string + downloadPath string } func NewHoustonHandler() HoustonServerWithHandler { @@ -44,21 +46,20 @@ func NewHoustonHandler() HoustonServerWithHandler { func (h *houstonHandler) RegisterHandlers(serveMux *http.ServeMux, prefix string) error { storagePath := loadConfig().StoragePath + h.deployPath = path.Join(storagePath, "deploys") + h.downloadPath = path.Join(storagePath, "downloads") - logger.Println("houstonHandler registed. storage_path :", storagePath) - if len(storagePath) > 0 { - storagePath = storagePath + "/" - } + logger.Printf("houstonHandler registed. deployPath : %s, downloadPath : %s", h.deployPath, h.downloadPath) if len(prefix) > 0 { prefix = "/" + prefix } serveMux.Handle(prefix, h) - fsx := http.FileServer(http.Dir(storagePath + "deploys")) + fsx := http.FileServer(http.Dir(h.deployPath)) serveMux.Handle(fmt.Sprintf("%s/deploys/", prefix), http.StripPrefix(fmt.Sprintf("%s/deploys/", prefix), fsx)) - ufsx := http.FileServer(http.Dir(storagePath + "downloads")) + ufsx := http.FileServer(http.Dir(h.downloadPath)) serveMux.Handle(fmt.Sprintf("%s/downloads/", prefix), http.StripPrefix(fmt.Sprintf("%s/downloads/", prefix), ufsx)) serveMux.HandleFunc(fmt.Sprintf("%s/upload", prefix), func(w http.ResponseWriter, r *http.Request) { @@ -75,7 +76,7 @@ func (h *houstonHandler) RegisterHandlers(serveMux *http.ServeMux, prefix string name := r.Header.Get("Houston-Service-Name") version := r.Header.Get("Houston-Service-Version") filename := r.Header.Get("Houston-Service-Filename") - dir := fmt.Sprintf(storagePath+"downloads/%s/%s", name, version) + dir := path.Join(h.downloadPath, name, version) if err := os.MkdirAll(dir, 0775); err == nil { file, _ := os.Create(path.Join(dir, filename)) if file != nil {