func Logger(c *gin.Context) { requestId := util.NewId() c.Set("request_id", requestId) method := c.Request.Method path := c.Request.URL.EscapedPath() ip := c.ClientIP() log.InfoFields("Request received", log.Fields{ "request_id": requestId, "method": method, "ip": ip, "path": path, }) start := time.Now() c.Next() duration := time.Since(start) code := c.Writer.Status() log.InfoFields("Request handled", log.Fields{ "request_id": requestId, "took": duration.String(), "code": code, }) }
func NewSuggestion(text string, from string) Suggestion { return Suggestion{ Type: "recommendation", Recommendation: InnerSuggestion{ Id: util.NewId(), Text: text, Genesis: from, }, } }
func PostSchoolRequest(c *gin.Context) { var request model.SchoolRequestRequest err := c.BindJSON(&request) if log.Error(err) { c.Error(err) return } dbreq := model.DbSchoolRequest{ Id: util.NewId(), RequesterUserId: c.MustGet("request_user_id").(string), Name: request.Name, Location: request.Location, } err = db.CreateSchoolRequest(dbreq) if log.Error(err) { c.Error(err) return } c.JSON(http.StatusOK, dbreq) }
func Login(c *gin.Context) { var returnCode int var request model.LoginRequest // Parse the user request err := c.BindJSON(&request) if log.Error(err) { c.Error(err) return } // Right now we assume that the user is logging in with Facebook fbUser, err := service.Facebook{}.GetCurrentUser(request.AccessToken) if log.Error(err) { c.Error(errors.NewHttp(errors.ISE, "Error contacting facebook api")) return } // Then get the user's profile picture fbPicture, err := service.Facebook{}.GetProfilePic(request.AccessToken) if log.Error(err) { c.Error(errors.NewHttp(errors.ISE, "Error contacting facebook api")) return } // See if the user is already a notion user in, dbUser, err := db.GetUserByFacebookId(fbUser.Id) if log.Error(err) { c.Error(errors.NewISE()) return } // If they are in the database, we just update their auth token if in { returnCode = http.StatusAccepted dbUser.FbAuthToken = request.AccessToken dbUser.FbProfilePic = fbPicture.Data.Url err = db.UpdateUser(dbUser) } else { returnCode = http.StatusCreated dbUser = model.DbUser{ Id: util.NewId(), Name: fbUser.Name, Email: fbUser.Email, Verified: false, AuthMethod: request.AuthMethod, FbUserId: fbUser.Id, FbAuthToken: request.AccessToken, FbProfilePic: fbPicture.Data.Url, } err = db.CreateUser(dbUser) } // Error check 'er yo if log.Error(err) { c.Error(errors.NewISE()) return } // Throw back the user object at the requester c.JSON(returnCode, model.NewUserResponse(dbUser)) }