commit 2021-11-08]10:47:23

This commit is contained in:
Godopu 2021-11-08 10:47:27 +09:00
commit 69f9a708d3
8 changed files with 168 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
public/

74
containermgmt/manager.go Normal file
View File

@ -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
}

14
go.mod Normal file
View File

@ -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
)

11
go.sum Normal file
View File

@ -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=

11
main.go Normal file
View File

@ -0,0 +1,11 @@
package main
import (
"etrismartfarmpoc/router"
"net/http"
)
func main() {
http.ListenAndServe(":3000", router.NewRouter())
// fmt.Println(string(out))
}

56
router/route.go Normal file
View File

@ -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)
}

1
router/route_test.go Normal file
View File

@ -0,0 +1 @@
package router_test

BIN
server Executable file

Binary file not shown.