Compare commits
No commits in common. 'master' and 'widyarhn-dev' have entirely different histories.
master
...
widyarhn-d
18 changed files with 853 additions and 25 deletions
@ -0,0 +1,3 @@ |
|||||||
|
PORT=1000 |
||||||
|
SECRET=sdc87sdf6gv87df6v8sd7f6v8sdf76v8sdf7v6 |
||||||
|
DB_URL="host=dumbo.db.elephantsql.com user=yvvpeiee password=QwAhcTtiKL8j9qRyUBIqs9kNbu3O0_FB dbname=yvvpeiee port=5432 sslmode=disable" |
@ -1,23 +0,0 @@ |
|||||||
# ---> Go |
|
||||||
# If you prefer the allow list template instead of the deny list, see community template: |
|
||||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore |
|
||||||
# |
|
||||||
# Binaries for programs and plugins |
|
||||||
*.exe |
|
||||||
*.exe~ |
|
||||||
*.dll |
|
||||||
*.so |
|
||||||
*.dylib |
|
||||||
|
|
||||||
# Test binary, built with `go test -c` |
|
||||||
*.test |
|
||||||
|
|
||||||
# Output of the go coverage tool, specifically when used with LiteIDE |
|
||||||
*.out |
|
||||||
|
|
||||||
# Dependency directories (remove the comment below to include it) |
|
||||||
# vendor/ |
|
||||||
|
|
||||||
# Go workspace file |
|
||||||
go.work |
|
||||||
|
|
@ -0,0 +1,111 @@ |
|||||||
|
package controllers |
||||||
|
|
||||||
|
import ( |
||||||
|
"go-crud/initializers" |
||||||
|
"go-crud/models" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/gin-gonic/gin" |
||||||
|
) |
||||||
|
|
||||||
|
func EventsCreate(c *gin.Context) { |
||||||
|
// Get data off req body
|
||||||
|
var body struct { |
||||||
|
Name string |
||||||
|
Content string |
||||||
|
PostBy string |
||||||
|
Category string |
||||||
|
} |
||||||
|
c.Bind(&body) |
||||||
|
|
||||||
|
// Create a event
|
||||||
|
now := time.Now() |
||||||
|
event := models.Event{ |
||||||
|
Name: body.Name, |
||||||
|
Content: body.Content, |
||||||
|
PostBy: body.PostBy, |
||||||
|
Category: body.Category, |
||||||
|
Date: &now, |
||||||
|
} |
||||||
|
result := initializers.DB.Create(&event) |
||||||
|
|
||||||
|
if result.Error != nil { |
||||||
|
c.Status(400) |
||||||
|
return |
||||||
|
} |
||||||
|
// Return it
|
||||||
|
c.JSON(200, gin.H{ |
||||||
|
"event": event, |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
func EventsIndex(c *gin.Context) { |
||||||
|
// Get the events
|
||||||
|
var events []models.Event |
||||||
|
initializers.DB.Find(&events) |
||||||
|
|
||||||
|
// Respond with them
|
||||||
|
c.JSON(200, gin.H{ |
||||||
|
"event": events, |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
func EventsShow(c *gin.Context) { |
||||||
|
// Get id off URL
|
||||||
|
id := c.Param("id") |
||||||
|
|
||||||
|
// Get the events
|
||||||
|
var event models.Event |
||||||
|
initializers.DB.First(&event, id) |
||||||
|
|
||||||
|
// Respond with them
|
||||||
|
c.JSON(200, gin.H{ |
||||||
|
"event": event, |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
func EventsUpdate(c *gin.Context) { |
||||||
|
// Get the id off the URL
|
||||||
|
id := c.Param("id") |
||||||
|
|
||||||
|
// Get the data off req body
|
||||||
|
|
||||||
|
var body struct { |
||||||
|
Name string |
||||||
|
Content string |
||||||
|
PostBy string |
||||||
|
Category string |
||||||
|
Date *time.Time |
||||||
|
} |
||||||
|
c.Bind(&body) |
||||||
|
|
||||||
|
// Find the post were updating
|
||||||
|
var event models.Event |
||||||
|
initializers.DB.First(&event, id) |
||||||
|
|
||||||
|
// Update it
|
||||||
|
now := time.Now() |
||||||
|
initializers.DB.Model(&event).Updates(models.Event{ |
||||||
|
Name: body.Name, |
||||||
|
Content: body.Content, |
||||||
|
PostBy: body.PostBy, |
||||||
|
Category: body.Category, |
||||||
|
Date: &now, |
||||||
|
}) |
||||||
|
|
||||||
|
// Respond with it
|
||||||
|
c.JSON(200, gin.H{ |
||||||
|
"event": event, |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
func EventsDelete(c *gin.Context) { |
||||||
|
// Get the id off the URL
|
||||||
|
id := c.Param("id") |
||||||
|
|
||||||
|
// Delete the post
|
||||||
|
initializers.DB.Delete(&models.Event{}, id) |
||||||
|
|
||||||
|
// Respond
|
||||||
|
c.Status(200) |
||||||
|
} |
@ -0,0 +1,93 @@ |
|||||||
|
package controllers |
||||||
|
|
||||||
|
import ( |
||||||
|
"go-crud/initializers" |
||||||
|
"go-crud/models" |
||||||
|
|
||||||
|
"github.com/gin-gonic/gin" |
||||||
|
) |
||||||
|
|
||||||
|
func PostsCreate(c *gin.Context) { |
||||||
|
// Get data off req body
|
||||||
|
var body struct { |
||||||
|
Body string |
||||||
|
Title string |
||||||
|
} |
||||||
|
c.Bind(&body) |
||||||
|
|
||||||
|
// Create a post
|
||||||
|
post := models.Post{Title: body.Title, Body: body.Body} |
||||||
|
result := initializers.DB.Create(&post) |
||||||
|
|
||||||
|
if result.Error != nil { |
||||||
|
c.Status(400) |
||||||
|
return |
||||||
|
} |
||||||
|
// Return it
|
||||||
|
c.JSON(200, gin.H{ |
||||||
|
"post": post, |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
func PostsIndex(c *gin.Context) { |
||||||
|
// Get the posts
|
||||||
|
var posts []models.Post |
||||||
|
initializers.DB.Find(&posts) |
||||||
|
|
||||||
|
// Respond with them
|
||||||
|
c.JSON(200, gin.H{ |
||||||
|
"post": posts, |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
func PostsShow(c *gin.Context) { |
||||||
|
// Get id off URL
|
||||||
|
id := c.Param("id") |
||||||
|
|
||||||
|
// Get the posts
|
||||||
|
var post models.Post |
||||||
|
initializers.DB.First(&post, id) |
||||||
|
|
||||||
|
// Respond with them
|
||||||
|
c.JSON(200, gin.H{ |
||||||
|
"post": post, |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
func PostsUpdate(c *gin.Context) { |
||||||
|
// Get the id off the URL
|
||||||
|
id := c.Param("id") |
||||||
|
|
||||||
|
// Get the data off req body
|
||||||
|
var body struct { |
||||||
|
Body string |
||||||
|
Title string |
||||||
|
} |
||||||
|
c.Bind(&body) |
||||||
|
|
||||||
|
// Find the post were updating
|
||||||
|
var post models.Post |
||||||
|
initializers.DB.First(&post, id) |
||||||
|
|
||||||
|
// Update it
|
||||||
|
initializers.DB.Model(&post).Updates(models.Post{ |
||||||
|
Title: body.Title, |
||||||
|
Body: body.Body, |
||||||
|
}) |
||||||
|
|
||||||
|
// Respond with it
|
||||||
|
c.JSON(200, gin.H{ |
||||||
|
"post": post, |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
func PostsDelete(c *gin.Context) { |
||||||
|
// Get the id off the URL
|
||||||
|
id := c.Param("id") |
||||||
|
|
||||||
|
// Delete the post
|
||||||
|
initializers.DB.Delete(&models.Post{}, id) |
||||||
|
|
||||||
|
// Respond
|
||||||
|
c.Status(200) |
||||||
|
} |
@ -0,0 +1,256 @@ |
|||||||
|
package controllers |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"net/http" |
||||||
|
"os" |
||||||
|
"strconv" |
||||||
|
"strings" |
||||||
|
"time" |
||||||
|
|
||||||
|
"go-crud/initializers" |
||||||
|
"go-crud/models" |
||||||
|
|
||||||
|
"github.com/gin-gonic/gin" |
||||||
|
"github.com/golang-jwt/jwt/v4" |
||||||
|
"golang.org/x/crypto/bcrypt" |
||||||
|
"gorm.io/gorm" |
||||||
|
) |
||||||
|
|
||||||
|
func SignUp(c *gin.Context) { |
||||||
|
var body struct { |
||||||
|
Email string |
||||||
|
Password string |
||||||
|
Nik string |
||||||
|
Name string |
||||||
|
Photo string |
||||||
|
Birthdate time.Time |
||||||
|
JobTitle string |
||||||
|
CreatedBy string |
||||||
|
UpdatedBy string |
||||||
|
DeletedBy string |
||||||
|
} |
||||||
|
|
||||||
|
if err := c.ShouldBindJSON(&body); err != nil { |
||||||
|
c.JSON(http.StatusBadRequest, gin.H{ |
||||||
|
"error": "Failed to read body", |
||||||
|
}) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
hash, err := bcrypt.GenerateFromPassword([]byte(body.Password), 10) |
||||||
|
if err != nil { |
||||||
|
c.JSON(http.StatusBadRequest, gin.H{ |
||||||
|
"error": "Failed to generate password hash", |
||||||
|
}) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
user := models.User{ |
||||||
|
Email: body.Email, |
||||||
|
Password: string(hash), |
||||||
|
Nik: body.Nik, |
||||||
|
Name: body.Name, |
||||||
|
Photo: body.Photo, |
||||||
|
Birthdate: body.Birthdate, |
||||||
|
JobTitle: body.JobTitle, |
||||||
|
LastLoginAt: time.Now(), |
||||||
|
CreatedBy: body.Nik, |
||||||
|
UpdatedBy: body.Nik, |
||||||
|
DeletedBy: body.Nik, |
||||||
|
Token: "", |
||||||
|
} |
||||||
|
|
||||||
|
result := initializers.DB.Create(&user) |
||||||
|
|
||||||
|
if result.Error != nil { |
||||||
|
c.JSON(http.StatusBadRequest, gin.H{ |
||||||
|
"error": "Failed to create user", |
||||||
|
}) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
updateLoginRecord(c, user.ID, time.Now()) |
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{}) |
||||||
|
} |
||||||
|
|
||||||
|
func LogIn(c *gin.Context) { |
||||||
|
var body struct { |
||||||
|
Email string |
||||||
|
Password string |
||||||
|
} |
||||||
|
if err := c.ShouldBindJSON(&body); err != nil { |
||||||
|
c.JSON(http.StatusBadRequest, gin.H{ |
||||||
|
"error": "Failed to read body", |
||||||
|
}) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
var user models.User |
||||||
|
initializers.DB.First(&user, "email = ?", body.Email) |
||||||
|
if user.ID == 0 { |
||||||
|
c.JSON(http.StatusBadRequest, gin.H{ |
||||||
|
"error": "Invalid email or password", |
||||||
|
}) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(body.Password)) |
||||||
|
if err != nil { |
||||||
|
c.JSON(http.StatusBadRequest, gin.H{ |
||||||
|
"error": "Invalid email or password", |
||||||
|
}) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
user.LastLoginAt = time.Now() |
||||||
|
initializers.DB.Save(&user) |
||||||
|
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ |
||||||
|
"sub": user.ID, |
||||||
|
"exp": time.Now().Add(time.Hour * 24 * 30).Unix(), |
||||||
|
}) |
||||||
|
|
||||||
|
tokenString, err := token.SignedString([]byte(os.Getenv("SECRET"))) |
||||||
|
if err != nil { |
||||||
|
c.JSON(http.StatusBadRequest, gin.H{ |
||||||
|
"error": "Failed to create token", |
||||||
|
}) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
c.SetSameSite(http.SameSiteLaxMode) |
||||||
|
c.SetCookie("Authorization", tokenString, 3600*24*30, "", "", false, true) |
||||||
|
c.JSON(http.StatusOK, gin.H{ |
||||||
|
"token": tokenString, |
||||||
|
}) |
||||||
|
|
||||||
|
updateLoginRecord(c, user.ID, time.Now()) |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
func LogOut(c *gin.Context) { |
||||||
|
c.SetCookie("Authorization", "", -1, "/", "", false, true) |
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{ |
||||||
|
"message": "Logout successful", |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
func Validate(c *gin.Context) { |
||||||
|
user, _ := c.Get("user") |
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{ |
||||||
|
"message": user, |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
func updateLoginRecord(c *gin.Context, userID uint, date time.Time) { |
||||||
|
// Cari data rekap login berdasarkan tanggal
|
||||||
|
var loginRecord models.LoginRecord |
||||||
|
result := initializers.DB.Where("login_date = ?", date.Format("2006-01-02")).First(&loginRecord) |
||||||
|
|
||||||
|
if result.Error != nil && result.Error != gorm.ErrRecordNotFound { |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
if result.Error == gorm.ErrRecordNotFound { |
||||||
|
// Jika belum ada data rekap untuk hari ini, buat data baru
|
||||||
|
loginRecord = models.LoginRecord{ |
||||||
|
LoginDate: date, |
||||||
|
UserIDs: fmt.Sprintf("%d", userID), |
||||||
|
} |
||||||
|
initializers.DB.Create(&loginRecord) |
||||||
|
} else { |
||||||
|
// Jika sudah ada data rekap, cek apakah user sudah login pada hari ini
|
||||||
|
if !containsInt(loginRecord.UserIDs, userID) { |
||||||
|
// Jika user belum login hari ini, tambahkan ID pengguna ke dalam array user_ids
|
||||||
|
loginRecord.UserIDs = fmt.Sprintf("%s,%d", loginRecord.UserIDs, userID) |
||||||
|
initializers.DB.Save(&loginRecord) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Ambil data pengguna yang login pada hari ini
|
||||||
|
var users []models.User |
||||||
|
initializers.DB.Find(&users, "id IN (?)", parseUserIDs(loginRecord.UserIDs)) |
||||||
|
|
||||||
|
// Update login record dengan pengguna yang login pada hari ini
|
||||||
|
loginRecord.Users = users |
||||||
|
|
||||||
|
initializers.DB.Save(&loginRecord) |
||||||
|
|
||||||
|
// Update LastLoginAt pada tabel User jika dalam satu hari user melakukan login kembali
|
||||||
|
user := models.User{} |
||||||
|
initializers.DB.First(&user, userID) |
||||||
|
user.LastLoginAt = date |
||||||
|
initializers.DB.Save(&user) |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// Helper function to check if an int is present in a slice
|
||||||
|
func containsInt(slice string, val uint) bool { |
||||||
|
ids := parseUserIDs(slice) |
||||||
|
for _, item := range ids { |
||||||
|
if item == val { |
||||||
|
return true |
||||||
|
} |
||||||
|
} |
||||||
|
return false |
||||||
|
} |
||||||
|
|
||||||
|
// Helper function to parse user IDs from a string
|
||||||
|
func parseUserIDs(idsStr string) []uint { |
||||||
|
ids := strings.Split(idsStr, ",") |
||||||
|
var userIDs []uint |
||||||
|
for _, idStr := range ids { |
||||||
|
id, err := strconv.ParseUint(idStr, 10, 32) |
||||||
|
if err != nil { |
||||||
|
continue |
||||||
|
} |
||||||
|
userIDs = append(userIDs, uint(id)) |
||||||
|
} |
||||||
|
return userIDs |
||||||
|
} |
||||||
|
|
||||||
|
func GetLoginRecords(c *gin.Context) { |
||||||
|
var loginRecords []models.LoginRecord |
||||||
|
result := initializers.DB.Find(&loginRecords) |
||||||
|
|
||||||
|
if result.Error != nil { |
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{ |
||||||
|
"code": http.StatusInternalServerError, |
||||||
|
"data": nil, |
||||||
|
"message": "Failed to fetch login records", |
||||||
|
"status": false, |
||||||
|
"totalRecord": 0, |
||||||
|
}) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
// Retrieve user details for each login record
|
||||||
|
for i := range loginRecords { |
||||||
|
var users []models.User |
||||||
|
initializers.DB.Find(&users, "id IN (?)", parseUserIDs(loginRecords[i].UserIDs)) |
||||||
|
|
||||||
|
loginRecords[i].Users = users |
||||||
|
} |
||||||
|
|
||||||
|
// Create JSON response
|
||||||
|
var response struct { |
||||||
|
Code int `json:"code"` |
||||||
|
Data []models.LoginRecord `json:"data"` |
||||||
|
Message string `json:"message"` |
||||||
|
Status bool `json:"status"` |
||||||
|
TotalRecord int `json:"totalRecord"` |
||||||
|
} |
||||||
|
|
||||||
|
response.Code = http.StatusOK |
||||||
|
response.Data = loginRecords |
||||||
|
response.Message = "OK" |
||||||
|
response.Status = true |
||||||
|
response.TotalRecord = len(loginRecords) |
||||||
|
|
||||||
|
c.JSON(http.StatusOK, response) |
||||||
|
} |
Binary file not shown.
@ -0,0 +1,47 @@ |
|||||||
|
module go-crud |
||||||
|
|
||||||
|
go 1.20 |
||||||
|
|
||||||
|
require gorm.io/driver/postgres v1.5.2 |
||||||
|
|
||||||
|
require ( |
||||||
|
github.com/bytedance/sonic v1.9.2 // indirect |
||||||
|
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect |
||||||
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible |
||||||
|
github.com/fatih/color v1.9.0 // indirect |
||||||
|
github.com/fsnotify/fsnotify v1.4.9 // indirect |
||||||
|
github.com/gabriel-vasile/mimetype v1.4.2 // indirect |
||||||
|
github.com/gin-contrib/sse v0.1.0 // indirect |
||||||
|
github.com/gin-gonic/gin v1.9.1 // indirect |
||||||
|
github.com/githubnemo/CompileDaemon v1.4.0 // indirect |
||||||
|
github.com/go-playground/locales v0.14.1 // indirect |
||||||
|
github.com/go-playground/universal-translator v0.18.1 // indirect |
||||||
|
github.com/go-playground/validator/v10 v10.14.1 // indirect |
||||||
|
github.com/goccy/go-json v0.10.2 // indirect |
||||||
|
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect |
||||||
|
github.com/jackc/pgpassfile v1.0.0 // indirect |
||||||
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect |
||||||
|
github.com/jackc/pgx/v5 v5.3.1 // indirect |
||||||
|
github.com/jinzhu/inflection v1.0.0 // indirect |
||||||
|
github.com/jinzhu/now v1.1.5 // indirect |
||||||
|
github.com/joho/godotenv v1.5.1 // indirect |
||||||
|
github.com/json-iterator/go v1.1.12 // indirect |
||||||
|
github.com/klauspost/cpuid/v2 v2.2.5 // indirect |
||||||
|
github.com/leodido/go-urn v1.2.4 // indirect |
||||||
|
github.com/mattn/go-colorable v0.1.4 // indirect |
||||||
|
github.com/mattn/go-isatty v0.0.19 // indirect |
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect |
||||||
|
github.com/modern-go/reflect2 v1.0.2 // indirect |
||||||
|
github.com/pelletier/go-toml/v2 v2.0.8 // indirect |
||||||
|
github.com/radovskyb/watcher v1.0.7 // indirect |
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect |
||||||
|
github.com/ugorji/go/codec v1.2.11 // indirect |
||||||
|
golang.org/x/arch v0.3.0 // indirect |
||||||
|
golang.org/x/crypto v0.10.0 // indirect |
||||||
|
golang.org/x/net v0.11.0 // indirect |
||||||
|
golang.org/x/sys v0.9.0 // indirect |
||||||
|
golang.org/x/text v0.10.0 // indirect |
||||||
|
google.golang.org/protobuf v1.31.0 // indirect |
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect |
||||||
|
gorm.io/gorm v1.25.2 // indirect |
||||||
|
) |
@ -0,0 +1,115 @@ |
|||||||
|
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= |
||||||
|
github.com/bytedance/sonic v1.9.2 h1:GDaNjuWSGu09guE9Oql0MSTNhNCLlWwO8y/xM5BzcbM= |
||||||
|
github.com/bytedance/sonic v1.9.2/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= |
||||||
|
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= |
||||||
|
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= |
||||||
|
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= |
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= |
||||||
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= |
||||||
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= |
||||||
|
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= |
||||||
|
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= |
||||||
|
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= |
||||||
|
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= |
||||||
|
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= |
||||||
|
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= |
||||||
|
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= |
||||||
|
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= |
||||||
|
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= |
||||||
|
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= |
||||||
|
github.com/githubnemo/CompileDaemon v1.4.0 h1:z96Qu4tj+RzRfF+L7f1O6E8ion5JQlisWeXWc2wzwDQ= |
||||||
|
github.com/githubnemo/CompileDaemon v1.4.0/go.mod h1:/G125r3YBIp6rcXtCZfiEHwFzcl7GSsNSwylxSNrkMA= |
||||||
|
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= |
||||||
|
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= |
||||||
|
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= |
||||||
|
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= |
||||||
|
github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+jU0zvx4AqHGnv4k= |
||||||
|
github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= |
||||||
|
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= |
||||||
|
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= |
||||||
|
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= |
||||||
|
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= |
||||||
|
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= |
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= |
||||||
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= |
||||||
|
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= |
||||||
|
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= |
||||||
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= |
||||||
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= |
||||||
|
github.com/jackc/pgx/v5 v5.3.1 h1:Fcr8QJ1ZeLi5zsPZqQeUZhNhxfkkKBOgJuYkJHoBOtU= |
||||||
|
github.com/jackc/pgx/v5 v5.3.1/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHozM/8= |
||||||
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= |
||||||
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= |
||||||
|
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= |
||||||
|
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= |
||||||
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= |
||||||
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= |
||||||
|
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= |
||||||
|
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= |
||||||
|
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= |
||||||
|
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= |
||||||
|
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= |
||||||
|
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= |
||||||
|
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= |
||||||
|
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= |
||||||
|
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= |
||||||
|
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= |
||||||
|
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= |
||||||
|
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= |
||||||
|
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= |
||||||
|
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= |
||||||
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= |
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= |
||||||
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= |
||||||
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= |
||||||
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= |
||||||
|
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= |
||||||
|
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= |
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
||||||
|
github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE= |
||||||
|
github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg= |
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= |
||||||
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= |
||||||
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= |
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= |
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= |
||||||
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= |
||||||
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= |
||||||
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= |
||||||
|
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= |
||||||
|
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= |
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= |
||||||
|
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= |
||||||
|
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= |
||||||
|
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= |
||||||
|
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= |
||||||
|
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= |
||||||
|
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= |
||||||
|
golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM= |
||||||
|
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= |
||||||
|
golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU= |
||||||
|
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ= |
||||||
|
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |
||||||
|
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
||||||
|
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= |
||||||
|
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= |
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||||
|
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= |
||||||
|
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||||
|
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58= |
||||||
|
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= |
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
||||||
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= |
||||||
|
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= |
||||||
|
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= |
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
||||||
|
gorm.io/driver/postgres v1.5.2 h1:ytTDxxEv+MplXOfFe3Lzm7SjG09fcdb3Z/c056DTBx0= |
||||||
|
gorm.io/driver/postgres v1.5.2/go.mod h1:fmpX0m2I1PKuR7mKZiEluwrP3hbs+ps7JIGMUBpCgl8= |
||||||
|
gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho= |
||||||
|
gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= |
||||||
|
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= |
@ -0,0 +1,22 @@ |
|||||||
|
package initializers |
||||||
|
|
||||||
|
import( |
||||||
|
"log" |
||||||
|
"os" |
||||||
|
|
||||||
|
"gorm.io/gorm" |
||||||
|
"gorm.io/driver/postgres" |
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
var DB *gorm.DB |
||||||
|
|
||||||
|
func ConnectToDB(){ |
||||||
|
var err error |
||||||
|
dsn := os.Getenv("DB_URL") |
||||||
|
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) |
||||||
|
|
||||||
|
if err != nil{ |
||||||
|
log.Fatal("Failed to connect to database") |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,13 @@ |
|||||||
|
package initializers |
||||||
|
|
||||||
|
import( |
||||||
|
"log" |
||||||
|
"github.com/joho/godotenv" |
||||||
|
) |
||||||
|
|
||||||
|
func LoadEnvVariables(){ |
||||||
|
err := godotenv.Load() |
||||||
|
if err != nil{ |
||||||
|
log.Fatal("Error loading .env file") |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,10 @@ |
|||||||
|
package initializers |
||||||
|
|
||||||
|
import "go-crud/models" |
||||||
|
|
||||||
|
func SyncDatabase() { |
||||||
|
DB.AutoMigrate(&models.Post{}) |
||||||
|
DB.AutoMigrate(&models.Event{}) |
||||||
|
DB.AutoMigrate(&models.User{}) |
||||||
|
DB.AutoMigrate(&models.LoginRecord{}) |
||||||
|
} |
@ -0,0 +1,40 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"go-crud/controllers" |
||||||
|
"go-crud/initializers" |
||||||
|
"go-crud/middleware" |
||||||
|
|
||||||
|
"github.com/gin-gonic/gin" |
||||||
|
) |
||||||
|
|
||||||
|
func init() { |
||||||
|
initializers.LoadEnvVariables() |
||||||
|
initializers.ConnectToDB() |
||||||
|
initializers.SyncDatabase() |
||||||
|
} |
||||||
|
func main() { |
||||||
|
r := gin.Default() |
||||||
|
|
||||||
|
r.POST("/signup", controllers.SignUp) |
||||||
|
r.POST("/login", controllers.LogIn) |
||||||
|
r.GET("/login-records", controllers.GetLoginRecords) |
||||||
|
|
||||||
|
r.GET("/validate", middleware.RequireAuth, controllers.Validate) |
||||||
|
|
||||||
|
r.Use(middleware.RequireAuth) |
||||||
|
r.POST("/logout", controllers.LogOut) |
||||||
|
r.POST("/posts", controllers.PostsCreate) |
||||||
|
r.PUT("/posts/:id", controllers.PostsUpdate) |
||||||
|
r.GET("/posts", controllers.PostsIndex) |
||||||
|
r.GET("/posts/:id", controllers.PostsShow) |
||||||
|
r.DELETE("/posts/:id", controllers.PostsDelete) |
||||||
|
|
||||||
|
r.POST("/events", controllers.EventsCreate) |
||||||
|
r.PUT("/events/:id", controllers.EventsUpdate) |
||||||
|
r.GET("/events", controllers.EventsIndex) |
||||||
|
r.GET("/events/:id", controllers.EventsShow) |
||||||
|
r.DELETE("/events/:id", controllers.EventsDelete) |
||||||
|
|
||||||
|
r.Run() // listen and serve on 0.0.0.0:8080
|
||||||
|
} |
@ -0,0 +1,75 @@ |
|||||||
|
package middleware |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"net/http" |
||||||
|
"os" |
||||||
|
"time" |
||||||
|
|
||||||
|
"go-crud/initializers" |
||||||
|
"go-crud/models" |
||||||
|
|
||||||
|
"github.com/gin-gonic/gin" |
||||||
|
"github.com/golang-jwt/jwt/v4" |
||||||
|
) |
||||||
|
|
||||||
|
func RequireAuth(c *gin.Context) { |
||||||
|
// Get the cookie off req
|
||||||
|
tokenString, err := c.Cookie("Authorization") |
||||||
|
if err != nil { |
||||||
|
c.AbortWithStatus(http.StatusUnauthorized) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
// Decode/validate it
|
||||||
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { |
||||||
|
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { |
||||||
|
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) |
||||||
|
} |
||||||
|
|
||||||
|
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
|
||||||
|
return []byte(os.Getenv("SECRET")), nil |
||||||
|
}) |
||||||
|
|
||||||
|
if err != nil || !token.Valid { |
||||||
|
c.AbortWithStatus(http.StatusUnauthorized) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
claims, ok := token.Claims.(jwt.MapClaims) |
||||||
|
if !ok { |
||||||
|
c.AbortWithStatus(http.StatusUnauthorized) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
// Check the exp
|
||||||
|
if exp, ok := claims["exp"].(float64); ok && float64(time.Now().Unix()) > exp { |
||||||
|
c.AbortWithStatus(http.StatusUnauthorized) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
// Find the user with token sub
|
||||||
|
var user models.User |
||||||
|
initializers.DB.First(&user, claims["sub"]) |
||||||
|
|
||||||
|
var recordData models.LoginRecord |
||||||
|
initializers.DB.First(&recordData, claims["sub"]) |
||||||
|
|
||||||
|
var post models.Post |
||||||
|
initializers.DB.First(&post, claims["sub"]) |
||||||
|
|
||||||
|
var event models.Event |
||||||
|
initializers.DB.First(&event, claims["sub"]) |
||||||
|
|
||||||
|
if user.ID == 0 { |
||||||
|
c.AbortWithStatus(http.StatusUnauthorized) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// Attach to req
|
||||||
|
c.Set("user", user) |
||||||
|
|
||||||
|
// Continue
|
||||||
|
c.Next() |
||||||
|
} |
@ -0,0 +1,16 @@ |
|||||||
|
package models |
||||||
|
|
||||||
|
import ( |
||||||
|
"time" |
||||||
|
|
||||||
|
"gorm.io/gorm" |
||||||
|
) |
||||||
|
|
||||||
|
type Event struct { |
||||||
|
gorm.Model |
||||||
|
Name string |
||||||
|
Content string |
||||||
|
PostBy string |
||||||
|
Category string |
||||||
|
Date *time.Time |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
package models |
||||||
|
|
||||||
|
import "gorm.io/gorm" |
||||||
|
|
||||||
|
type Post struct { |
||||||
|
gorm.Model |
||||||
|
Title string |
||||||
|
Body string |
||||||
|
} |
@ -0,0 +1,14 @@ |
|||||||
|
package models |
||||||
|
|
||||||
|
import ( |
||||||
|
"time" |
||||||
|
|
||||||
|
"gorm.io/gorm" |
||||||
|
) |
||||||
|
|
||||||
|
type LoginRecord struct { |
||||||
|
gorm.Model |
||||||
|
LoginDate time.Time |
||||||
|
UserIDs string `gorm:"type:text"` |
||||||
|
Users []User `json:"users" gorm:"-"` |
||||||
|
} |
@ -0,0 +1,29 @@ |
|||||||
|
package models |
||||||
|
|
||||||
|
import ( |
||||||
|
"time" |
||||||
|
|
||||||
|
"gorm.io/gorm" |
||||||
|
) |
||||||
|
|
||||||
|
type User struct { |
||||||
|
gorm.Model |
||||||
|
Nik string
|
||||||
|
Name string
|
||||||
|
Photo string
|
||||||
|
Birthdate time.Time
|
||||||
|
JobTitle string
|
||||||
|
Email string `gorm:"unique"` |
||||||
|
LastLoginAt time.Time
|
||||||
|
CreatedBy string
|
||||||
|
UpdatedBy string
|
||||||
|
DeletedBy string
|
||||||
|
Token string
|
||||||
|
Username string
|
||||||
|
RoleID int
|
||||||
|
Password string
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in new issue