当前位置: 首页>>代码示例>>Golang>>正文


Golang logs.Error函数代码示例

本文整理汇总了Golang中github.com/quorumsco/logs.Error函数的典型用法代码示例。如果您正苦于以下问题:Golang Error函数的具体用法?Golang Error怎么用?Golang Error使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Error函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Register

// Creates a new user
func Register(w http.ResponseWriter, req *http.Request) {
	if req.Method == "POST" {
		req.ParseForm()

		passwordHash, err := bcrypt.GenerateFromPassword([]byte(req.FormValue("password")), bcrypt.DefaultCost)
		if err != nil {
			panic(err)
		}

		u := &models.User{
			Firstname: sPtr(req.FormValue("firstname")),
			Surname:   sPtr(req.FormValue("surname")),
			Mail:      sPtr(req.FormValue("mail")),
			Password:  sPtr(string(passwordHash)),
		}

		errs := u.Validate()
		if len(errs) > 0 {
			logs.Debug(errs)
			return
		}

		var store = models.UserStore(getDB(req))
		err = store.Save(u)
		if err != nil {
			logs.Error(err)
			return
		}
	}

	templates := getTemplates(req)
	if err := templates["users/register"].ExecuteTemplate(w, "base", nil); err != nil {
		logs.Error(err)
	}
}
开发者ID:gofmt,项目名称:oauth2,代码行数:36,代码来源:users.go

示例2: Authenticate

// Middleware that takes care of the authentication. If the token is not correct or there is no token, an error is returned
func Authenticate(h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		// Pour desactiver l'authentification
		// router.Context(r).Env["GroupID"] = uint(1)
		// router.Context(r).Env["UserID"] = uint(1)
		// h.ServeHTTP(w, r)
		// return
		//
		token := strings.TrimLeft(r.Header.Get("Authorization"), "Bearer ")
		if token == "" {
			logs.Debug("No token found")
			Fail(w, r, "missing bearer authorization token", http.StatusBadRequest)
		} else {
			body, _ := Bearer(r, "OAuth2", "/oauth2/info", token)
			resp := make(map[string]interface{})
			if err := json.Unmarshal(body, &resp); err != nil {
				logs.Error(err)
				Fail(w, r, map[string]string{"Authentication": err.Error()}, http.StatusBadRequest)
				return
			}
			if resp["error"] != nil {
				// refresh := strings.TrimLeft(r.Header.Get("Refresh"), "Refresh ")
				// r.Form = make(url.Values)
				// r.Form.Add("grant_type", "refresh_token")
				// r.Form.Add("refresh_token", refresh)
				// logs.Debug(refresh)
				// body, _ := ServiceBody(PostMethod, r, "OAuth2", "/oauth2/token", "application/x-www-form-urlencoded", []byte(r.Form.Encode()))
				// resp := make(map[string]interface{})
				// if err := json.Unmarshal(body, &resp); err != nil {
				// 	logs.Error(err)
				// 	Fail(w, r, map[string]string{"Authentication": err.Error()}, http.StatusBadRequest)
				// 	return
				// }
				// logs.Debug(resp)
				// if resp["error"] != nil {
				logs.Debug("Token expired")
				Fail(w, r, "Token", http.StatusBadRequest)
				// }
			} else {
				s := strings.Split(resp["owner"].(string), ":")
				ID, _ := strconv.Atoi(s[0])
				groupID, _ := strconv.Atoi(s[1])

				router.Context(r).Env["GroupID"] = uint(groupID)
				router.Context(r).Env["UserID"] = uint(ID)
				if ID == 0 {
					logs.Error(errors.New("ID is 0, no such user"))
					Fail(w, r, map[string]string{"ID": "is 0"}, http.StatusBadRequest)
					return
				}
				logs.Debug("Authentication done")
				h.ServeHTTP(w, r)
			}
		}
	}
	return http.HandlerFunc(fn)
}
开发者ID:quorumsco,项目名称:gateway,代码行数:58,代码来源:controllers.go

示例3: UpdateUser

func UpdateUser(w http.ResponseWriter, req *http.Request) {
	//logs.Debug("on rentre dans RegisterUser")
	req.ParseForm()
	if req.FormValue("mail") != "" && req.FormValue("password") != "" {
		// Perform an OAuth "Resource Owner Password Credentials Grant"
		req.Form.Add("grant_type", "password")

		body, err := ServiceBody(PostMethod, req, "OAuth2", "/users/update", "application/x-www-form-urlencoded", []byte(req.Form.Encode()))
		if err != nil {
			logs.Error(err)
			jsonapi.Error(w, req, err.Error(), http.StatusInternalServerError)
			return
		} else {
			logs.Debug("on passe successfull dans UpdateUser de Gateway/controllers/session.go")
		}

		// Check http status for errors
		var t StructRegisterUser
		err = json.Unmarshal(body, &t)
		if err != nil {
		} else {
			jsonapi.Error(w, req, t.Message, http.StatusBadRequest)
		}
	}

}
开发者ID:quorumsco,项目名称:gateway,代码行数:26,代码来源:session.go

