Add user management and improve code organization with context structs

This commit is contained in:
2025-05-13 15:00:42 +03:30
parent 75bad5e091
commit 25d9b67be6
6 changed files with 33 additions and 1 deletions

View File

@@ -15,6 +15,7 @@ import (
)
func main() {
// Get environment variables
var tag = os.Getenv("COM_TAG")
if tag == "" {
tag = "catsofmastodon"
@@ -24,6 +25,7 @@ func main() {
instance = "https://haminoa.net"
}
// Receive posts
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err, posts := helpers.GetPosts(ctx, tag, instance)
@@ -32,23 +34,26 @@ func main() {
}
log.Println("Number of fetched posts: ", len(posts))
// Connect to database
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{Logger: logger.Default.LogMode(logger.Warn)})
if err != nil {
panic("failed to connect database")
}
helpers.AddMigrations(db)
// Process posts
var existingPostIds = helpers.GetExistingPostIds(db)
var existingAccountIds = helpers.GetExistingAccountIds(db)
var newPosts = helpers.GetNewPosts(existingPostIds, posts)
var newAccounts = helpers.GetNewAccounts(existingAccountIds, newPosts)
// Save to database
log.Println("Number of existing posts: ", len(existingPostIds))
log.Println("Number of existing accounts: ", len(existingAccountIds))
log.Println("Number of new posts: ", len(newPosts))
log.Println("Number of new accounts: ", len(newAccounts))
// Additional logging
if newAccounts != nil {
log.Println("Number of inserted accounts: ", helpers.InsertNewAccounts(db, newAccounts))
} else {

8
internal/AppContext.go Normal file
View File

@@ -0,0 +1,8 @@
package internal
import "gorm.io/gorm"
type AppContext struct {
Db *gorm.DB
}

View File

@@ -0,0 +1,7 @@
package internal
import "github.com/gin-gonic/gin"
type AppWebContext struct {
GinEngine *gin.Engine
}

View File

@@ -0,0 +1,10 @@
package models
// Funny you are
type ComUser struct {
Id string
Username string
Password string
Email string
IsVerified bool
}

View File

@@ -0,0 +1 @@
package repositories

View File

@@ -0,0 +1 @@
package services