This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
/
instance.go
109 lines (92 loc) · 2.66 KB
/
instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
*
* In The Name of God
*
* +===============================================
* | Author: Parham Alvani <parham.alvani@gmail.com>
* |
* | Creation Date: 16-12-2018
* |
* | File Name: instance.go
* +===============================================
*/
package actions
import (
"fmt"
"net/http"
"time"
"github.com/I1820/lg/models"
"github.com/gobuffalo/buffalo"
)
// instance creation request
type instanceReq struct {
Name string
models.Config
}
// list of the running instances
var instances map[string]*models.Instance = make(map[string]*models.Instance)
// InstancesResource manages instances of load generators.
type InstancesResource struct {
buffalo.Resource
}
// List returns all running instances This function is mapped
// to the path GET /instances
func (v InstancesResource) List(c buffalo.Context) error {
return c.Render(http.StatusOK, r.JSON(instances))
}
// Create runs new generator instance. This function is mapped
// to the path POST /instances
func (v InstancesResource) Create(c buffalo.Context) error {
var req instanceReq
if err := c.Bind(&req); err != nil {
return c.Error(http.StatusBadRequest, err)
}
var config models.Config = req.Config
// check for duplicate name
if _, ok := instances[req.Name]; ok {
return c.Error(http.StatusBadRequest, fmt.Errorf("Duplicate name"))
}
rate, err := time.ParseDuration(c.Param("rate"))
if err != nil {
rate = 1 * time.Millisecond
}
destination := c.Param("destination")
if destination == "" {
destination = "mqtt://127.0.0.1:1883"
}
i, err := models.NewInstance(config, rate, destination)
if err != nil {
return c.Error(http.StatusInternalServerError, err)
}
i.Run()
go func() {
for {
time.Sleep(1 * time.Second)
socket.BroadcastTo("I1820", req.Name, i.R.Count())
}
}()
instances[req.Name] = i
return c.Render(http.StatusOK, r.JSON(true))
}
// Show shows the detail of given instance. This function is mapped
// to the path GET /instances/{instance_id}
func (v InstancesResource) Show(c buffalo.Context) error {
id := c.Param("instance_id")
i, ok := instances[id]
if !ok {
return c.Error(http.StatusNotFound, fmt.Errorf("There is no instnace with name %s", id))
}
return c.Render(http.StatusOK, r.JSON(i.R.Count()))
}
// Destroy stops given instance and removes it from the instances list.
// This function is mapped to the path DELETE /instances/{instance_id}
func (v InstancesResource) Destroy(c buffalo.Context) error {
id := c.Param("instance_id")
i, ok := instances[id]
if !ok {
return c.Error(http.StatusNotFound, fmt.Errorf("There is no instnace with name %s", id))
}
i.Stop()
delete(instances, id)
return c.Render(http.StatusOK, r.JSON(true))
}