示例4: RetrieveGroup

// RetrieveGroup calls the GroupSQL First method and returns the results
func RetrieveGroup(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.Atoi(router.Context(r).Param("id"))
	if err != nil {
		logs.Debug(err)
		Fail(w, r, map[string]interface{}{"id": "not integer"}, http.StatusBadRequest)
		return
	}

	var (
		g          = models.Group{}
		db         = getDB(r)
		groupStore = models.GroupStore(db)
	)
	if err = groupStore.First(&g, uint(id)); err != nil {
		if err == sql.ErrNoRows {
			Fail(w, r, nil, http.StatusNotFound)
			return
		}
		logs.Error(err)
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	Success(w, r, views.Group{Group: &g}, http.StatusOK)
}
开发者ID:gofmt,项目名称:oauth2,代码行数:26,代码来源:groups.go

示例5: serve

func serve(ctx *cli.Context) error {
	var app = application.New()

	config, err := settings.Parse(ctx.String("config"))
	if err != nil && ctx.String("config") != "" {
		logs.Error(err)
	}

	if ctx.Bool("debug") || config.Debug() {
		logs.Level(logs.DebugLevel)
	}

	app.Components["Mux"] = router.New()

	if ctx.Bool("debug") || config.Debug() {
		app.Use(router.Logger)
	}

	app.Use(app.Apply)
	app.Post("/trigger/:id", controllers.TriggerBuild)

	server, err := config.Server()
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	return app.Serve(server.String())
}
开发者ID:quorumsco,项目名称:wercker,代码行数:28,代码来源:main.go

示例6: RetrieveGroup

// Returns the group informations using his authentication token. Calls Oauth2 via HTTP. Returns results via jsonapi.
func RetrieveGroup(w http.ResponseWriter, r *http.Request) {
	ID := router.Context(r).Env["GroupID"].(uint)
	path := fmt.Sprintf("/groups/%d", ID)
	body, _ := Service(GetMethod, r, "OAuth2", path)
	resp := make(map[string]interface{})
	if err := json.Unmarshal(body, &resp); err != nil {
		logs.Error(err)
		Fail(w, r, map[string]string{"Authentication": "Could not join the server"}, http.StatusBadRequest)
		return
	}
	if resp["error"] != nil {
		Fail(w, r, resp["error_description"], http.StatusBadRequest)
	} else {
		resp = resp["data"].(map[string]interface{})
		resp = resp["group"].(map[string]interface{})
		//UID := int64(resp["id"].(float64))
		GID := uint(resp["id"].(float64))
		NAME := sPtr(resp["name"].(string))
		Date := time.Now()
		if resp["endofcampain"] != nil {
			datecampain := resp["endofcampain"].(string)
			Date, _ = time.Parse(time.RFC3339, datecampain)
		}
		g := usermodels.Group{ID: GID, Name: NAME, Endofcampain: &Date}
		// Success(w, r, userviews.Group{Group: &g}, http.StatusOK)
		//g := usermodels.Group{ID: GID, Name: NAME}
		SuccessOKOr404(w, r, userviews.Group{Group: &g})
	}
}
开发者ID:quorumsco,项目名称:gateway,代码行数:30,代码来源:contacts.go

示例7: index

// Index indexes a contact into elasticsearch
func index(Contact *models.Contact, s *elastic.Client) error {
	id := strconv.Itoa(int(Contact.ID))
	if id == "" {
		logs.Error("id is nil")
		return errors.New("id is nil")
	}

	if Contact.Address.Longitude == "null" || Contact.Address.Latitude == "null" {
		logs.Debug("Could not index contact %s", id)
		return nil
	}

	if Contact.Address.Latitude != "" && Contact.Address.Longitude != "" {
		Contact.Address.Location = fmt.Sprintf("%s,%s", Contact.Address.Latitude, Contact.Address.Longitude)
	}

	_, err := s.Index().
		Index("contacts").
		Type("contact").
		Id(id).
		BodyJson(Contact).
		Do()
	if err != nil {
		logs.Critical(err)
		return err
	}

	logs.Debug("Contact %s indexed", id)

	return nil
}
开发者ID:quorumsco,项目名称:indexor,代码行数:32,代码来源:main.go

示例8: UpdateGroup

// UpdateGroup calls the GroupSQL Save method and returns the results
func UpdateGroup(w http.ResponseWriter, r *http.Request) {
	var (
		groupID int
		err     error
	)
	if groupID, err = strconv.Atoi(router.Context(r).Param("id")); err != nil {
		logs.Debug(err)
		Fail(w, r, map[string]interface{}{"id": "not integer"}, http.StatusBadRequest)
		return
	}

	var (
		db         = getDB(r)
		groupStore = models.GroupStore(db)
		g          = &models.Group{ID: uint(groupID)}
	)
	if err = groupStore.First(g, g.ID); err != nil {
		Fail(w, r, map[string]interface{}{"group": err.Error()}, http.StatusBadRequest)
		return
	}

	if err = Request(&views.Group{Group: g}, r); err != nil {
		logs.Debug(err)
		Fail(w, r, map[string]interface{}{"group": err.Error()}, http.StatusBadRequest)
		return
	}

	if err = groupStore.Save(g); err != nil {
		logs.Error(err)
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	Success(w, r, views.Group{Group: g}, http.StatusOK)
}
开发者ID:gofmt,项目名称:oauth2,代码行数:36,代码来源:groups.go

示例9: Session

// Generates a authentication token, stores it in a Redis instance under a new id and stores the id in a cookie on the user's browser to allow the gateway to later retrieve the token from the cookie and authenticate the user
func Session(w http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	if req.FormValue("username") != "" && req.FormValue("password") != "" {
		// Perform an OAuth "Resource Owner Password Credentials Grant"
		req.Form.Add("grant_type", "password")

		body, err := ServiceBody(PostMethod, req, "OAuth2", "/oauth2/token", "application/x-www-form-urlencoded", []byte(req.Form.Encode()))
		if err != nil {
			logs.Error(err)
			return
		}

		// TODO: Check http status for errors
		access := new(components.AccessData)
		if err := json.Unmarshal(body, access); err != nil {
			logs.Error(err)
			jsonapi.Error(w, req, err.Error(), http.StatusBadRequest)
			return
		}
		if access.AccessToken == "" {
			var err = errors.New("wrong identifiers")
			logs.Error(err)
			jsonapi.Fail(w, req, err.Error(), http.StatusBadRequest)
			return
		}
		session := &components.Session{
			ID:           strings.TrimRight(base64.StdEncoding.EncodeToString(uuid.NewRandom()), "="),
			AccessToken:  access.AccessToken,
			RefreshToken: access.RefreshToken,
			ExpiresIn:    access.ExpiresIn,
		}
		app := router.Context(req).Env["Application"].(*application.Application)
		store := app.Components["Redis"].(*components.RedisStore)
		if err := store.Save(session); err != nil {
			logs.Error(err)
			jsonapi.Error(w, req, err.Error(), http.StatusBadRequest)
			return
		}

		cookie := &http.Cookie{Name: "SID", Value: session.ID, Expires: time.Now().Add(356 * 24 * time.Hour)}
		http.SetCookie(w, cookie)
		jsonapi.Success(w, req, "", http.StatusOK)
		fmt.Println(cookie)
	} else {
		jsonapi.Error(w, req, "Bad Request", http.StatusBadRequest)
	}
}
开发者ID:quorumsco,项目名称:gateway,代码行数:48,代码来源:session.go

示例10: serve

func serve(ctx *cli.Context) error {
	var err error

	var config settings.Config
	if ctx.String("config") != "" {
		config, err = settings.Parse(ctx.String("config"))
		if err != nil {
			logs.Error(err)
		}
	}

	if config.Debug() {
		logs.Level(logs.DebugLevel)
	}

	dialect, args, err := config.SqlDB()
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	logs.Debug("database type: %s", dialect)

	var app = application.New()
	if app.Components["DB"], err = databases.InitGORM(dialect, args); err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	logs.Debug("connected to %s", args)

	if config.Migrate() {
		app.Components["DB"].(*gorm.DB).AutoMigrate(models.Models()...)
		logs.Debug("database migrated successfully")
	}

	app.Components["Templates"] = views.Templates()
	// app.Components["Mux"] = router.New()
	app.Components["Mux"] = gojimux.New()

	if config.Debug() {
		app.Use(router.Logger)
	}

	app.Use(app.Apply)

	app.Get("/users/register", controllers.Register)
	app.Post("/users/auth", controllers.Auth)
	app.Post("/users/register", controllers.Register)

	server, err := config.Server()
	if err != nil {
		logs.Critical(err)
		os.Exit(1)
	}
	return app.Serve(server.String())
}
开发者ID:quorumsco,项目名称:users,代码行数:55,代码来源:main.go

示例11: DeleteSession

// Deletes the cookie on the user's browser, allows him to logout
func DeleteSession(w http.ResponseWriter, req *http.Request) {
	cookie, err := req.Cookie("SID")
	if err == nil {
		app := router.Context(req).Env["Application"].(*application.Application)
		store := app.Components["Redis"].(*components.RedisStore)

		err := store.Delete(cookie.Value)
		if err != nil {
			logs.Error(err)
			jsonapi.Error(w, req, err.Error(), http.StatusBadRequest)
			return
		}
		cookie := &http.Cookie{Name: "SID", Value: cookie.Value, Expires: time.Now()}
		http.SetCookie(w, cookie)
		jsonapi.Success(w, req, "", http.StatusOK)
		logs.Debug("Cookie deleted")
	} else {
		logs.Error(err)
	}
}
开发者ID:quorumsco,项目名称:gateway,代码行数:21,代码来源:session.go

示例12: Auth

func Auth(w http.ResponseWriter, r *http.Request) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		logs.Error(err)
		Fail(w, r, map[string]string{"Authentification": "Error"}, http.StatusBadRequest)
		return
	}
	infos := make(map[string]interface{})
	if err := json.Unmarshal(body, &infos); err != nil {
		logs.Error(err)
		Fail(w, r, map[string]string{"Authentification": "Bad parameters"}, http.StatusBadRequest)
		return
	}
	username := infos["username"].(string)
	password := infos["password"].(string)
	passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		panic(err)
	}

	var (
		u         = models.User{Mail: &username, Password: sPtr(string(passwordHash))}
		db        = getDB(r)
		userStore = models.UserStore(db)
	)
	if err = userStore.First(&u); err != nil {
		logs.Error(err)
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}
	if u.GroupID == 0 {
		Fail(w, r, map[string]interface{}{"User": "No such user"}, http.StatusBadRequest)
		return
	}
	Success(w, r, views.User{User: &u}, http.StatusOK)
}
开发者ID:quorumsco,项目名称:users,代码行数:36,代码来源:users.go

