2023-05-22 00:54:49 +09:00
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
2023-06-27 00:03:43 +09:00
|
|
|
"encoding/json"
|
2023-05-23 13:54:25 +09:00
|
|
|
"fmt"
|
2023-05-22 00:54:49 +09:00
|
|
|
"io"
|
|
|
|
|
"net/http"
|
2023-06-27 10:06:26 +09:00
|
|
|
"net/url"
|
2023-05-23 13:54:25 +09:00
|
|
|
"os"
|
2023-05-22 10:53:58 +09:00
|
|
|
"path"
|
2023-05-22 00:54:49 +09:00
|
|
|
"reflect"
|
|
|
|
|
"runtime/debug"
|
|
|
|
|
"strings"
|
2023-05-22 02:13:03 +09:00
|
|
|
|
2023-06-27 00:03:43 +09:00
|
|
|
"repositories.action2quare.com/ayo/gocommon/flagx"
|
2023-06-14 00:13:51 +09:00
|
|
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
2023-05-22 00:54:49 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
defaultMaxMemory = 32 << 10 // 32 KB
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type HoustonServerWithHandler interface {
|
|
|
|
|
HoustonServer
|
|
|
|
|
RegisterHandlers(serveMux *http.ServeMux, prefix string) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type houstonHandler struct {
|
|
|
|
|
HoustonServer
|
2023-06-13 11:04:30 +09:00
|
|
|
methods map[string]reflect.Method
|
|
|
|
|
deployPath string
|
|
|
|
|
downloadPath string
|
2023-05-22 00:54:49 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewHoustonHandler() HoustonServerWithHandler {
|
|
|
|
|
var tmp *houstonHandler
|
|
|
|
|
|
|
|
|
|
methods := make(map[string]reflect.Method)
|
|
|
|
|
tp := reflect.TypeOf(tmp)
|
|
|
|
|
for i := 0; i < tp.NumMethod(); i++ {
|
|
|
|
|
method := tp.Method(i)
|
|
|
|
|
methods[strings.ToLower(method.Name)] = method
|
|
|
|
|
}
|
|
|
|
|
return &houstonHandler{
|
|
|
|
|
HoustonServer: NewServer(),
|
|
|
|
|
methods: methods,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *houstonHandler) RegisterHandlers(serveMux *http.ServeMux, prefix string) error {
|
2023-06-22 17:15:56 +09:00
|
|
|
config := loadServerConfig()
|
|
|
|
|
storagePath := config.StorageRoot
|
|
|
|
|
h.deployPath = path.Join(storagePath, sub_folder_name_deploys)
|
|
|
|
|
h.downloadPath = path.Join(storagePath, sub_folder_name_downloads)
|
2023-06-13 10:10:30 +09:00
|
|
|
|
2023-06-13 11:18:22 +09:00
|
|
|
if err := os.MkdirAll(h.deployPath, 0775); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := os.MkdirAll(h.downloadPath, 0775); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-14 00:13:51 +09:00
|
|
|
logger.Printf("houstonHandler registed. deployPath : %s, downloadPath : %s", h.deployPath, h.downloadPath)
|
2023-06-13 10:10:30 +09:00
|
|
|
|
2023-06-10 16:27:46 +09:00
|
|
|
if len(prefix) > 0 {
|
|
|
|
|
prefix = "/" + prefix
|
|
|
|
|
}
|
|
|
|
|
serveMux.Handle(prefix, h)
|
2023-05-22 00:54:49 +09:00
|
|
|
|
2023-06-13 11:04:30 +09:00
|
|
|
fsx := http.FileServer(http.Dir(h.deployPath))
|
2023-06-22 20:39:38 +09:00
|
|
|
serveMux.Handle(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys), http.StripPrefix(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_deploys), fsx))
|
2023-05-23 13:54:25 +09:00
|
|
|
|
2023-06-13 11:04:30 +09:00
|
|
|
ufsx := http.FileServer(http.Dir(h.downloadPath))
|
2023-06-22 20:39:38 +09:00
|
|
|
serveMux.Handle(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_downloads), http.StripPrefix(fmt.Sprintf("%s/%s/", prefix, sub_folder_name_downloads), ufsx))
|
2023-05-23 13:54:25 +09:00
|
|
|
|
2023-06-10 16:27:46 +09:00
|
|
|
serveMux.HandleFunc(fmt.Sprintf("%s/upload", prefix), func(w http.ResponseWriter, r *http.Request) {
|
2023-05-23 13:54:25 +09:00
|
|
|
defer func() {
|
|
|
|
|
s := recover()
|
|
|
|
|
if s != nil {
|
2023-06-14 00:13:51 +09:00
|
|
|
logger.Println(s)
|
2023-05-23 13:54:25 +09:00
|
|
|
debug.PrintStack()
|
|
|
|
|
}
|
|
|
|
|
io.Copy(io.Discard, r.Body)
|
|
|
|
|
r.Body.Close()
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
name := r.Header.Get("Houston-Service-Name")
|
|
|
|
|
version := r.Header.Get("Houston-Service-Version")
|
|
|
|
|
filename := r.Header.Get("Houston-Service-Filename")
|
2023-06-13 11:04:30 +09:00
|
|
|
dir := path.Join(h.downloadPath, name, version)
|
2023-06-13 10:10:30 +09:00
|
|
|
if err := os.MkdirAll(dir, 0775); err == nil {
|
2023-05-23 13:54:25 +09:00
|
|
|
file, _ := os.Create(path.Join(dir, filename))
|
|
|
|
|
if file != nil {
|
|
|
|
|
defer file.Close()
|
|
|
|
|
if _, err = io.Copy(file, r.Body); err != nil {
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2023-05-22 00:54:49 +09:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 00:03:43 +09:00
|
|
|
var noauth = flagx.Bool("noauth", false, "")
|
|
|
|
|
|
2023-06-27 10:06:26 +09:00
|
|
|
type respWriteTracker struct {
|
|
|
|
|
inner http.ResponseWriter
|
|
|
|
|
reqUrlValues url.Values
|
|
|
|
|
body []byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rt *respWriteTracker) Header() http.Header {
|
|
|
|
|
return rt.inner.Header()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rt *respWriteTracker) Write(bt []byte) (int, error) {
|
|
|
|
|
rt.body = append(rt.body, bt...)
|
|
|
|
|
return rt.inner.Write(bt)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (rt *respWriteTracker) WriteHeader(statusCode int) {
|
|
|
|
|
if statusCode != http.StatusOK {
|
|
|
|
|
logger.Println()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var logApiCallFlag = flagx.Bool("logapicall", false, "")
|
|
|
|
|
|
2023-05-22 00:54:49 +09:00
|
|
|
func (h *houstonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
defer func() {
|
|
|
|
|
s := recover()
|
|
|
|
|
if s != nil {
|
2023-06-14 00:13:51 +09:00
|
|
|
logger.Println(s)
|
2023-05-22 00:54:49 +09:00
|
|
|
debug.PrintStack()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
io.Copy(io.Discard, r.Body)
|
|
|
|
|
r.Body.Close()
|
|
|
|
|
}()
|
|
|
|
|
|
2023-06-27 00:03:43 +09:00
|
|
|
var userinfo map[string]any
|
|
|
|
|
if !*noauth {
|
|
|
|
|
authheader := r.Header.Get("Authorization")
|
|
|
|
|
if len(authheader) == 0 {
|
|
|
|
|
logger.Println("Authorization header is not valid :", authheader)
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "https://graph.microsoft.com/oidc/userinfo", nil)
|
|
|
|
|
req.Header.Add("Authorization", authheader)
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Println("graph microsoft api call failed :", err)
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
raw, _ := io.ReadAll(resp.Body)
|
|
|
|
|
if err = json.Unmarshal(raw, &userinfo); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, expired := userinfo["error"]; expired {
|
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 13:54:25 +09:00
|
|
|
var operation string
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
|
operation = r.FormValue("operation")
|
|
|
|
|
} else {
|
|
|
|
|
operation = r.URL.Query().Get("operation")
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-22 00:54:49 +09:00
|
|
|
if len(operation) == 0 {
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
method, ok := h.methods[strings.ToLower(operation)]
|
|
|
|
|
if !ok {
|
|
|
|
|
// 없는 operation
|
2023-06-14 00:13:51 +09:00
|
|
|
logger.Println("fail to call api. operation is not valid :", operation)
|
2023-05-22 00:54:49 +09:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 10:06:26 +09:00
|
|
|
if *logApiCallFlag {
|
|
|
|
|
var urlvalues url.Values
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
|
urlvalues = r.Form
|
|
|
|
|
} else {
|
|
|
|
|
urlvalues = r.URL.Query()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tracker := &respWriteTracker{
|
|
|
|
|
inner: w,
|
|
|
|
|
reqUrlValues: urlvalues,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
logger.Println("api called :", userinfo["email"], urlvalues)
|
|
|
|
|
logger.Println("-->", string(tracker.body))
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
w = http.ResponseWriter(tracker)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-22 00:54:49 +09:00
|
|
|
args := []reflect.Value{
|
|
|
|
|
reflect.ValueOf(h),
|
|
|
|
|
reflect.ValueOf(w),
|
|
|
|
|
reflect.ValueOf(r),
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-14 15:51:54 +09:00
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
|
|
|
|
2023-05-22 00:54:49 +09:00
|
|
|
method.Func.Call(args)
|
|
|
|
|
}
|