web api 추가

This commit is contained in:
2023-05-23 10:58:15 +09:00
parent 170c110b86
commit 54063a19c4
2 changed files with 130 additions and 14 deletions

View File

@ -1,8 +1,10 @@
package server
import (
"fmt"
"io"
"net/http"
"os"
"path"
"reflect"
"runtime/debug"
@ -43,6 +45,46 @@ func NewHoustonHandler() HoustonServerWithHandler {
func (h *houstonHandler) RegisterHandlers(serveMux *http.ServeMux, prefix string) error {
serveMux.Handle("/"+path.Join(prefix, "houston"), h)
dfsx := http.FileServer(http.Dir("./deploys"))
serveMux.Handle(
"/"+path.Join(prefix, "deploys", "/"),
http.StripPrefix(fmt.Sprintf("/%s/deploys", prefix), dfsx))
ufsx := http.FileServer(http.Dir("./downloads"))
serveMux.Handle(
"/"+path.Join(prefix, "downloads", "/"),
http.StripPrefix(fmt.Sprintf("/%s/downloads", prefix), ufsx))
serveMux.HandleFunc("/"+path.Join(prefix, "upload"), func(w http.ResponseWriter, r *http.Request) {
defer func() {
s := recover()
if s != nil {
logger.Println(s)
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")
dir := fmt.Sprintf("./downloads/%s/%s", name, version)
if err := os.MkdirAll(dir, os.ModePerm); err == nil {
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)
}
})
return nil
}
@ -60,7 +102,13 @@ func (h *houstonHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Body.Close()
}()
operation := r.FormValue("operation")
var operation string
if r.Method == "POST" {
operation = r.FormValue("operation")
} else {
operation = r.URL.Query().Get("operation")
}
if len(operation) == 0 {
w.WriteHeader(http.StatusBadRequest)
return