示例13: RetrieveGroupCollection

// RetrieveGroupCollection calls the GroupSQL Find method and returns the results
func RetrieveGroupCollection(w http.ResponseWriter, r *http.Request) {
	var (
		err        error
		groups     []models.Group
		db         = getDB(r)
		groupStore = models.GroupStore(db)
	)
	if groups, err = groupStore.Find(); err != nil {
		logs.Error(err)
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	Success(w, r, views.Groups{Groups: groups}, http.StatusOK)
}
开发者ID:gofmt,项目名称:oauth2,代码行数:16,代码来源:groups.go

示例14: CreateContact

// CreateContact calls the Create method from Contacts via RPC (Contact client) and then calls the Index method from Contacts via RPC (Search client). Returns results via jsonapi.
func CreateContact(w http.ResponseWriter, r *http.Request) {

	var (
		args = models.ContactArgs{Contact: &models.Contact{GroupID: router.Context(r).Env["GroupID"].(uint)}}
		//dataGEO = models.Ban{}
	)

	if err := Request(&views.Contact{Contact: args.Contact}, r); err != nil {
		logs.Error(err)
		Fail(w, r, map[string]interface{}{"contact": err.Error()}, http.StatusBadRequest)
		return
	}

	var (
		contactClient = rpcClient(r, "ContactClient")
		reply         = models.ContactReply{}
	)

	var coordonnees [2]string
	coordonnees = Geoloc(&args)
	if coordonnees[0] == "noresponse" {
		err := errors.New("noresponse")
		Error(w, r, err.Error(), http.StatusBadRequest)
		return
	}
	args.Contact.Address.Latitude = coordonnees[0]
	args.Contact.Address.Longitude = coordonnees[1]

	err := contactClient.Call("Contact.Create", args, &reply)
	if err != nil {
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	rep := models.ContactReply{}
	args.Contact = reply.Contact

	err = contactClient.Call("Search.Index", args, &rep)
	if err != nil {
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	Success(w, r, views.Contact{Contact: reply.Contact}, http.StatusOK)
}
开发者ID:quorumsco,项目名称:gateway,代码行数:46,代码来源:contacts.go

示例15: Authorize

// Authorize endpoint
func Authorize(w http.ResponseWriter, r *http.Request) {
	server := OAuthComponent(r)
	resp := server.NewResponse()
	defer resp.Close()

	if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {
		if !example.HandleLoginPage(ar, w, r) {
			return
		}
		ar.UserData = "test" // Get user_id
		ar.Authorized = true
		server.FinishAuthorizeRequest(resp, r, ar)
	}
	if resp.IsError && resp.InternalError != nil {
		logs.Error(resp.InternalError.Error())
	}
	osin.OutputJSON(resp, w, r)
}
开发者ID:gofmt,项目名称:oauth2,代码行数:19,代码来源:oauth2.go


注:本文中的github.com/quorumsco/logs.Error函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。