Init project with vote module
This commit is contained in:
commit
98f3c2aedc
11 changed files with 671 additions and 0 deletions
26
extract.go
Normal file
26
extract.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"planner/models"
|
||||
)
|
||||
|
||||
func extract_user(orm *gorm.DB, c *gin.Context) *models.User {
|
||||
username, _, ok := c.Request.BasicAuth()
|
||||
if !ok {
|
||||
c.Status(http.StatusUnauthorized)
|
||||
return nil
|
||||
}
|
||||
u := models.User{
|
||||
Username: username,
|
||||
}
|
||||
|
||||
result := orm.Take(&u)
|
||||
if result.Error != nil {
|
||||
c.String(http.StatusNotFound, "Unable to find user "+username)
|
||||
return nil
|
||||
}
|
||||
return &u
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue