本文整理汇总了Golang中github.com/ant0ine/go-json-rest/rest.Request类的典型用法代码示例。如果您正苦于以下问题:Golang Request类的具体用法?Golang Request怎么用?Golang Request使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Request类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: suggestions
func suggestions(w rest.ResponseWriter, r *rest.Request) {
var req data.SuggestionReq
err := r.DecodeJsonPayload(&req)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if req.FallbackLocale == "" {
req.FallbackLocale = data.Locale("en")
}
if req.Locales == nil {
rest.Error(w, "locales required", 400)
return
}
if len(req.Query) > maxQueryLen {
rest.Error(w, fmt.Sprintf("Max query length is %d", maxQueryLen), 400)
return
}
resp, err := glosbe.Suggest(req)
if err != nil {
rest.Error((w), err.Error(), 500)
return
}
w.WriteJson(resp)
}
开发者ID:eicca,项目名称:smart-translate-server,代码行数:27,代码来源:rest.go
示例2: PostUser
func (api *Api) PostUser(w rest.ResponseWriter, r *rest.Request) {
user := User{}
err := r.DecodeJsonPayload(&user)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
lastInsertId, err := api.DB.Query("INSERT INTO " + TABLENAME + "(" + COLNAME + ") OUTPUT Inserted.ID VALUES('" + user.Name + "')")
if err != nil {
fmt.Println(err)
return
}
if lastInsertId.Next() {
var id int
if err := lastInsertId.Scan(&id); err != nil {
log.Fatal(err)
}
user.Id = id
} else {
rest.NotFound(w, r)
return
}
if err := lastInsertId.Err(); err != nil {
log.Fatal(err)
}
w.WriteJson(&user)
}
开发者ID:sohamsankaran,项目名称:go-database-drivers,代码行数:31,代码来源:sqlserver-json-rest.go
示例3: Post
func Post(w rest.ResponseWriter, r *rest.Request) {
host := Host{}
err := r.DecodeJsonPayload(&host)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if host.Id == "" {
rest.Error(w, "id required", 400)
return
}
if host.Address == "" {
rest.Error(w, "address required", 400)
return
}
ra, err := net.ResolveIPAddr("ip4:icmp", host.Address)
if err != nil {
rest.Error(w, err.Error(), 400)
return
}
lock.Lock()
q := make(chan bool, 1) // chan for stop ping
store[host.Id] = &HostStore{
host,
time.Now(),
0.0, 0.0, 0.0, 0.0,
0,
ring.New(DefCircleLen),
q}
go ping(host.Id, ra, time.Second*DefRTT, q, &lock, store)
lock.Unlock()
w.WriteJson(&host)
}
开发者ID:hizel,项目名称:pingd,代码行数:33,代码来源:pingd.go
示例4: StartCluster
// StartCluster starts all nodes in a cluster
func StartCluster(w rest.ResponseWriter, r *rest.Request) {
dbConn, err := util.GetConnection(CLUSTERADMIN_DB)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), 400)
return
}
defer dbConn.Close()
err = secimpl.Authorize(dbConn, r.PathParam("Token"), "perm-cluster")
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusUnauthorized)
return
}
ID := r.PathParam("ID")
if ID == "" {
logit.Error.Println("StartCluster: error cluster ID required")
rest.Error(w, "cluster ID required", http.StatusBadRequest)
return
}
cluster, err := admindb.GetCluster(dbConn, ID)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusBadRequest)
}
//start docker containers
containers, err := admindb.GetAllContainersForCluster(dbConn, cluster.ID)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusBadRequest)
}
i := 0
i = 0
var response swarmapi.DockerStartResponse
for i = range containers {
req := &swarmapi.DockerStartRequest{}
req.ContainerName = containers[i].Name
logit.Info.Println("will attempt to start container " + req.ContainerName)
response, err = swarmapi.DockerStart(req)
if err != nil {
logit.Error.Println("StartCluster: error when trying to start container" + err.Error())
}
logit.Info.Println("StartCluster: started " + response.Output)
i++
}
status := types.SimpleStatus{}
status.Status = "OK"
w.WriteHeader(http.StatusOK)
w.WriteJson(&status)
}
开发者ID:rogervaas,项目名称:crunchy-postgresql-manager,代码行数:61,代码来源:clustermgmt.go
示例5: PostCluster
// PostCluster updates or inserts a new cluster definition
func PostCluster(w rest.ResponseWriter, r *rest.Request) {
dbConn, err := util.GetConnection(CLUSTERADMIN_DB)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), 400)
return
}
defer dbConn.Close()
//logit.Info.Println("PostCluster: in PostCluster")
cluster := types.Cluster{}
err = r.DecodeJsonPayload(&cluster)
if err != nil {
logit.Error.Println("error in decode" + err.Error())
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = secimpl.Authorize(dbConn, cluster.Token, "perm-cluster")
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if cluster.Name == "" {
logit.Error.Println("PostCluster: error in Name")
rest.Error(w, "cluster name required", http.StatusBadRequest)
return
}
//logit.Info.Println("PostCluster: have ID=" + cluster.ID + " Name=" + cluster.Name + " type=" + cluster.ClusterType + " status=" + cluster.Status)
dbcluster := types.Cluster{}
dbcluster.ID = cluster.ID
dbcluster.ProjectID = cluster.ProjectID
dbcluster.Name = cluster.Name
dbcluster.ClusterType = cluster.ClusterType
dbcluster.Status = cluster.Status
dbcluster.Containers = cluster.Containers
if cluster.ID == "" {
strid, err := admindb.InsertCluster(dbConn, dbcluster)
newid := strconv.Itoa(strid)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusBadRequest)
return
}
cluster.ID = newid
} else {
//logit.Info.Println("PostCluster: about to call UpdateCluster")
err2 := admindb.UpdateCluster(dbConn, dbcluster)
if err2 != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
w.WriteJson(&cluster)
}
开发者ID:rogervaas,项目名称:crunchy-postgresql-manager,代码行数:61,代码来源:clustermgmt.go
示例6: HandleCreateEvent
// Require a write key.
// Create a event.
func HandleCreateEvent(w rest.ResponseWriter, req *rest.Request) {
if err := RequireWriteKey(w, req); err != nil {
rest.Error(w, err.Error(), err.(StatusError).Code)
return
}
project := currentProject(req)
event := req.PathParam("event_name")
var data CreateSingleEventParams
var err error
if err = eventData(req, &data); err != nil {
rest.Error(w, err.Error(), http.StatusBadRequest)
return
}
events := make(map[string][]interface{})
events[event] = []interface{}{data}
result, err := createEvent(project, event, data)
if err != nil {
rest.Error(w, err.Error(), http.StatusBadRequest)
} else {
w.WriteJson(result)
}
}
开发者ID:angdev,项目名称:chocolat,代码行数:29,代码来源:events.go
示例7: renewInstance
func (routes *Routes) renewInstance(w rest.ResponseWriter, r *rest.Request) {
iid := r.PathParam(RouteParamInstanceID)
if iid == "" {
routes.logger.WithFields(log.Fields{
"namespace": r.Env["REMOTE_USER"],
"error": "instance id is required",
}).Warn("Failed to renew instance")
i18n.Error(r, w, http.StatusBadRequest, i18n.ErrorInstanceIdentifierMissing)
return
}
catalog := routes.catalog(w, r)
if catalog == nil {
routes.logger.WithFields(log.Fields{
"namespace": r.Env["REMOTE_USER"],
"error": "catalog is nil",
}).Errorf("Failed to renew instance %s", iid)
return
}
if err := catalog.Renew(iid); err != nil {
routes.logger.WithFields(log.Fields{
"namespace": r.Env["REMOTE_USER"],
"error": err,
}).Warnf("Failed to renew instance %s", iid)
i18n.Error(r, w, statusCodeFromError(err), i18n.ErrorInstanceHeartbeatFailed)
return
}
w.WriteHeader(http.StatusOK)
}
开发者ID:charshy,项目名称:registry,代码行数:34,代码来源:instance_handler.go
示例8: GetHealthCheck
func GetHealthCheck(w rest.ResponseWriter, r *rest.Request) {
dbConn, err := util.GetConnection("clusteradmin")
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer dbConn.Close()
err = secimpl.Authorize(dbConn, r.PathParam("Token"), "perm-read")
if err != nil {
logit.Error.Println("validate token error " + err.Error())
rest.Error(w, err.Error(), http.StatusUnauthorized)
return
}
var results []collect.HealthCheck
results, err = collect.GetHealthCheck(dbConn)
if err != nil {
logit.Error.Println(err.Error())
w.WriteJson(&results)
return
}
w.WriteJson(&results)
}
开发者ID:CrunchyData,项目名称:crunchy-postgresql-manager-openshift,代码行数:29,代码来源:healthcheck.go
示例9: PostCreateGame
func (api *ChessApi) PostCreateGame(res rest.ResponseWriter, req *rest.Request) {
user := getUser(req)
type createBody struct {
Color game.Color `json:"Color"`
}
body := new(createBody)
err := req.DecodeJsonPayload(body)
if err != nil || body.Color == "" {
idx := rand.Perm(2)[0]
body.Color = []game.Color{game.White, game.Black}[idx]
}
ok, msg := api.Commands.ExecCommand(
commands.CreateGame, user.Uuid, map[string]interface{}{
"color": body.Color,
},
)
if ok {
res.WriteHeader(http.StatusAccepted)
res.WriteJson("ok")
} else {
res.WriteHeader(http.StatusBadRequest)
res.WriteJson(map[string]string{"error": msg})
}
}
开发者ID:gnidan,项目名称:foodtastechess,代码行数:29,代码来源:api.go
示例10: StatusUpdate
// StatusUpdate called by backup jobs as they execute
func StatusUpdate(w rest.ResponseWriter, r *rest.Request) {
request := TaskStatus{}
err := r.DecodeJsonPayload(&request)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var dbConn *sql.DB
dbConn, err = util.GetConnection(CLUSTERADMIN_DB)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dbConn.Close()
//logit.Info.Println("StatusUpdate called")
err = UpdateStatus(dbConn, &request)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response := StatusUpdateResponse{}
response.Output = "ok"
w.WriteJson(&response)
}
开发者ID:rogervaas,项目名称:crunchy-postgresql-manager,代码行数:34,代码来源:taskserverimpl.go
示例11: PutPlugin
func (this *McPlugins) PutPlugin(w rest.ResponseWriter, req *rest.Request) {
mcPlugin := McPlugin{}
if err := req.DecodeJsonPayload(&mcPlugin); err != nil {
w.WriteJson(utils.Error(http.StatusInternalServerError, err.Error()))
return
}
id := req.PathParam("id")
objId := bson.ObjectIdHex(id)
src := bson.M{"_id": objId}
tar := bson.M{
"name": mcPlugin.Name,
"detail": mcPlugin.Detail,
}
query := func(c *mgo.Collection) error {
return c.Update(src, tar)
}
err := db.Query("plugins", query)
if err != nil {
w.WriteJson(utils.Error(1, err.Error()))
return
}
w.WriteJson(utils.Success(&mcPlugin))
}
开发者ID:AndyEverLie,项目名称:mc_api,代码行数:25,代码来源:mc_plugin.go
示例12: StatusAdd
// StatusAdd called by backup jobs as they execute to write new status info
func StatusAdd(w rest.ResponseWriter, r *rest.Request) {
//logit.Info.Println("StatusAdd called")
request := TaskStatus{}
err := r.DecodeJsonPayload(&request)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dbConn, err := util.GetConnection(CLUSTERADMIN_DB)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dbConn.Close()
var id string
id, err = AddStatus(dbConn, &request)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusInternalServerError)
}
response := StatusAddResponse{}
response.ID = id
w.WriteJson(&response)
}
开发者ID:rogervaas,项目名称:crunchy-postgresql-manager,代码行数:34,代码来源:taskserverimpl.go
示例13: GetServer
// GetServer return a server definition
func GetServer(w rest.ResponseWriter, r *rest.Request) {
dbConn, err := util.GetConnection(CLUSTERADMIN_DB)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), 400)
return
}
defer dbConn.Close()
err = secimpl.Authorize(dbConn, r.PathParam("Token"), "perm-read")
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusUnauthorized)
return
}
ID := r.PathParam("ID")
//logit.Info.Println("in GetServer with ID=" + ID)
//currently no state about a server is maintained other than IP and port number
//which we use for the ID, Name, and IPAddress values
server := types.Server{ID, ID, ID, ID, "", "", ""}
w.WriteJson(&server)
}
开发者ID:rogervaas,项目名称:crunchy-postgresql-manager,代码行数:26,代码来源:server.go
示例14: putDataE
func putDataE(w rest.ResponseWriter, r *rest.Request) {
host := r.PathParam("host")
if host == "" {
rest.Error(w, "host is empty", 400)
return
}
var jsonBody []byte
jsonBody, _ = ioutil.ReadAll(r.Body)
//if err != nil { log.Fatal(err) }
simpleDataFromBody, err2 := simplejson.NewJson(jsonBody)
if err2 != nil {
rest.Error(w, "body (json) is empty", 400)
return
}
if simpleDataFromBody.Get("message").MustString() == "" {
rest.Error(w, "message for "+host+" is not valued", 400)
return
}
if simpleDataFromBody.Get("details").MustString() == "" {
rest.Error(w, "details for "+host+" is not valued", 400)
return
}
CompteurE++
if CompteurE%2 == 0 {
time.Sleep(500 * time.Millisecond)
}
defer r.Body.Close()
w.Header().Set("X-Host", host)
}
开发者ID:jlandure,项目名称:go_server_mock,代码行数:32,代码来源:go_mock.go
示例15: PostTransportTraffic
func PostTransportTraffic(w rest.ResponseWriter, r *rest.Request) {
form := shared.TransportTraffic{}
err := r.DecodeJsonPayload(&form)
if err != nil {
apiutils.WriteRestError(w, apierrors.NewInternalError(err))
return
}
transportTrafficMu.Lock()
defer transportTrafficMu.Unlock()
transportTraffic = form
if lg.V(10) {
if len(transportTrafficLog) == 6 {
lg.Infof("transport traffic: %.0fkb/s %.0fkb/s %.0fkb/s %.0fkb/s %.0fkb/s %.0fkb/s",
(transportTrafficLog[0].Throughput)/1024,
(transportTrafficLog[1].Throughput)/1024,
(transportTrafficLog[2].Throughput)/1024,
(transportTrafficLog[3].Throughput)/1024,
(transportTrafficLog[4].Throughput)/1024,
(transportTrafficLog[5].Throughput)/1024,
)
transportTrafficLog = make([]shared.TransportTraffic, 0)
}
if transportTraffic.Throughput > 1024 {
transportTrafficLog = append(transportTrafficLog, form)
}
}
response := true
w.WriteJson(response)
}
开发者ID:thomasf,项目名称:alkasir,代码行数:29,代码来源:api.go
示例16: DockerStop
func DockerStop(w rest.ResponseWriter, r *rest.Request) {
logit.Info.Println("DockerStop called")
req := DockerStopRequest{}
err := r.DecodeJsonPayload(&req)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
docker, err3 := dockerapi.NewClient("unix://var/run/docker.sock")
if err3 != nil {
logit.Error.Println("can't get connection to docker socket")
rest.Error(w, err3.Error(), http.StatusInternalServerError)
return
}
err3 = docker.StopContainer(req.ContainerName, 10)
if err3 != nil {
logit.Error.Println("can't stop container " + req.ContainerName)
rest.Error(w, err3.Error(), http.StatusInternalServerError)
return
}
var response DockerStopResponse
response.Output = "success"
w.WriteJson(&response)
}
开发者ID:CrunchyData,项目名称:crunchy-postgresql-manager-openshift,代码行数:26,代码来源:docker.go
示例17: StopPgpool
func StopPgpool(w rest.ResponseWriter, r *rest.Request) {
logit.Info.Println("StopPgpool called")
response := StopPgpoolResponse{}
req := StopPgpoolRequest{}
err := r.DecodeJsonPayload(&req)
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var cmd *exec.Cmd
cmd = exec.Command("stop-pgpool.sh", req.Path)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
logit.Error.Println(err.Error())
rest.Error(w, err.Error(), 400)
return
}
response.Output = out.String()
response.Status = "OK"
w.WriteJson(&response)
}
开发者ID:CrunchyData,项目名称:crunchy-postgresql-manager-openshift,代码行数:29,代码来源:pgpool.go
示例18: Register
func Register(w rest.ResponseWriter, r *rest.Request) {
var form struct {
Id string `json:"username"`
Pass string `json:"password"`
}
r.DecodeJsonPayload(&form)
if form.Pass == "" {
rest.Error(w, "Invalid input", 400)
return
}
res, err := userRepository.GetUserById(form.Id)
if res != "" {
rest.Error(w, "[username]exists", 400)
return
}
user := &domain.User{Id: form.Id, Pass: domain.HashPassword(form.Pass)}
bytes, _ := json.Marshal(user)
userRepository.SaveUserById(form.Id, string(bytes))
res, err = userRepository.GetUserById(form.Id)
log.Println(res, err)
w.WriteJson(map[string]string{"Status": "OK"})
}
开发者ID:unitrans,项目名称:unitrans,代码行数:25,代码来源:web_api.go
示例19: resolveHandler
func resolveHandler(w rest.ResponseWriter, req *rest.Request) {
domain := req.PathParam("domain")
ipaddr, err := resolveFromRedis(domain)
if err == nil {
resp := Resp{
Code: 0,
Domain: domain,
IpAddr: ipaddr,
}
w.WriteJson(&resp)
}
ipaddr, errs := resolveFromDNS(domain)
if errs != nil {
resp := Resp{
Code: 1,
Domain: domain,
IpAddr: "resolve error, maybe this domain has not resolved",
}
w.WriteJson(&resp)
} else {
resp := Resp{
Code: 0,
Domain: domain,
IpAddr: ipaddr,
}
cacheRespToRedis(domain, ipaddr)
w.WriteJson(&resp)
}
}
开发者ID:CafeChair,项目名称:GoHttpDNS,代码行数:29,代码来源:handler.go
示例20: Translate
func Translate(w rest.ResponseWriter, r *rest.Request) {
request := &RequestObject{}
r.DecodeJsonPayload(request)
c := translator.Translate(request.Text, request.Lang)
w.WriteJson(c)
}
开发者ID:unitrans,项目名称:unitrans,代码行数:7,代码来源:web_api.go
注:本文中的github.com/ant0ine/go-json-rest/rest.Request类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论