Refactor app context setup and move API fetch logic to PostService
This commit is contained in:
46
cmd/main.go
46
cmd/main.go
@@ -2,52 +2,32 @@ package main
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal/helpers"
|
||||
"CatsOfMastodonBotGo/internal/models"
|
||||
"CatsOfMastodonBotGo/internal/server"
|
||||
"CatsOfMastodonBotGo/internal/services"
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Get environment variables
|
||||
var tag = os.Getenv("COM_TAG")
|
||||
if tag == "" {
|
||||
tag = "catsofmastodon"
|
||||
}
|
||||
var instance = os.Getenv("COM_INSTANCE")
|
||||
if instance == "" {
|
||||
instance = "https://haminoa.net"
|
||||
}
|
||||
// Setup AppContext
|
||||
var appContext = helpers.SetupAppContext()
|
||||
|
||||
// Receive posts
|
||||
// Get posts
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
err, posts := helpers.GetPosts(ctx, tag, instance)
|
||||
var posts []models.Post = nil
|
||||
err, posts := appContext.PostService.GetPostsFromApi(ctx, appContext.Tag, appContext.Instance)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
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 postService = services.NewPostService(db)
|
||||
var existingPostIds = postService.GetExistingPostIds()
|
||||
var existingAccountIds = postService.GetExistingAccountIds()
|
||||
var newPosts = postService.GetNewPosts(existingPostIds, posts)
|
||||
var newAccounts = postService.GetNewAccounts(existingAccountIds, newPosts)
|
||||
var existingPostIds = appContext.PostService.GetExistingPostIds()
|
||||
var existingAccountIds = appContext.PostService.GetExistingAccountIds()
|
||||
var newPosts = appContext.PostService.GetNewPosts(existingPostIds, posts)
|
||||
var newAccounts = appContext.PostService.GetNewAccounts(existingAccountIds, newPosts)
|
||||
|
||||
// Save to database
|
||||
log.Println("Number of existing posts: ", len(existingPostIds))
|
||||
@@ -57,12 +37,12 @@ func main() {
|
||||
|
||||
// Additional logging
|
||||
if newAccounts != nil {
|
||||
log.Println("Number of inserted accounts: ", postService.InsertNewAccounts(newAccounts))
|
||||
log.Println("Number of inserted accounts: ", appContext.PostService.InsertNewAccounts(newAccounts))
|
||||
} else {
|
||||
log.Println("No new accounts inserted")
|
||||
}
|
||||
if newPosts != nil {
|
||||
log.Println("Number of inserted posts: ", postService.InsertNewPosts(newPosts))
|
||||
log.Println("Number of inserted posts: ", appContext.PostService.InsertNewPosts(newPosts))
|
||||
} else {
|
||||
log.Println("No new posts inserted")
|
||||
}
|
||||
|
@@ -10,4 +10,6 @@ import (
|
||||
type AppContext struct {
|
||||
Db *gorm.DB
|
||||
PostService *services.PostService
|
||||
Instance string
|
||||
Tag string
|
||||
}
|
44
internal/helpers/SetupAppContext.go
Normal file
44
internal/helpers/SetupAppContext.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal"
|
||||
"CatsOfMastodonBotGo/internal/services"
|
||||
"os"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func SetupAppContext() *internal.AppContext {
|
||||
// Setup AppContext
|
||||
instance := os.Getenv("INSTANCE")
|
||||
if instance == "" {
|
||||
instance = "https://haminoa.net"
|
||||
}
|
||||
tag := os.Getenv("TAG")
|
||||
if tag == "" {
|
||||
tag = "catsofmastodon"
|
||||
}
|
||||
|
||||
|
||||
// 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)
|
||||
|
||||
// Inititlize AppContext
|
||||
var appContext = &internal.AppContext{
|
||||
Db: db,
|
||||
PostService: postService,
|
||||
Instance: instance,
|
||||
Tag: tag,
|
||||
}
|
||||
return appContext
|
||||
|
||||
}
|
@@ -1,43 +0,0 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal/models"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetPosts(ctx context.Context, tag string, instance string) (error, []models.Post) {
|
||||
var requestUrl = instance + "/api/v1/timelines/tag/" + tag + "?limit=40"
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err, nil
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err, nil
|
||||
}
|
||||
if resp.StatusCode != 200 || strings.Split(strings.ToLower(resp.Header.Get("Content-Type")), ";")[0] != "application/json" {
|
||||
log.Fatal("Status code:", resp.StatusCode, " Content-Type:", resp.Header.Get("Content-Type"))
|
||||
return err, nil
|
||||
}
|
||||
|
||||
var posts []models.Post = nil
|
||||
err = json.NewDecoder(resp.Body).Decode(&posts)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err, nil
|
||||
}
|
||||
// defer: it basically means "do this later when the function returns"
|
||||
defer resp.Body.Close()
|
||||
if posts == nil {
|
||||
return fmt.Errorf("no posts found for tag %s on instance %s", tag, instance), nil
|
||||
}
|
||||
return nil, posts
|
||||
}
|
@@ -2,6 +2,12 @@ package services
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal/models"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -16,6 +22,38 @@ func NewPostService(db *gorm.DB) *PostService {
|
||||
return &PostService{db: db}
|
||||
}
|
||||
|
||||
func (ps *PostService) GetPostsFromApi(ctx context.Context, tag string, instance string) (error, []models.Post) {
|
||||
var requestUrl = instance + "/api/v1/timelines/tag/" + tag + "?limit=40"
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err, nil
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err, nil
|
||||
}
|
||||
if resp.StatusCode != 200 || strings.Split(strings.ToLower(resp.Header.Get("Content-Type")), ";")[0] != "application/json" {
|
||||
log.Fatal("Status code:", resp.StatusCode, " Content-Type:", resp.Header.Get("Content-Type"))
|
||||
return err, nil
|
||||
}
|
||||
|
||||
var posts []models.Post = nil
|
||||
err = json.NewDecoder(resp.Body).Decode(&posts)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err, nil
|
||||
}
|
||||
// defer: it basically means "do this later when the function returns"
|
||||
defer resp.Body.Close()
|
||||
if posts == nil {
|
||||
return fmt.Errorf("no posts found for tag %s on instance %s", tag, instance), nil
|
||||
}
|
||||
return nil, posts
|
||||
}
|
||||
|
||||
func (ps *PostService) GetExistingPostIds() []string {
|
||||
var existingPostIds []string
|
||||
ps.db.Model(&models.Post{}).Pluck("id", &existingPostIds)
|
||||
|
Reference in New Issue
Block a user