63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"CatsOfMastodonBotGo/internal/helpers"
|
|
"CatsOfMastodonBotGo/internal/models"
|
|
"CatsOfMastodonBotGo/internal/server"
|
|
"context"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func main() {
|
|
// Setup AppContext
|
|
var appContext = helpers.SetupAppContext()
|
|
|
|
ticker := time.NewTicker(1 * time.Minute)
|
|
|
|
runFetchPosts := func() {
|
|
// Get posts
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
var posts []models.Post = nil
|
|
err, posts := appContext.PostService.GetPostsFromApi(ctx, appContext.Tag, appContext.Instance)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
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.Printf("Fetched %d posts, %d accounts; %d new posts and %d new accounts\n", len(posts), len(existingAccountIds), len(newPosts), len(newAccounts))
|
|
|
|
// Additional logging
|
|
if newAccounts != nil {
|
|
log.Printf("Inserted %d accounts\n", appContext.PostService.InsertNewAccounts(newAccounts))
|
|
}
|
|
if newPosts != nil {
|
|
log.Printf("Inserted %d posts\n", appContext.PostService.InsertNewPosts(newPosts))
|
|
}
|
|
}
|
|
|
|
go func() {
|
|
for range ticker.C {
|
|
runFetchPosts()
|
|
}
|
|
}()
|
|
|
|
// https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817
|
|
engine := gin.Default()
|
|
r := server.SetupRouter(engine)
|
|
err := r.Run(":8080")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
}
|