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.Fatal(err) } 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("Number of existing posts: %d, existing accounts: %d, new posts: %d, new accounts: %d\n", len(existingPostIds), len(existingAccountIds), len(newPosts), len(newAccounts)) // Additional logging if newAccounts != nil { log.Println("Number of inserted accounts: ", appContext.PostService.InsertNewAccounts(newAccounts)) } else { log.Print(" - No new accounts inserted") } if newPosts != nil { log.Print(" - Number of inserted posts: ", appContext.PostService.InsertNewPosts(newPosts)) } else { log.Print(" - No new posts inserted") } } 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) } }