commit 69f9a708d343274cc656f52ccadd7b4e50172449 Author: Godopu Date: Mon Nov 8 10:47:27 2021 +0900 commit 2021-11-08]10:47:23 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d298be1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +public/ \ No newline at end of file diff --git a/containermgmt/manager.go b/containermgmt/manager.go new file mode 100644 index 0000000..b71172c --- /dev/null +++ b/containermgmt/manager.go @@ -0,0 +1,74 @@ +package containermgnt + +import ( + "context" + "encoding/json" + "fmt" + "log" + "os/exec" + "strings" +) + +type Container struct { + Image string `json:"image"` + Name string `json:"name"` +} + +type ContainerReturnKey struct{} + +var ReturnKey = ContainerReturnKey{} + +func CreateContainer(ctx context.Context, cont *Container) context.Context { + + fmt.Println("container : ", *cont) + args := strings.Split(fmt.Sprintf("container\\run\\-d\\--name\\%s\\%s", cont.Name, cont.Image), "\\") + fmt.Println(args) + bout, err := exec.Command("docker", args...).Output() + if err != nil { + log.Fatalln(err) + } + + ctx = context.WithValue(ctx, ReturnKey, bout) + + return ctx +} + +func GetContainers(ctx context.Context) context.Context { + // cmd := exec.Command("firefox") + // err := cmd.Run() + + cmd := strings.Split("container\\ls\\--format\\'{{.Image}} {{.Names}}'\\-a", "\\") + bout, err := exec.Command("docker", cmd...).Output() + if err != nil { + log.Fatalln(err) + } + + sout := strings.Split(string(bout), "\n") + + var list []Container + for _, e := range sout { + l := strings.Split(e, " ") + + if len(l) < 2 { + continue + } + + container := Container{ + Image: l[0], + Name: l[1], + } + + list = append(list, container) + } + + b, err := json.Marshal(list) + if err != nil { + log.Fatalln("JSON Marshal error!!") + } + + fmt.Println(string(b)) + + ctx = context.WithValue(ctx, ReturnKey, b) + + return ctx +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..eab91f8 --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module etrismartfarmpoc + +go 1.17 + +require ( + github.com/gorilla/mux v1.8.0 + github.com/unrolled/render v1.4.0 + github.com/urfave/negroni v1.0.0 +) + +require ( + github.com/fsnotify/fsnotify v1.4.9 // indirect + golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2bde986 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/unrolled/render v1.4.0 h1:p73obhpsXuE3paXOtcuXTBKgBJpLCfmABnsUiO35x+Q= +github.com/unrolled/render v1.4.0/go.mod h1:cK4RSTTVdND5j9EYEc0LAMOvdG11JeiKjyjfyZRvV2w= +github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc= +github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea h1:+WiDlPBBaO+h9vPNZi8uJ3k4BkKQB7Iow3aqwHVA5hI= +golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go new file mode 100644 index 0000000..5a902d1 --- /dev/null +++ b/main.go @@ -0,0 +1,11 @@ +package main + +import ( + "etrismartfarmpoc/router" + "net/http" +) + +func main() { + http.ListenAndServe(":3000", router.NewRouter()) + // fmt.Println(string(out)) +} diff --git a/router/route.go b/router/route.go new file mode 100644 index 0000000..5e7ffe4 --- /dev/null +++ b/router/route.go @@ -0,0 +1,56 @@ +package router + +import ( + "bytes" + "context" + "encoding/json" + containermgnt "etrismartfarmpoc/containermgmt" + "fmt" + "net/http" + + "github.com/gorilla/mux" + "github.com/urfave/negroni" +) + +// var rd *render.Render = render.New() + +func NewRouter() http.Handler { + mux := mux.NewRouter() + + mux.HandleFunc("/service", GetServiceList).Methods("GET") + mux.HandleFunc("/extension", PostService).Methods("POST") + + // mux.Handle("/", http.FileServer(http.Dir("public"))) + n := negroni.Classic() // 파일 서버 및 로그기능을 제공함 + n.UseHandler(mux) + + return n +} + +func GetServiceList(w http.ResponseWriter, r *http.Request) { + result := containermgnt.GetContainers(context.Background()) + b := result.Value(containermgnt.ReturnKey).([]byte) + w.Write(b) +} + +func PostService(w http.ResponseWriter, r *http.Request) { + request := containermgnt.Container{ + Image: "hello-world", + Name: "TempContainer", + } + + s, err := json.Marshal(request) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) + } + + fmt.Println(s) + cont := new(containermgnt.Container) + decoder := json.NewDecoder(bytes.NewReader(s)) + decoder.Decode(cont) + + result := containermgnt.CreateContainer(context.Background(), cont) + b := result.Value(containermgnt.ReturnKey).([]byte) + w.Write(b) +} diff --git a/router/route_test.go b/router/route_test.go new file mode 100644 index 0000000..60e2650 --- /dev/null +++ b/router/route_test.go @@ -0,0 +1 @@ +package router_test diff --git a/server b/server new file mode 100755 index 0000000..4ba242b Binary files /dev/null and b/server differ