本文整理汇总了Golang中github.com/paked/gerrycode/communicator.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetUserHandler
func GetUserHandler(w http.ResponseWriter, r *http.Request) {
c := communicator.New(w)
username := r.FormValue("username")
sid := r.FormValue("id")
if sid != "" {
var id int64
fmt.Sscanf(sid, "%d", &id)
fmt.Println(id)
u, err := models.GetUserByID(id)
if err != nil {
c.Fail("Error getting user")
return
}
c.OKWithData("Here is your user: ", u)
return
}
if username != "" {
u, err := models.GetUser("username", username)
if err != nil {
c.Fail("Could not get that username")
return
}
c.OKWithData("Here is your user: ", u)
return
}
restrict.R(func(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
fmt.Println("hello?")
c := communicator.New(w)
fid, ok := t.Claims["id"].(float64)
if !ok {
c.Fail("Could not get that ID")
return
}
id := int64(fid)
u, err := models.GetUserByID(id)
if err != nil {
c.Fail("Error getting user")
return
}
c.OKWithData("Here is your user", u)
})(w, r)
}
示例2: R
func R(fn func(http.ResponseWriter, *http.Request, *jwt.Token)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ts := r.FormValue("access_token")
c := communicator.New(w)
token, err := jwt.Parse(ts, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", t.Header["alg"])
}
return pkey, nil
})
if err != nil {
c.Fail("You are not using a valid token:" + err.Error())
return
}
if !token.Valid {
c.Fail("Something obscurely weird happened to your token!")
return
}
fn(w, r, token)
}
}
示例3: CreateWorkshopHandler
func CreateWorkshopHandler(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
c := communicator.New(w)
name := r.FormValue("name")
description := r.FormValue("description")
explanation := r.FormValue("explanation")
if name == "" || description == "" || explanation == "" {
c.Fail("Invalid data")
return
}
vars := mux.Vars(r)
u, err := getUserFromToken(t)
if err != nil {
c.Fail("Unable to get user from token")
return
}
s, _, err := getClassFromString(u, vars["class_id"])
if err != nil {
c.Fail("Could not get class info")
return
}
a, err := s.CreateWorkshop(name, description, explanation)
if err != nil {
c.Fail("Could not create workshop")
return
}
c.OKWithData("Here is your workshops", a)
}
示例4: loginHandler
func loginHandler(w http.ResponseWriter, r *http.Request) {
coms := communicator.New(w)
username := r.FormValue("username")
password := r.FormValue("password")
u, err := models.FetchUser(username)
if err != nil {
fmt.Println(err)
coms.Error("Could not fetch user")
return
}
if !u.Login(password) {
coms.Error("Incorrect password")
return
}
claims := make(map[string]interface{})
claims["id"] = u.ID
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
ts, err := restrict.Token(claims)
if err != nil {
coms.Fail("Failure signing the token")
return
}
coms.OKWithData("token", ts)
}
示例5: CreateClassHandler
func CreateClassHandler(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
c := communicator.New(w)
u, err := getUserFromToken(t)
if err != nil {
c.Fail("Unable to get your user")
return
}
name := r.FormValue("name")
description := r.FormValue("description")
if name == "" || description == "" {
c.Fail("Invalide data")
return
}
class, err := u.NewClass(name, description)
if err != nil {
c.Fail("Could not create class")
return
}
err = class.SetImage("imgs/home.png")
if err != nil {
c.Fail("Could not set home class image")
return
}
c.OKWithData("Here is your class", class)
}
示例6: LoginUserHandler
func LoginUserHandler(w http.ResponseWriter, r *http.Request) {
c := communicator.New(w)
username := r.FormValue("username")
password := r.FormValue("password")
u, err := GetUser("username", username)
if err != nil {
log.Println(err)
c.Fail("Could not get user")
return
}
ok := u.Login(password)
if !ok {
c.Fail("Incorrect username")
return
}
claims := make(map[string]interface{})
claims["id"] = u.ID
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
ts, err := restrict.Token(claims)
if err != nil {
c.Fail("Failure signing that token!")
return
}
c.OKWithData("Here is your token", ts)
}
示例7: GetWorkshopsHandler
func GetWorkshopsHandler(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
c := communicator.New(w)
vars := mux.Vars(r)
u, err := getUserFromToken(t)
if err != nil {
c.Fail("Unable to get user from token")
return
}
s, _, err := getClassFromString(u, vars["class_id"])
if err != nil {
c.Fail("Could not get class info")
return
}
d, err := time.ParseDuration("168h")
if err != nil {
c.Fail("could not parse duration")
return
}
tm := time.Now().Add(d)
as, err := s.Workshops(tm)
if err != nil {
c.Fail("Could not get workshop")
return
}
c.OKWithData("Here are your workshops", as)
}
示例8: SetClassImageHandler
func SetClassImageHandler(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
c := communicator.New(w)
vars := mux.Vars(r)
u, err := getUserFromToken(t)
if err != nil {
c.Fail("Could not get user")
return
}
s, class, err := getClassFromString(u, vars["class_id"])
if err != nil {
c.Fail("Could not get class")
return
}
if !s.IsAdmin() {
c.Error("You are not an admin!")
return
}
err = class.SetImage(r.FormValue("image_url"))
if err != nil {
c.Fail("Could not set image url")
return
}
c.OK("Done!")
}
示例9: RemovePollHandler
func RemovePollHandler(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
c := communicator.New(w)
c.OKWithData("Are you removing! I don't know!", removeAnItem)
removeAnItem = false
}
示例10: MassChangeHandler
func MassChangeHandler(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
c := communicator.New(w)
u, err := getUserFromToken(t)
if err != nil {
c.Fail("Could not get token")
return
}
amountString := r.FormValue("amount")
amount, err := strconv.Atoi(amountString)
if err != nil {
c.Fail("That is not a valid mass number")
return
}
if amount > lastWeight && queue != (Stock{}) {
_, err := db.Exec("INSERT INTO pantry (user, brand, category, manufacturer, description) VALUES (?, ?, ?, ?, ?)", u.ID, queue.Brand, queue.Category, queue.Manufacturer, queue.Description)
if err != nil {
log.Println(err)
c.Fail("Kill the pantry")
return
}
log.Println("Added item to queue")
queue = Stock{}
} else {
// make user show poll
}
}
示例11: GetWorkshopHandler
func GetWorkshopHandler(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
c := communicator.New(w)
vars := mux.Vars(r)
u, err := getUserFromToken(t)
if err != nil {
c.Fail("COuld not get user")
return
}
_, class, err := getClassFromString(u, vars["class_id"])
if err != nil {
c.Fail("Could not get class info")
return
}
wid, err := strconv.ParseInt(vars["workshop_id"], 10, 64)
if err != nil {
c.Fail("Not a valid workshop id")
return
}
work, err := class.Workshop(wid)
if err != nil {
c.Fail("Could not get workshop")
return
}
c.OKWithData("Hre is your workshop", work)
}
示例12: AllFoodInPantryHandler
func AllFoodInPantryHandler(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
c := communicator.New(w)
u, err := getUserFromToken(t)
if err != nil {
c.Fail("Could not get token from user")
return
}
st := []Stock{}
rows, err := db.Query("SELECT id, weight, initial_weight, brand, category, manufacturer, description FROM pantry WHERE user = ?", u.ID)
for rows.Next() {
s := Stock{}
err = rows.Scan(&s.ID, &s.Weight, &s.InitialWeight, &s.Brand, &s.Category, &s.Manufacturer, &s.Description)
if err != nil {
log.Println(err)
c.Fail("Could not get information out of ID")
return
}
st = append(st, s)
}
c.OKWithData("food.", st)
}
示例13: LoginHandler
func LoginHandler(w http.ResponseWriter, r *http.Request) {
c := communicator.New(w)
username := r.FormValue("username")
password := r.FormValue("password")
u, err := models.GetUser("username", username)
if err != nil {
c.Fail("Unable to find that user")
return
}
ok, err := u.Login(password)
if err != nil {
c.Fail("Authentication error")
}
if !ok {
c.Fail("That was not a matching password")
return
}
claims := make(map[string]interface{})
claims["id"] = u.ID
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
ts, err := restrict.Token(claims)
if err != nil {
c.Fail("Failure signing that token!")
return
}
c.OKWithData("Successfully logged in that user", ts)
}
示例14: InfoHandler
func InfoHandler(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
c := communicator.New(w)
u, err := getUserFromToken(t)
if err != nil {
c.Fail("Could not get user ID from token")
return
}
c.OKWithData("Here is your user, welcome to the party", u)
}
示例15: ConsumeFoodHandler
// adds a new data point to relevant statistics
// removes food items that have "ran out of stock"
func ConsumeFoodHandler(w http.ResponseWriter, r *http.Request, t *jwt.Token) {
c := communicator.New(w)
u, err := getUserFromToken(t)
if err != nil {
c.Fail("Could not get user from token")
return
}
idString := r.FormValue("food_item")
quantityString := r.FormValue("quantity")
idInt, err := strconv.Atoi(idString)
if err != nil {
c.Fail("That id is not int convertable")
return
}
quantityInt, err := strconv.Atoi(quantityString)
if err != nil {
c.Fail("That qunaitty is not convertable")
return
}
id := int64(idInt)
quantity := float64(quantityInt)
log.Println(quantity) // TODO use quantity
s := Stock{}
row := db.QueryRow("SELECT id, user, brand, category, manufacturer, description, weight, initial_weight FROM pantry WHERE id = ? AND user = ?", id, u.ID)
err = row.Scan(&s.ID, &s.Category, &s.Manufacturer, &s.Description, &s.Weight, &s.InitialWeight)
if err != nil {
log.Println(err)
c.Fail("Could not get that db")
return
}
_, err = db.Exec("UPDATE pantry SET weight = ? AND avail = ? WHERE id = ? AND user = ?", s.Weight-quantity, (s.Weight-quantity) >= 0, s.ID, u.ID)
if err != nil {
log.Println(err)
c.Fail("Could not update pantry")
return
}
if s.Weight-quantity < 2 {
message := "You are running out of " + s.Brand
twilio.SendSMS(from, u.Phone, message, "", "")
}
c.OK("consumed.")
}