當前位置: 首頁>>代碼示例>>Golang>>正文


Golang communicator.New函數代碼示例

本文整理匯總了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)
}
開發者ID:paked,項目名稱:steel,代碼行數:52,代碼來源:server.go

示例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)
	}
}
開發者ID:paked,項目名稱:restrict,代碼行數:25,代碼來源:restrict.go

示例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)
}
開發者ID:paked,項目名稱:steel,代碼行數:34,代碼來源:server.go

示例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)
}
開發者ID:paked,項目名稱:go-service-example,代碼行數:30,代碼來源:user_handlers.go

示例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)
}
開發者ID:paked,項目名稱:steel,代碼行數:31,代碼來源:server.go

示例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)
}
開發者ID:paked,項目名稱:nutrimon,代碼行數:31,代碼來源:main.go

示例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)
}
開發者ID:paked,項目名稱:steel,代碼行數:32,代碼來源:server.go

示例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!")
}
開發者ID:paked,項目名稱:steel,代碼行數:29,代碼來源:server.go

示例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
}
開發者ID:paked,項目名稱:nutrimon,代碼行數:7,代碼來源:main.go

示例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
	}
}
開發者ID:paked,項目名稱:nutrimon,代碼行數:31,代碼來源:main.go

示例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)
}
開發者ID:paked,項目名稱:steel,代碼行數:30,代碼來源:server.go

示例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)
}
開發者ID:paked,項目名稱:nutrimon,代碼行數:29,代碼來源:main.go

示例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)
}
開發者ID:paked,項目名稱:steel,代碼行數:34,代碼來源:server.go

示例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)
}
開發者ID:paked,項目名稱:nutrimon,代碼行數:10,代碼來源:main.go

示例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.")
}
開發者ID:paked,項目名稱:nutrimon,代碼行數:55,代碼來源:main.go


注:本文中的github.com/paked/gerrycode/communicator.New函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。