Starting to improve the project structure
This commit is contained in:
66
cmd/CatsOfMastodonBotGo/main.go
Normal file
66
cmd/CatsOfMastodonBotGo/main.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal/helpers"
|
||||
"CatsOfMastodonBotGo/internal/models"
|
||||
"CatsOfMastodonBotGo/internal/server"
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Setup AppContext
|
||||
var appContext = helpers.SetupAppContext()
|
||||
|
||||
ticker := time.NewTicker(10 * 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 existing posts; %d new posts and %d new accounts\n", len(posts), len(existingPostIds), 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()
|
||||
}
|
||||
}()
|
||||
|
||||
// Run initial fetch on startup
|
||||
go func() {
|
||||
runFetchPosts()
|
||||
}()
|
||||
|
||||
// https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817
|
||||
|
||||
r := server.SetupRouter(appContext)
|
||||
err := r.Run(":8080")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user