This repository was archived by the owner on Feb 3, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 7 files changed +109
-31
lines changed
Expand file tree Collapse file tree 7 files changed +109
-31
lines changed Original file line number Diff line number Diff line change @@ -8,14 +8,14 @@ import (
88func main() {
99log.SetFlags(log.LstdFlags | log.Lshortfile)
1010
11- m, _ := manager.Create()
11+ m := manager.Create()
1212
1313active, err := m.GetActive()
1414if err != nil {
1515log.Panic(err)
1616}
1717
18- for _, container := range active{
18+ for _, container := range active {
1919log.Println(container.ID)
2020}
2121}
Original file line number Diff line number Diff line change @@ -13,5 +13,5 @@ func main() {
1313
1414log.Println("starting server")
1515
16- log.Fatal(server.Run("0.0.0.0", "8080 "))
16+ log.Fatal(server.Run("0.0.0.0", "3000 "))
1717}
Original file line number Diff line number Diff line change @@ -9,13 +9,13 @@ type Core struct {
99client *client.Client
1010}
1111
12- func Create() ( Core, error) {
12+ func Create() Core {
1313cli, err := client.NewEnvClient()
1414if err != nil {
1515log.Panic(err)
1616}
1717
1818return Core{
1919client: cli,
20- }, nil
21- }
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package manager
2+
3+ import (
4+ "github.com/deissh/lambda/pkg/typings"
5+ "github.com/docker/docker/api/types"
6+ "github.com/docker/docker/api/types/container"
7+ "github.com/docker/go-connections/nat"
8+ "golang.org/x/net/context"
9+ )
10+
11+ func (c *Core) Create(info typings.FunctionInfo) error {
12+ // получаем контейнер
13+ //_, err := c.client.ImagePull(
14+ // context.Background(),
15+ // info.Repository.ImageName,
16+ // types.ImagePullOptions{})
17+ //if err != nil {
18+ // log.Println(err)
19+ // return err
20+ //}
21+
22+ // создаем контейнер и настраиваем сеть
23+ //todo: добавить сеть для контейнеров
24+ resp, err := c.client.ContainerCreate(
25+ context.Background(),
26+ &container.Config{
27+ Image: info.Repository.ImageName,
28+ },
29+ &container.HostConfig{
30+ AutoRemove: true,
31+ PortBindings: map[nat.Port][]nat.PortBinding{
32+ "8080/tcp": {
33+ {
34+ HostIP: "",
35+ HostPort: info.Service.Port,
36+ },
37+ },
38+ },
39+ },
40+ nil,
41+ info.Uuid)
42+ if err != nil {
43+ return err
44+ }
45+
46+ // запускаем контейнер
47+ err = c.client.ContainerStart(
48+ context.Background(),
49+ resp.ID,
50+ types.ContainerStartOptions{})
51+ if err != nil {
52+ panic(err)
53+ }
54+
55+ return nil
56+ }
Original file line number Diff line number Diff line change 11package server
22
33import (
4+ "github.com/deissh/lambda/pkg/manager"
45"github.com/deissh/lambda/pkg/typings"
56"github.com/gin-gonic/gin"
7+ "log"
68"net/http"
79)
810
9- func createHandler(c *gin.Context) {
10- var body typings.FunctionInfo
11+ func createHandler(m manager.Core) gin.HandlerFunc {
12+ return func(c *gin.Context) {
13+ var body typings.FunctionInfo
1114
12- if err := c.ShouldBindJSON(&body); err != nil {
13- c.JSON(http.StatusBadRequest, gin.H{
14- "error": err.Error(),
15+ if err := c.ShouldBind(&body); err != nil {
16+ c.JSON(http.StatusBadRequest, gin.H{
17+ "error": err.Error(),
18+ })
19+
20+ log.Println(err.Error())
21+ return
22+ }
23+
24+ if err := m.Create(body); err != nil {
25+ c.JSON(http.StatusBadRequest, gin.H{
26+ "error": err.Error(),
27+ })
28+
29+ return
30+ }
31+
32+ c.JSON(200, gin.H{
33+ "message": "ok",
1534})
16- return
1735}
18-
19- c.JSON(200, gin.H{
20- "message": "ok",
21- })
22- }
36+ }
Original file line number Diff line number Diff line change 11package server
22
33import (
4+ "github.com/deissh/lambda/pkg/manager"
45"github.com/gin-gonic/gin"
56"log"
67)
78
89type core struct {
9- ip string
10- port string
11- router *gin.Engine
10+ ip string
11+ port string
12+ router *gin.Engine
13+ manager manager.Core
1214}
1315
1416func Run(host string, port string) error {
@@ -20,6 +22,7 @@ func Run(host string, port string) error {
2022}
2123
2224func (s core) Start() error {
25+ s.manager = manager.Create()
2326s.router = gin.Default()
2427
2528log.Println("starting server")
@@ -32,10 +35,11 @@ func (s core) Start() error {
3235}
3336
3437func (s core) Routing() {
38+
3539g := s.router.Group("/v1")
3640{
3741g.GET("/")
38- g.POST("/create", createHandler)
39- g.Any ("/function/:uuid")
42+ g.POST("/create", createHandler(s.manager) )
43+ g.GET ("/function/:uuid")
4044}
41- }
45+ }
Original file line number Diff line number Diff line change @@ -3,16 +3,20 @@ package typings
33type FunctionInfo struct {
44Name string `json:"name" binding:"required"`
55Uuid string `json:"uuid" binding:"required"`
6- Runtime struct{
6+ Runtime struct {
77Executor string `json:"executor" binding:"required"`
8- Cmd string `json:"cmd" binding:"required "`
9- Params string `json:"params"`
10- Env []struct{
11- Name string `json:"name"`
8+ Cmd string `json:"cmd"`
9+ Params string `json:"params"`
10+ Env []struct {
11+ Name string `json:"name"`
1212Value string `json:"value"`
1313} `json:"env"`
1414} `json:"runtime" binding:"required"`
15- Repository struct{
16- Url string `json:"url " binding:"required"`
15+ Repository struct {
16+ ImageName string `json:"image " binding:"required"`
1717} `json:"repository" binding:"required"`
18- }
18+ Service struct {
19+ Port string `json:"port" binding:"required"`
20+ Host string `json:"host" binding:"required"`
21+ } `json:"service" binding:"required"`
22+ }
You can’t perform that action at this time.
0 commit comments