-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathports.go
43 lines (37 loc) · 1.08 KB
/
ports.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
package apiserver
import (
"fmt"
"net/http"
"github.com/laincloud/deployd/engine"
"github.com/mijia/sweb/form"
"github.com/mijia/sweb/log"
"github.com/mijia/sweb/server"
"golang.org/x/net/context"
)
type RestfulPorts struct {
server.BaseResource
}
type Ports struct {
Ports []int
}
func (rn RestfulPorts) Get(ctx context.Context, r *http.Request) (int, interface{}) {
return http.StatusOK, engine.FetchAllPortsInfo()
}
func (rn RestfulPorts) Post(ctx context.Context, r *http.Request) (int, interface{}) {
options := []string{"validate"}
cmd := form.ParamStringOptions(r, "cmd", options, "noop")
switch cmd {
case "validate":
var ports Ports
if err := form.ParamBodyJson(r, &ports); err != nil {
log.Warnf("Failed to decode valiad ports, %s", err)
return http.StatusBadRequest, fmt.Sprintf("Invalid ports params format: %s", err)
}
occs := engine.OccupiedPorts(ports.Ports...)
if len(occs) == 0 {
return http.StatusOK, nil
}
return http.StatusBadRequest, fmt.Sprintf("Conflicted ports: %v", occs)
}
return http.StatusBadRequest, fmt.Sprintf("Unknown request!")
}