Compare commits

...

8 Commits

Author SHA1 Message Date
a7ef859c43 Fix api attachment urls 2025-07-29 20:40:53 +03:30
ba06de2c11 Update admin dash 2025-07-29 19:29:07 +03:30
8dcdd27745 Minor improvements to admin panel API 2025-07-29 19:21:15 +03:30
a43bcc9c14 Update admin panel 2025-07-29 19:10:12 +03:30
71800440be Add imagekit fallback on no imagekit ID 2025-07-29 17:55:16 +03:30
051408fcdd Added imagekit optimization support 2025-07-29 17:52:39 +03:30
48b893a403 remove unused code 2025-07-26 22:43:06 +03:30
0a0af6b04b Minor updates 2025-07-25 22:19:29 +03:30
10 changed files with 53 additions and 9 deletions

View File

@@ -21,6 +21,9 @@ func main() {
services.InitPostService()
// Not needed but anyways
services.InitImgKitHelper()
ticker := time.NewTicker(10 * time.Minute)
runFetchPosts := func() {

View File

@@ -22,6 +22,8 @@ type config struct {
DBUser string
DBPassword string
DBName string
ImageKitId string
}
var Config *config
@@ -29,7 +31,7 @@ var Config *config
func Load() *config {
err := godotenv.Load()
if err != nil {
slog.Error("Error loading .env file - Using environment variables instead")
slog.Warn("Error loading .env file - Using environment variables instead")
}
// Get mastodon instance
@@ -65,7 +67,6 @@ func Load() *config {
audience = "CatsOfMastodonBotGo"
}
dbEngine := os.Getenv("CAOM_DB_ENGINE")
dbHost := os.Getenv("CAOM_DB_HOST")
dbPort := os.Getenv("CAOM_DB_PORT")
@@ -82,6 +83,11 @@ func Load() *config {
dbPassword = ""
dbName = "caom.db"
}
imageKitId := os.Getenv("CAOM_IMAGEKIT_ID")
if imageKitId == "" {
slog.Info("No imagekit id provided, not using imagekit.io")
}
// Inititlize AppContext
var appContext = &config{
AdminPassword: adminPassword,
@@ -98,6 +104,8 @@ func Load() *config {
DBUser: dbUser,
DBPassword: dbPassword,
DBName: dbName,
ImageKitId: imageKitId,
}
return appContext

View File

@@ -0,0 +1,26 @@
package services
import "CatsOfMastodonBotGo/internal/config"
type ImgKitHelper struct {
}
var ImgKitHelperInstance *ImgKitHelper
func InitImgKitHelper() {
ImgKitHelperInstance = &ImgKitHelper{}
}
func GetPreviewUrl(url string) string {
if config.Config.ImageKitId == "" {
return url
}
return "https://ik.imagekit.io/" + config.Config.ImageKitId + "/tr:w-500,h-500,c-at_max,f-webp,q-50/" + url
}
func GetRemoteUrl(url string) string {
if config.Config.ImageKitId == "" {
return url
}
return "https://ik.imagekit.io/" + config.Config.ImageKitId + "/tr:q-70,dpr-auto,f-webp/" + url
}

View File

@@ -53,6 +53,8 @@ func (ps *AdminDashboardHandler) RejectMedia(c *gin.Context) {
func (ps *AdminDashboardHandler) GetMedia(c *gin.Context) {
media := ps.PostService.GetMedia()
media.PreviewUrl = services.GetPreviewUrl(media.RemoteUrl)
media.RemoteUrl = services.GetPreviewUrl(media.RemoteUrl)
c.JSON(http.StatusOK, media)
}

View File

@@ -16,9 +16,14 @@ func InitApiEndpointHandler() {
ApiEndpointHandlerInstance = &ApiEndpointHandler{
PostService: *services.PostServiceInstance,
}
}
func (ps *ApiEndpointHandler) GetRandomPost(c *gin.Context) {
c.JSON(200,ps.PostService.GetRandomPost())
}
post := ps.PostService.GetRandomPost()
for i := range post.Attachments {
post.Attachments[i].RemoteUrl = services.GetRemoteUrl(post.Attachments[i].RemoteUrl)
post.Attachments[i].PreviewUrl = services.GetPreviewUrl(post.Attachments[i].RemoteUrl)
}
c.JSON(200, post)
}

View File

@@ -22,6 +22,6 @@ func (ps *EmbedCardHandler) GetEmbedCard(c *gin.Context) {
post := ps.PostService.GetRandomPost()
c.HTML(200, "home/embed.html", gin.H{
"postUrl": post.Url,
"imageUrl": post.Attachments[0].RemoteUrl,
"imageUrl": services.GetRemoteUrl(post.Attachments[0].RemoteUrl),
})
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.5 KiB

View File

@@ -13,7 +13,7 @@
<meta property="og:type" content="website" />
<meta name="twitter:card" content="summary_large_image" />
<script type="module" crossorigin src="/admin/assets/index-Do3iuUd_.js"></script>
<script type="module" crossorigin src="/admin/assets/index-JxecVd-K.js"></script>
<link rel="stylesheet" crossorigin href="/admin/assets/index-DShmOgsI.css">
</head>

View File

@@ -1,2 +1,2 @@
User-agent: *
Allow: /
Disallow: /