-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.go
More file actions
37 lines (32 loc) · 919 Bytes
/
Copy pathhandler.go
File metadata and controls
37 lines (32 loc) · 919 Bytes
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
// Copyright 2016 Eleme Inc. All rights reserved.
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"github.com/Eagle-X/witch/system"
"github.com/martini-contrib/render"
)
var (
// ErrServerError is internal server error.
ErrServerError = errors.New("Internal Server Error")
// ErrBadRequest is bad request error.
ErrBadRequest = errors.New("Bad Request")
)
func sysAction(control *system.Controller, req *http.Request, r render.Render) {
bs, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("Read request body error: %s", err)
r.JSON(http.StatusInternalServerError, ErrServerError)
return
}
log.Printf("Request action: %s", bs)
action := &system.Action{}
if err := json.Unmarshal(bs, action); err != nil {
log.Printf("Invalid action format: %s", err)
r.JSON(http.StatusBadRequest, ErrBadRequest)
return
}
r.JSON(http.StatusOK, control.Handle(action))
}