Refactor app structure: move models to domain, centralize config and database init - TODO: add jwt

This commit is contained in:
2025-05-17 20:07:29 +03:30
parent ab9254fcad
commit 7b601e75ba
12 changed files with 142 additions and 181 deletions
+22 -14
View File
@@ -1,18 +1,26 @@
package main package main
import ( import (
"CatsOfMastodonBotGo/internal/helpers" "CatsOfMastodonBotGo/internal/config"
"CatsOfMastodonBotGo/internal/models" "CatsOfMastodonBotGo/internal/database"
"CatsOfMastodonBotGo/internal/domain"
"CatsOfMastodonBotGo/internal/server" "CatsOfMastodonBotGo/internal/server"
"CatsOfMastodonBotGo/internal/services"
"context" "context"
"log" "log"
"time" "time"
) )
func main() { func main() {
// Setup AppContext // Setup config
var appContext = helpers.SetupAppContext() config.Init()
var config = config.Load()
// Initialize database
database.Init()
services.InitPostService()
ticker := time.NewTicker(10 * time.Minute) ticker := time.NewTicker(10 * time.Minute)
@@ -20,27 +28,27 @@ func main() {
// Get posts // Get posts
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
var posts []models.Post = nil var posts []domain.Post = nil
err, posts := appContext.PostService.GetPostsFromApi(ctx, appContext.Tag, appContext.Instance) err, posts := services.PostServiceInstance.GetPostsFromApi(ctx, config.Tag, config.Instance)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
} }
var existingPostIds = appContext.PostService.GetExistingPostIds() var existingPostIds = services.PostServiceInstance.GetExistingPostIds()
var existingAccountIds = appContext.PostService.GetExistingAccountIds() var existingAccountIds = services.PostServiceInstance.GetExistingAccountIds()
var newPosts = appContext.PostService.GetNewPosts(existingPostIds, posts) var newPosts = services.PostServiceInstance.GetNewPosts(existingPostIds, posts)
var newAccounts = appContext.PostService.GetNewAccounts(existingAccountIds, newPosts) var newAccounts = services.PostServiceInstance.GetNewAccounts(existingAccountIds, newPosts)
// Save to database // Save to database
log.Printf("Fetched %d posts; %d existing posts; %d new posts and %d new accounts\n", len(posts), len(existingPostIds), len(newPosts), len(newAccounts)) log.Printf("Fetched %d posts; %d existing posts; %d new posts and %d new accounts\n", len(posts), len(existingPostIds), len(newPosts), len(newAccounts))
// Additional logging // Additional logging
if newAccounts != nil { if newAccounts != nil {
log.Printf("Inserted %d accounts\n", appContext.PostService.InsertNewAccounts(newAccounts)) log.Printf("Inserted %d accounts\n", services.PostServiceInstance.InsertNewAccounts(newAccounts))
} }
if newPosts != nil { if newPosts != nil {
log.Printf("Inserted %d posts\n", appContext.PostService.InsertNewPosts(newPosts)) log.Printf("Inserted %d posts\n", services.PostServiceInstance.InsertNewPosts(newPosts))
} }
} }
@@ -57,7 +65,7 @@ func main() {
// https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817 // https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817
r := server.SetupRouter(appContext) r := server.SetupRouter()
err := r.Run(":8080") err := r.Run(":8080")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
+7 -1
View File
@@ -19,7 +19,9 @@ type config struct {
Tag string Tag string
} }
func SetupAppContext() *config { var Config *config
func Load() *config {
// Get mastodon instance // Get mastodon instance
instance := os.Getenv("CAOM_INSTANCE") instance := os.Getenv("CAOM_INSTANCE")
if instance == "" { if instance == "" {
@@ -77,3 +79,7 @@ func SetupAppContext() *config {
return appContext return appContext
} }
func Init() {
Config = Load()
}
+11
View File
@@ -7,6 +7,8 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
var Gorm *gorm.DB
func Connect() (*gorm.DB, error) { func Connect() (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil { if err != nil {
@@ -18,3 +20,12 @@ func Connect() (*gorm.DB, error) {
} }
return db, nil return db, nil
} }
// IDK if this is how it works or not, leave it as is for now
func Init () {
var err error
Gorm, err = Connect()
if err != nil {
panic(err)
}
}
-11
View File
@@ -1,11 +0,0 @@
package domain
type Account struct {
AccId string `json:"id" gorm:"primaryKey"`
Username string `json:"username"`
Acct string `json:"acct"`
DisplayName string `json:"display_name"`
IsBot bool `json:"bot"`
Url string `json:"url"`
AvatarStatic string `json:"avatar_static"`
}
-12
View File
@@ -1,12 +0,0 @@
package domain
type MediaAttachment struct {
ID string `json:"id" gorm:"primaryKey"`
Type string `json:"type"`
Url string `json:"url"`
PreviewUrl string `json:"preview_url"`
RemoteUrl string `json:"remote_url"`
PostID string // Foreign key to Post
Approved bool `json:"approved"`
Rejected bool `json:"rejected"`
}
+21
View File
@@ -7,3 +7,24 @@ type Post struct {
Account Account `json:"account" gorm:"foreignKey:AccountID;references:AccId"` Account Account `json:"account" gorm:"foreignKey:AccountID;references:AccId"`
Attachments []MediaAttachment `json:"media_attachments" gorm:"foreignKey:PostID;references:ID"` Attachments []MediaAttachment `json:"media_attachments" gorm:"foreignKey:PostID;references:ID"`
} }
type Account struct {
AccId string `json:"id" gorm:"primaryKey"`
Username string `json:"username"`
Acct string `json:"acct"`
DisplayName string `json:"display_name"`
IsBot bool `json:"bot"`
Url string `json:"url"`
AvatarStatic string `json:"avatar_static"`
}
type MediaAttachment struct {
ID string `json:"id" gorm:"primaryKey"`
Type string `json:"type"`
Url string `json:"url"`
PreviewUrl string `json:"preview_url"`
RemoteUrl string `json:"remote_url"`
PostID string // Foreign key to Post
Approved bool `json:"approved"`
Rejected bool `json:"rejected"`
}
-71
View File
@@ -1,71 +0,0 @@
package helpers
import (
"CatsOfMastodonBotGo/internal"
"CatsOfMastodonBotGo/internal/auth"
"CatsOfMastodonBotGo/internal/services"
"log"
"os"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func SetupAppContext() *internal.AppContext {
// Setup AppContext
instance := os.Getenv("CAOM_INSTANCE")
if instance == "" {
instance = "https://mstdn.party"
}
tag := os.Getenv("CAOM_TAG")
if tag == "" {
tag = "catsofmastodon"
}
adminPassword := os.Getenv("CAOM_ADMIN_PASSWORD")
if adminPassword == "" {
log.Println("No admin password provided, using default password 'catsaregood'")
adminPassword = "catsaregood"
}
// Jwt params
secret := os.Getenv("CAOM_JWT_SECRET")
if secret == "" {
log.Fatal("No jwt secret provided, using default secret 'secret'")
}
issuer := os.Getenv("CAOM_JWT_ISSUER")
if issuer == "" {
log.Println("No jwt issuer provided, using default issuer 'CatsOfMastodonBotGo'")
issuer = "CatsOfMastodonBotGo"
}
audience := os.Getenv("CAOM_JWT_AUDIENCE")
if audience == "" {
log.Println("No jwt audience provided, using default audience 'CatsOfMastodonBotGo'")
audience = "CatsOfMastodonBotGo"
}
// Setup 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")
}
AddMigrations(db)
//Setup PostService
var postService = services.NewPostService(db)
// Setup Jwt
var jwt = auth.NewJwtTokenGenerator(secret, issuer, audience)
// Inititlize AppContext
var appContext = &internal.AppContext{
Db: db,
PostService: postService,
Jwt: jwt,
AdminPassword: adminPassword,
Instance: instance,
Tag: tag,
}
return appContext
}
+9 -10
View File
@@ -2,14 +2,13 @@ package server
import ( import (
"CatsOfMastodonBotGo/internal" "CatsOfMastodonBotGo/internal"
handlers_admin "CatsOfMastodonBotGo/internal/web/handlers/admin" "CatsOfMastodonBotGo/internal/web/handlers"
handlers_api "CatsOfMastodonBotGo/internal/web/handlers/api"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func SetupRouter(appContext *internal.AppContext) *gin.Engine { func SetupRouter() *gin.Engine {
r := gin.Default() r := gin.Default()
r.Use(cors.New(cors.Config{ r.Use(cors.New(cors.Config{
@@ -19,20 +18,20 @@ func SetupRouter(appContext *internal.AppContext) *gin.Engine {
AllowCredentials: true, AllowCredentials: true,
})) }))
adminDashboardHandler := handlers_admin.NewAdminDashboardHandler(appContext) handlers.InitAdminDashboardHandler()
apiHandler := handlers_api.NewApiEndpointHandler(appContext) handlers.InitApiEndpointHandler()
admin := r.Group("/admin") admin := r.Group("/admin")
// My man, this is done way more efficient and fast in .NET, specially the authentication part // My man, this is done way more efficient and fast in .NET, specially the authentication part
admin.POST("/login", adminDashboardHandler.Login) admin.POST("/login", handlers.AdminDashboardHandlerInstance.Login)
admin.GET("/getmedia", appContext.Jwt.GinMiddleware(), adminDashboardHandler.GetMedia) admin.GET("/getmedia", appContext.Jwt.GinMiddleware(), handlers.AdminDashboardHandlerInstance.GetMedia)
admin.POST("/approve", appContext.Jwt.GinMiddleware(), adminDashboardHandler.ApproveMedia) admin.POST("/approve", appContext.Jwt.GinMiddleware(), handlers.AdminDashboardHandlerInstance.ApproveMedia)
admin.POST("/reject", appContext.Jwt.GinMiddleware(), adminDashboardHandler.RejectMedia) admin.POST("/reject", appContext.Jwt.GinMiddleware(), handlers.AdminDashboardHandlerInstance.RejectMedia)
api := r.Group("/api") api := r.Group("/api")
api.GET("/post/random", apiHandler.GetRandomPost) api.GET("/post/random", handlers.ApiEndpointHandlerInstance.GetRandomPost)
return r return r
} }
+24 -21
View File
@@ -1,7 +1,8 @@
package services package services
import ( import (
"CatsOfMastodonBotGo/internal/models" "CatsOfMastodonBotGo/internal/database"
"CatsOfMastodonBotGo/internal/domain"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
@@ -16,12 +17,14 @@ type PostService struct {
db *gorm.DB db *gorm.DB
} }
var PostServiceInstance *PostService
// Constructor // Constructor
func NewPostService(db *gorm.DB) *PostService { func InitPostService() {
return &PostService{db: db} PostServiceInstance = &PostService{db: database.Gorm}
} }
func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance string) (error, []models.Post) { func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance string) (error, []domain.Post) {
var requestUrl = instance + "/api/v1/timelines/tag/" + tag + "?limit=40" var requestUrl = instance + "/api/v1/timelines/tag/" + tag + "?limit=40"
req, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil) req, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil)
if err != nil { if err != nil {
@@ -36,7 +39,7 @@ func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance st
return fmt.Errorf("Status code:", resp.StatusCode, " Content-Type:", resp.Header.Get("Content-Type")), nil return fmt.Errorf("Status code:", resp.StatusCode, " Content-Type:", resp.Header.Get("Content-Type")), nil
} }
var posts []models.Post = nil var posts []domain.Post = nil
err = json.NewDecoder(resp.Body).Decode(&posts) err = json.NewDecoder(resp.Body).Decode(&posts)
if err != nil { if err != nil {
return err, nil return err, nil
@@ -51,19 +54,19 @@ func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance st
func (ps *PostService) GetExistingPostIds() []string { func (ps *PostService) GetExistingPostIds() []string {
var existingPostIds []string var existingPostIds []string
ps.db.Model(&models.Post{}).Pluck("id", &existingPostIds) ps.db.Model(&domain.Post{}).Pluck("id", &existingPostIds)
return existingPostIds return existingPostIds
} }
func (ps *PostService) GetExistingAccountIds() []string { func (ps *PostService) GetExistingAccountIds() []string {
var existingAccountIds []string var existingAccountIds []string
ps.db.Model(&models.Account{}).Pluck("acc_id", &existingAccountIds) ps.db.Model(&domain.Account{}).Pluck("acc_id", &existingAccountIds)
return existingAccountIds return existingAccountIds
} }
func (*PostService) GetNewPosts(existingPostIds []string, posts []models.Post) []models.Post { func (*PostService) GetNewPosts(existingPostIds []string, posts []domain.Post) []domain.Post {
var newPosts []models.Post = nil var newPosts []domain.Post = nil
for _, post := range posts { for _, post := range posts {
if !arrayContains(existingPostIds, post.ID) && len(post.Attachments) > 0 && !post.Account.IsBot { if !arrayContains(existingPostIds, post.ID) && len(post.Attachments) > 0 && !post.Account.IsBot {
var allImageMedia = true var allImageMedia = true
@@ -81,8 +84,8 @@ func (*PostService) GetNewPosts(existingPostIds []string, posts []models.Post) [
return newPosts return newPosts
} }
func (*PostService) GetNewAccounts(existingAccountIds []string, posts []models.Post) []models.Account { func (*PostService) GetNewAccounts(existingAccountIds []string, posts []domain.Post) []domain.Account {
var newAccounts []models.Account = nil var newAccounts []domain.Account = nil
for _, post := range posts { for _, post := range posts {
if !arrayContains(existingAccountIds, post.Account.AccId) { if !arrayContains(existingAccountIds, post.Account.AccId) {
newAccounts = append(newAccounts, post.Account) newAccounts = append(newAccounts, post.Account)
@@ -91,45 +94,45 @@ func (*PostService) GetNewAccounts(existingAccountIds []string, posts []models.P
return newAccounts return newAccounts
} }
func (ps *PostService) InsertNewPosts(newPosts []models.Post) int { func (ps *PostService) InsertNewPosts(newPosts []domain.Post) int {
return int(ps.db.Create(&newPosts).RowsAffected) return int(ps.db.Create(&newPosts).RowsAffected)
} }
func (ps *PostService) InsertNewAccounts(newAccounts []models.Account) int { func (ps *PostService) InsertNewAccounts(newAccounts []domain.Account) int {
return int(ps.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&newAccounts).RowsAffected) return int(ps.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&newAccounts).RowsAffected)
} }
// From this point on, its for the api endpoints // From this point on, its for the api endpoints
func (ps *PostService) GetRandomPost() models.Post { func (ps *PostService) GetRandomPost() domain.Post {
var post models.Post var post domain.Post
ps.db. ps.db.
Preload("Account"). Preload("Account").
Preload("Attachments", "approved = ?", true). Preload("Attachments", "approved = ?", true).
Order("RANDOM()"). Order("RANDOM()").
First(&post) First(&post)
if len(post.Attachments) > 0 { if len(post.Attachments) > 0 {
post.Attachments = []models.MediaAttachment{post.Attachments[0]} post.Attachments = []domain.MediaAttachment{post.Attachments[0]}
} }
return post return post
} }
func (ps *PostService) ApproveMedia(mediaId string) bool { func (ps *PostService) ApproveMedia(mediaId string) bool {
return ps.db.Model(&models.MediaAttachment{}). return ps.db.Model(&domain.MediaAttachment{}).
Where("id = ?", mediaId). Where("id = ?", mediaId).
Update("approved", true).RowsAffected > 0 Update("approved", true).RowsAffected > 0
} }
func (ps *PostService) RejectMedia(mediaId string) bool { func (ps *PostService) RejectMedia(mediaId string) bool {
return ps.db.Model(&models.MediaAttachment{}). return ps.db.Model(&domain.MediaAttachment{}).
Where("id = ?", mediaId). Where("id = ?", mediaId).
Update("rejected", true).RowsAffected > 0 Update("rejected", true).RowsAffected > 0
} }
// Get a post which approve and rejet are false (For admin panel) // Get a post which approve and rejet are false (For admin panel)
func (ps *PostService) GetMedia() models.MediaAttachment { func (ps *PostService) GetMedia() domain.MediaAttachment {
var media models.MediaAttachment var media domain.MediaAttachment
ps.db.Model(&models.MediaAttachment{}). ps.db.Model(&domain.MediaAttachment{}).
Where("approved = ?", false). Where("approved = ?", false).
Where("rejected = ?", false). Where("rejected = ?", false).
Order("RANDOM()"). Order("RANDOM()").
@@ -1,56 +1,59 @@
package handlers_admin package handlers
import ( import (
"CatsOfMastodonBotGo/internal"
"net/http" "net/http"
requestmodels "CatsOfMastodonBotGo/internal/models/requestModels" "CatsOfMastodonBotGo/internal/config"
requestmodels "CatsOfMastodonBotGo/internal/domain/requestModels"
"CatsOfMastodonBotGo/internal/services"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type AdminDashboardHandler struct { type AdminDashboardHandler struct {
AppContext *internal.AppContext PostService services.PostService
} }
func NewAdminDashboardHandler(appContext *internal.AppContext) *AdminDashboardHandler { var AdminDashboardHandlerInstance *AdminDashboardHandler
return &AdminDashboardHandler{
AppContext: appContext, func InitAdminDashboardHandler() {
AdminDashboardHandlerInstance = &AdminDashboardHandler{
PostService: *services.PostServiceInstance,
} }
} }
func (appContext *AdminDashboardHandler) ApproveMedia(c *gin.Context) { func (ps *AdminDashboardHandler) ApproveMedia(c *gin.Context) {
var input requestmodels.ApproveMediaInput var input requestmodels.ApproveMediaInput
if err := c.ShouldBindJSON(&input); err != nil { if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return return
} }
if appContext.AppContext.PostService.ApproveMedia(input.MediaId) { if ps.PostService.ApproveMedia(input.MediaId) {
c.JSON(http.StatusOK, gin.H{"message": "Media approved successfully"}) c.JSON(http.StatusOK, gin.H{"message": "Media approved successfully"})
} else { } else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to approve media"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to approve media"})
} }
} }
func (appContext *AdminDashboardHandler) RejectMedia(c *gin.Context) { func (ps *AdminDashboardHandler) RejectMedia(c *gin.Context) {
var input requestmodels.RejectMediaInput var input requestmodels.RejectMediaInput
if err := c.ShouldBindJSON(&input); err != nil { if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return return
} }
if appContext.AppContext.PostService.RejectMedia(input.MediaId) { if ps.PostService.RejectMedia(input.MediaId) {
c.JSON(http.StatusOK, gin.H{"message": "Media rejected successfully"}) c.JSON(http.StatusOK, gin.H{"message": "Media rejected successfully"})
} else { } else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to reject media"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to reject media"})
} }
} }
func (appContext *AdminDashboardHandler) GetMedia(c *gin.Context) { func (ps *AdminDashboardHandler) GetMedia(c *gin.Context) {
media := appContext.AppContext.PostService.GetMedia() media := ps.PostService.GetMedia()
c.JSON(http.StatusOK, media) c.JSON(http.StatusOK, media)
} }
func (appContext *AdminDashboardHandler) Login(c *gin.Context) { func (ps *AdminDashboardHandler) Login(c *gin.Context) {
var input requestmodels.LoginInput var input requestmodels.LoginInput
@@ -60,14 +63,14 @@ func (appContext *AdminDashboardHandler) Login(c *gin.Context) {
return return
} }
if input.Password == appContext.AppContext.AdminPassword { // Its more than enough for this project if input.Password == config.Config.AdminPassword { // Its more than enough for this project
token, err := appContext.AppContext.Jwt.GenerateToken(map[string]interface{}{"role": "admin"}) // token, err := ps.ps.Jwt.GenerateToken(map[string]interface{}{"role": "admin"})
if err != nil { // if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"}) // c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"})
return // return
} // }
c.JSON(http.StatusOK, gin.H{"message": "Login successful", "token": token}) c.JSON(http.StatusOK, gin.H{"message": "Login successful", "token": "yoy"}) // TODO: Add token
} else { } else {
c.JSON(401, gin.H{ c.JSON(401, gin.H{
-20
View File
@@ -1,20 +0,0 @@
package handlers_api
import (
"CatsOfMastodonBotGo/internal"
"github.com/gin-gonic/gin"
)
type ApiEndpointHandler struct {
AppContext *internal.AppContext
}
func NewApiEndpointHandler(appContext *internal.AppContext) *ApiEndpointHandler {
return &ApiEndpointHandler{
AppContext: appContext,
}
}
func (appContext *ApiEndpointHandler) GetRandomPost(c *gin.Context) {
c.JSON(200,appContext.AppContext.PostService.GetRandomPost())
}
+24
View File
@@ -0,0 +1,24 @@
package handlers
import (
"CatsOfMastodonBotGo/internal/services"
"github.com/gin-gonic/gin"
)
type ApiEndpointHandler struct {
PostService services.PostService
}
var ApiEndpointHandlerInstance *ApiEndpointHandler
func InitApiEndpointHandler() {
ApiEndpointHandlerInstance = &ApiEndpointHandler{
PostService: *services.PostServiceInstance,
}
}
func (ps *ApiEndpointHandler) GetRandomPost(c *gin.Context) {
c.JSON(200,ps.PostService.GetRandomPost())
}