本文整理匯總了Golang中httprouter.Params類的典型用法代碼示例。如果您正苦於以下問題:Golang Params類的具體用法?Golang Params怎麽用?Golang Params使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Params類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: upd_K
func upd_K(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
key, _ := strconv.Atoi(p.ByName("key"))
value := p.ByName("value")
data[key] = value
rw.WriteHeader(http.StatusCreated)
fmt.Fprint(rw, "200")
}
示例2: getTrip
func getTrip(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
tripId := p.ByName("trip_id")
session, err := mgo.Dial("mongodb://sejal:[email protected]:45064/planner")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("planner").C("trip")
trip := Trip{}
err = c.Find(bson.M{"id": tripId}).One(&trip)
if err != nil {
fmt.Fprint(rw, "Data not found")
return
}
createTripResponse := CreateTripResponse{}
createTripResponse.Id = trip.Id
createTripResponse.Status = trip.Status
createTripResponse.Starting_from_location_id = trip.Starting_from_location_id
createTripResponse.Best_route_location_ids = trip.Best_route_location_ids
createTripResponse.Total_uber_costs = trip.Total_uber_costs
createTripResponse.Total_uber_duration = trip.Total_uber_duration
createTripResponse.Total_distance = trip.Total_distance
getTripJson, err := json.Marshal(createTripResponse)
if err != nil {
log.Fatal(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusCreated)
fmt.Fprint(rw, string(getTripJson))
}
示例3: deleteLocation
func deleteLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
id := bson.ObjectIdHex(p.ByName("locationID"))
err := collection.RemoveId(id)
if err != nil {
fmt.Printf("got an error deleting a doc %v\n")
}
rw.WriteHeader(200)
}
示例4: PutKey
//PutKey to insert value in a map
func (uc MyController) PutKey(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("id")
val := p.ByName("value")
eid, _ := strconv.Atoi(id)
myMap[eid] = val
fmt.Println(myMap)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
}
示例5: putLocation
func putLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
locationId := p.ByName("location_id")
updateLocationRequest := new(UpdateLocationRequest)
decoder := json.NewDecoder(req.Body)
error := decoder.Decode(&updateLocationRequest)
if error != nil {
log.Println(error.Error())
http.Error(rw, error.Error(), http.StatusInternalServerError)
return
}
location := Location{}
address := Address{updateLocationRequest.Address, updateLocationRequest.City, updateLocationRequest.State, updateLocationRequest.Zip, location}
session, err := mgo.Dial("mongodb://sejal:[email protected]:45064/planner")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("planner").C("user")
result := User{}
err = c.Find(bson.M{"userid": locationId}).One(&result)
if err != nil {
log.Fatal(err)
}
userName := result.Name
user := User{locationId, userName, address}
getCoordinates(&user.UserAddress)
colQuerier := bson.M{"userid": locationId}
change := bson.M{"$set": bson.M{"useraddress": user.UserAddress}}
err = c.Update(colQuerier, change)
if err != nil {
panic(err)
}
result2 := User{}
err = c.Find(bson.M{"userid": locationId}).One(&result2)
if err != nil {
log.Fatal(err)
}
outgoingJSON, err := json.Marshal(result2)
if err != nil {
log.Fatal(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusCreated)
fmt.Fprint(rw, string(outgoingJSON))
}
示例6: getLocation
func getLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
id := bson.ObjectIdHex(p.ByName("locationID"))
err := collection.FindId(id).One(&locRes)
if err != nil {
fmt.Printf("error finding a doc %v\n")
}
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
rw.WriteHeader(200)
json.NewEncoder(rw).Encode(locRes)
}
示例7: get_K
func get_K(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
key, _ := strconv.Atoi(p.ByName("key"))
pair := new(Pair)
pair.Key = key
pair.Value = data[key]
oj, err := json.Marshal(pair)
if err != nil {
//log.Println(error.Error())
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusCreated)
fmt.Fprint(rw, string(oj))
}
示例8: deleteLocation
func deleteLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
locationId, _ := strconv.Atoi(p.ByName("location_id"))
session, err := mgo.Dial("mongodb://sejal:[email protected]:45064/planner")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("planner").C("user")
err = c.Remove(bson.M{"userid": locationId})
if err != nil {
log.Fatal(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusCreated)
fmt.Fprint(rw, "User Location deleted successfully")
}
示例9: handlePutRequests
func handlePutRequests(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
k, _ := strconv.Atoi(p.ByName("key"))
value := p.ByName("value")
url := "http://localhost:" + getServerForClient(k) + "/keys/" + strconv.Itoa(k) + "/" + value
req, err := http.NewRequest("PUT", url, nil)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var data interface{}
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &data)
var m = data.(interface{}).(float64)
fmt.Fprint(rw, m)
}
示例10: DeleteLocation
//DeleteLocation to delete a location
func DeleteLocation(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("idno")
oid, _ := strconv.Atoi(id)
session, err := mgo.Dial("mongodb://dhaval:[email protected]:41144/mongo_db")
checkError(err)
defer session.Close()
//insert values in mongo_db
session.SetMode(mgo.Monotonic, true)
err = session.DB("mongo_db").C("location").RemoveId(oid)
if err != nil {
panic(err)
}
w.WriteHeader(200)
}
示例11: GetTrip
func GetTrip(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
tripDetails := TripDetails{}
id := bson.ObjectIdHex(p.ByName("trip_id"))
err := collection.FindId(id).One(&tripDetails)
if err != nil {
fmt.Printf("got error While Searching document %v\n")
}
if tripDetails.ID == "" {
fmt.Fprintf(rw, "LocationID not valid")
} else {
tripDetails.NextDestinationLocationID = ""
tripDetails.UberWaitTime = ""
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
rw.Header().Set("Access-Control-Allow-Origin", "*")
rw.WriteHeader(200)
json.NewEncoder(rw).Encode(tripDetails)
}
}
示例12: GetKey
func GetKey(rw http.ResponseWriter, request *http.Request, p httprouter.Params) {
out := a1
index := i1
port := strings.Split(request.Host, ":")
if port[1] == "3001" {
out = a2
index = i2
} else if port[1] == "3002" {
out = a3
index = i3
}
key, _ := strconv.Atoi(p.ByName("key_id"))
for i := 0; i < index; i++ {
if out[i].Key == key {
result, _ := json.Marshal(out[i])
fmt.Fprintln(rw, string(result))
}
}
}
示例13: PutKeys
func PutKeys(rw http.ResponseWriter, request *http.Request, p httprouter.Params) {
port := strings.Split(request.Host, ":")
key, _ := strconv.Atoi(p.ByName("key_id"))
if port[1] == "3000" {
a1 = append(a1, KeyVal{key, p.ByName("value")})
i1++
} else if port[1] == "3001" {
a2 = append(a2, KeyVal{key, p.ByName("value")})
i2++
} else {
a3 = append(a3, KeyVal{key, p.ByName("value")})
i3++
}
}
示例14: updateLocation
func updateLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
var tempLocRes LocationRes
var locRes LocationRes
id := bson.ObjectIdHex(p.ByName("locationID"))
err := collection.FindId(id).One(&locRes)
if err != nil {
fmt.Printf("error finding a doc %v\n")
}
tempLocRes.Name = locRes.Name
tempLocRes.Address = locRes.Address
tempLocRes.City = locRes.City
tempLocRes.State = locRes.State
tempLocRes.Zip = locRes.Zip
decoder := json.NewDecoder(req.Body)
err = decoder.Decode(&tempLocRes)
if err != nil {
fmt.Errorf("Error in decoding the Input: %v", err)
}
address := tempLocRes.Address + " " + tempLocRes.City + " " + tempLocRes.State + " " + tempLocRes.Zip
address = strings.Replace(address, " ", "%20", -1)
locationDetails := getGoogleLoc(address)
tempLocRes.Coordinate.Lat = locationDetails.Results[0].Geometry.Location.Lat
tempLocRes.Coordinate.Lng = locationDetails.Results[0].Geometry.Location.Lng
err = collection.UpdateId(id, tempLocRes)
if err != nil {
fmt.Printf("got an error updating a doc %v\n")
}
err = collection.FindId(id).One(&locRes)
if err != nil {
fmt.Printf("got an error finding a doc %v\n")
}
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
rw.WriteHeader(201)
json.NewEncoder(rw).Encode(locRes)
}
示例15: getLocation
func getLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
locationId := p.ByName("location_id")
session, err := mgo.Dial("mongodb://sejal:[email protected]:45064/planner")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("planner").C("user")
result := User{}
err = c.Find(bson.M{"userid": locationId}).One(&result)
if err != nil {
fmt.Fprint(rw, "Data not found")
return
}
outgoingJSON, err := json.Marshal(result)
if err != nil {
log.Fatal(err)
}
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusCreated)
fmt.Fprint(rw, string(outgoingJSON))
}