config handler 추가

This commit is contained in:
2024-06-05 13:41:43 +09:00
parent bad563dce7
commit d546f2340d

View File

@ -1,10 +1,13 @@
package server
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"reflect"
@ -56,15 +59,62 @@ func (h *houstonHandler) RegisterHandlers(serveMux *http.ServeMux, prefix string
return err
}
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(h.deployPath))
serveMux.Handle(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys), http.StripPrefix(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys), fsx))
deployPrefix := fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys)
logger.Printf("houstonHandler registed. deployPath : %s -> %s", fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys), h.deployPath)
serveMux.HandleFunc(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys), func(w http.ResponseWriter, r *http.Request) {
p := strings.TrimPrefix(r.URL.Path, deployPrefix)
rp := strings.TrimPrefix(r.URL.RawPath, deployPrefix)
h := md5.New()
src := strings.TrimLeft(r.URL.Path, fmt.Sprintf("/%s/", prefix))
h.Write([]byte(src))
at := hex.EncodeToString(h.Sum(nil))
if len(p) < len(r.URL.Path) && (r.URL.RawPath == "" || len(rp) < len(r.URL.RawPath)) && at == r.Header.Get("As-X-UrlHash") {
r2 := new(http.Request)
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
r2.URL.Path = p
r2.URL.RawPath = rp
fsx.ServeHTTP(w, r2)
} else {
http.NotFound(w, r)
}
})
// config는 접근하기 편하게 단축 경로 제공
serveMux.HandleFunc("/config/", func(w http.ResponseWriter, r *http.Request) {
logger.Println("config url.path :", r.URL.Path)
h := md5.New()
h.Write([]byte(r.URL.Path))
at := hex.EncodeToString(h.Sum(nil))
hash := r.Header.Get("As-X-UrlHash")
logger.Println("config at = hash :", at, hash)
if at == hash {
urlpath := strings.TrimPrefix(r.URL.Path, "/config/")
dir := path.Dir(urlpath)
file := path.Base(urlpath)
dest := fmt.Sprintf("%s/config/%s", dir, file)
logger.Println("config dest :", dest)
r2 := new(http.Request)
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
r2.URL.Path = dest
r2.URL.RawPath = dest
fsx.ServeHTTP(w, r2)
} else {
http.NotFound(w, r)
}
})
ufsx := http.FileServer(http.Dir(h.downloadPath))
serveMux.Handle(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_downloads), http.StripPrefix(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_downloads), ufsx))