Refactor app structure: move models to domain, centralize config and database init - TODO: add jwt
This commit is contained in:
@@ -1,18 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal/helpers"
|
||||
"CatsOfMastodonBotGo/internal/models"
|
||||
"CatsOfMastodonBotGo/internal/config"
|
||||
"CatsOfMastodonBotGo/internal/database"
|
||||
"CatsOfMastodonBotGo/internal/domain"
|
||||
"CatsOfMastodonBotGo/internal/server"
|
||||
"CatsOfMastodonBotGo/internal/services"
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Setup AppContext
|
||||
var appContext = helpers.SetupAppContext()
|
||||
// Setup config
|
||||
config.Init()
|
||||
var config = config.Load()
|
||||
|
||||
|
||||
// Initialize database
|
||||
database.Init()
|
||||
|
||||
services.InitPostService()
|
||||
|
||||
ticker := time.NewTicker(10 * time.Minute)
|
||||
|
||||
@@ -20,27 +28,27 @@ func main() {
|
||||
// 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)
|
||||
var posts []domain.Post = nil
|
||||
err, posts := services.PostServiceInstance.GetPostsFromApi(ctx, config.Tag, config.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)
|
||||
var existingPostIds = services.PostServiceInstance.GetExistingPostIds()
|
||||
var existingAccountIds = services.PostServiceInstance.GetExistingAccountIds()
|
||||
var newPosts = services.PostServiceInstance.GetNewPosts(existingPostIds, posts)
|
||||
var newAccounts = services.PostServiceInstance.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))
|
||||
log.Printf("Inserted %d accounts\n", services.PostServiceInstance.InsertNewAccounts(newAccounts))
|
||||
}
|
||||
if newPosts != nil {
|
||||
log.Printf("Inserted %d posts\n", appContext.PostService.InsertNewPosts(newPosts))
|
||||
log.Printf("Inserted %d posts\n", services.PostServiceInstance.InsertNewPosts(newPosts))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +65,7 @@ func main() {
|
||||
|
||||
// https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817
|
||||
|
||||
r := server.SetupRouter(appContext)
|
||||
r := server.SetupRouter()
|
||||
err := r.Run(":8080")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@@ -19,7 +19,9 @@ type config struct {
|
||||
Tag string
|
||||
}
|
||||
|
||||
func SetupAppContext() *config {
|
||||
var Config *config
|
||||
|
||||
func Load() *config {
|
||||
// Get mastodon instance
|
||||
instance := os.Getenv("CAOM_INSTANCE")
|
||||
if instance == "" {
|
||||
@@ -77,3 +79,7 @@ func SetupAppContext() *config {
|
||||
return appContext
|
||||
|
||||
}
|
||||
|
||||
func Init() {
|
||||
Config = Load()
|
||||
}
|
||||
|
@@ -7,6 +7,8 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var Gorm *gorm.DB
|
||||
|
||||
func Connect() (*gorm.DB, error) {
|
||||
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
@@ -17,4 +19,13 @@ func Connect() (*gorm.DB, error) {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// IDK if this is how it works or not, leave it as is for now
|
||||
func Init () {
|
||||
var err error
|
||||
Gorm, err = Connect()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
package domain
|
||||
|
||||
type Account struct {
|
||||
AccId string `json:"id" gorm:"primaryKey"`
|
||||
Username string `json:"username"`
|
||||
Acct string `json:"acct"`
|
||||
DisplayName string `json:"display_name"`
|
||||
IsBot bool `json:"bot"`
|
||||
Url string `json:"url"`
|
||||
AvatarStatic string `json:"avatar_static"`
|
||||
}
|
@@ -1,12 +0,0 @@
|
||||
package domain
|
||||
|
||||
type MediaAttachment struct {
|
||||
ID string `json:"id" gorm:"primaryKey"`
|
||||
Type string `json:"type"`
|
||||
Url string `json:"url"`
|
||||
PreviewUrl string `json:"preview_url"`
|
||||
RemoteUrl string `json:"remote_url"`
|
||||
PostID string // Foreign key to Post
|
||||
Approved bool `json:"approved"`
|
||||
Rejected bool `json:"rejected"`
|
||||
}
|
@@ -7,3 +7,24 @@ type Post struct {
|
||||
Account Account `json:"account" gorm:"foreignKey:AccountID;references:AccId"`
|
||||
Attachments []MediaAttachment `json:"media_attachments" gorm:"foreignKey:PostID;references:ID"`
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
AccId string `json:"id" gorm:"primaryKey"`
|
||||
Username string `json:"username"`
|
||||
Acct string `json:"acct"`
|
||||
DisplayName string `json:"display_name"`
|
||||
IsBot bool `json:"bot"`
|
||||
Url string `json:"url"`
|
||||
AvatarStatic string `json:"avatar_static"`
|
||||
}
|
||||
|
||||
type MediaAttachment struct {
|
||||
ID string `json:"id" gorm:"primaryKey"`
|
||||
Type string `json:"type"`
|
||||
Url string `json:"url"`
|
||||
PreviewUrl string `json:"preview_url"`
|
||||
RemoteUrl string `json:"remote_url"`
|
||||
PostID string // Foreign key to Post
|
||||
Approved bool `json:"approved"`
|
||||
Rejected bool `json:"rejected"`
|
||||
}
|
@@ -1,71 +0,0 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal"
|
||||
"CatsOfMastodonBotGo/internal/auth"
|
||||
"CatsOfMastodonBotGo/internal/services"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func SetupAppContext() *internal.AppContext {
|
||||
// Setup AppContext
|
||||
instance := os.Getenv("CAOM_INSTANCE")
|
||||
if instance == "" {
|
||||
instance = "https://mstdn.party"
|
||||
}
|
||||
tag := os.Getenv("CAOM_TAG")
|
||||
if tag == "" {
|
||||
tag = "catsofmastodon"
|
||||
}
|
||||
adminPassword := os.Getenv("CAOM_ADMIN_PASSWORD")
|
||||
if adminPassword == "" {
|
||||
log.Println("No admin password provided, using default password 'catsaregood'")
|
||||
adminPassword = "catsaregood"
|
||||
}
|
||||
|
||||
// Jwt params
|
||||
secret := os.Getenv("CAOM_JWT_SECRET")
|
||||
if secret == "" {
|
||||
log.Fatal("No jwt secret provided, using default secret 'secret'")
|
||||
}
|
||||
issuer := os.Getenv("CAOM_JWT_ISSUER")
|
||||
if issuer == "" {
|
||||
log.Println("No jwt issuer provided, using default issuer 'CatsOfMastodonBotGo'")
|
||||
issuer = "CatsOfMastodonBotGo"
|
||||
}
|
||||
audience := os.Getenv("CAOM_JWT_AUDIENCE")
|
||||
if audience == "" {
|
||||
log.Println("No jwt audience provided, using default audience 'CatsOfMastodonBotGo'")
|
||||
audience = "CatsOfMastodonBotGo"
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
// Setup Jwt
|
||||
var jwt = auth.NewJwtTokenGenerator(secret, issuer, audience)
|
||||
|
||||
// Inititlize AppContext
|
||||
var appContext = &internal.AppContext{
|
||||
Db: db,
|
||||
PostService: postService,
|
||||
Jwt: jwt,
|
||||
AdminPassword: adminPassword,
|
||||
Instance: instance,
|
||||
Tag: tag,
|
||||
}
|
||||
return appContext
|
||||
|
||||
}
|
@@ -2,14 +2,13 @@ package server
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal"
|
||||
handlers_admin "CatsOfMastodonBotGo/internal/web/handlers/admin"
|
||||
handlers_api "CatsOfMastodonBotGo/internal/web/handlers/api"
|
||||
"CatsOfMastodonBotGo/internal/web/handlers"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SetupRouter(appContext *internal.AppContext) *gin.Engine {
|
||||
func SetupRouter() *gin.Engine {
|
||||
r := gin.Default()
|
||||
|
||||
r.Use(cors.New(cors.Config{
|
||||
@@ -19,20 +18,20 @@ func SetupRouter(appContext *internal.AppContext) *gin.Engine {
|
||||
AllowCredentials: true,
|
||||
}))
|
||||
|
||||
adminDashboardHandler := handlers_admin.NewAdminDashboardHandler(appContext)
|
||||
apiHandler := handlers_api.NewApiEndpointHandler(appContext)
|
||||
handlers.InitAdminDashboardHandler()
|
||||
handlers.InitApiEndpointHandler()
|
||||
|
||||
admin := r.Group("/admin")
|
||||
|
||||
// My man, this is done way more efficient and fast in .NET, specially the authentication part
|
||||
admin.POST("/login", adminDashboardHandler.Login)
|
||||
admin.GET("/getmedia", appContext.Jwt.GinMiddleware(), adminDashboardHandler.GetMedia)
|
||||
admin.POST("/approve", appContext.Jwt.GinMiddleware(), adminDashboardHandler.ApproveMedia)
|
||||
admin.POST("/reject", appContext.Jwt.GinMiddleware(), adminDashboardHandler.RejectMedia)
|
||||
admin.POST("/login", handlers.AdminDashboardHandlerInstance.Login)
|
||||
admin.GET("/getmedia", appContext.Jwt.GinMiddleware(), handlers.AdminDashboardHandlerInstance.GetMedia)
|
||||
admin.POST("/approve", appContext.Jwt.GinMiddleware(), handlers.AdminDashboardHandlerInstance.ApproveMedia)
|
||||
admin.POST("/reject", appContext.Jwt.GinMiddleware(), handlers.AdminDashboardHandlerInstance.RejectMedia)
|
||||
|
||||
api := r.Group("/api")
|
||||
|
||||
api.GET("/post/random", apiHandler.GetRandomPost)
|
||||
api.GET("/post/random", handlers.ApiEndpointHandlerInstance.GetRandomPost)
|
||||
|
||||
return r
|
||||
}
|
||||
|
@@ -1,7 +1,8 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal/models"
|
||||
"CatsOfMastodonBotGo/internal/database"
|
||||
"CatsOfMastodonBotGo/internal/domain"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -16,12 +17,14 @@ type PostService struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
var PostServiceInstance *PostService
|
||||
|
||||
// Constructor
|
||||
func NewPostService(db *gorm.DB) *PostService {
|
||||
return &PostService{db: db}
|
||||
func InitPostService() {
|
||||
PostServiceInstance = &PostService{db: database.Gorm}
|
||||
}
|
||||
|
||||
func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance string) (error, []models.Post) {
|
||||
func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance string) (error, []domain.Post) {
|
||||
var requestUrl = instance + "/api/v1/timelines/tag/" + tag + "?limit=40"
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil)
|
||||
if err != nil {
|
||||
@@ -36,7 +39,7 @@ func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance st
|
||||
return fmt.Errorf("Status code:", resp.StatusCode, " Content-Type:", resp.Header.Get("Content-Type")), nil
|
||||
}
|
||||
|
||||
var posts []models.Post = nil
|
||||
var posts []domain.Post = nil
|
||||
err = json.NewDecoder(resp.Body).Decode(&posts)
|
||||
if err != nil {
|
||||
return err, nil
|
||||
@@ -51,19 +54,19 @@ func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance st
|
||||
|
||||
func (ps *PostService) GetExistingPostIds() []string {
|
||||
var existingPostIds []string
|
||||
ps.db.Model(&models.Post{}).Pluck("id", &existingPostIds)
|
||||
ps.db.Model(&domain.Post{}).Pluck("id", &existingPostIds)
|
||||
return existingPostIds
|
||||
}
|
||||
|
||||
func (ps *PostService) GetExistingAccountIds() []string {
|
||||
var existingAccountIds []string
|
||||
ps.db.Model(&models.Account{}).Pluck("acc_id", &existingAccountIds)
|
||||
ps.db.Model(&domain.Account{}).Pluck("acc_id", &existingAccountIds)
|
||||
return existingAccountIds
|
||||
}
|
||||
|
||||
|
||||
func (*PostService) GetNewPosts(existingPostIds []string, posts []models.Post) []models.Post {
|
||||
var newPosts []models.Post = nil
|
||||
func (*PostService) GetNewPosts(existingPostIds []string, posts []domain.Post) []domain.Post {
|
||||
var newPosts []domain.Post = nil
|
||||
for _, post := range posts {
|
||||
if !arrayContains(existingPostIds, post.ID) && len(post.Attachments) > 0 && !post.Account.IsBot {
|
||||
var allImageMedia = true
|
||||
@@ -81,8 +84,8 @@ func (*PostService) GetNewPosts(existingPostIds []string, posts []models.Post) [
|
||||
return newPosts
|
||||
}
|
||||
|
||||
func (*PostService) GetNewAccounts(existingAccountIds []string, posts []models.Post) []models.Account {
|
||||
var newAccounts []models.Account = nil
|
||||
func (*PostService) GetNewAccounts(existingAccountIds []string, posts []domain.Post) []domain.Account {
|
||||
var newAccounts []domain.Account = nil
|
||||
for _, post := range posts {
|
||||
if !arrayContains(existingAccountIds, post.Account.AccId) {
|
||||
newAccounts = append(newAccounts, post.Account)
|
||||
@@ -91,45 +94,45 @@ func (*PostService) GetNewAccounts(existingAccountIds []string, posts []models.P
|
||||
return newAccounts
|
||||
}
|
||||
|
||||
func (ps *PostService) InsertNewPosts(newPosts []models.Post) int {
|
||||
func (ps *PostService) InsertNewPosts(newPosts []domain.Post) int {
|
||||
return int(ps.db.Create(&newPosts).RowsAffected)
|
||||
}
|
||||
|
||||
func (ps *PostService) InsertNewAccounts(newAccounts []models.Account) int {
|
||||
func (ps *PostService) InsertNewAccounts(newAccounts []domain.Account) int {
|
||||
return int(ps.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&newAccounts).RowsAffected)
|
||||
}
|
||||
|
||||
// From this point on, its for the api endpoints
|
||||
|
||||
func (ps *PostService) GetRandomPost() models.Post {
|
||||
var post models.Post
|
||||
func (ps *PostService) GetRandomPost() domain.Post {
|
||||
var post domain.Post
|
||||
ps.db.
|
||||
Preload("Account").
|
||||
Preload("Attachments", "approved = ?", true).
|
||||
Order("RANDOM()").
|
||||
First(&post)
|
||||
if len(post.Attachments) > 0 {
|
||||
post.Attachments = []models.MediaAttachment{post.Attachments[0]}
|
||||
post.Attachments = []domain.MediaAttachment{post.Attachments[0]}
|
||||
}
|
||||
return post
|
||||
}
|
||||
|
||||
func (ps *PostService) ApproveMedia(mediaId string) bool {
|
||||
return ps.db.Model(&models.MediaAttachment{}).
|
||||
return ps.db.Model(&domain.MediaAttachment{}).
|
||||
Where("id = ?", mediaId).
|
||||
Update("approved", true).RowsAffected > 0
|
||||
}
|
||||
|
||||
func (ps *PostService) RejectMedia(mediaId string) bool {
|
||||
return ps.db.Model(&models.MediaAttachment{}).
|
||||
return ps.db.Model(&domain.MediaAttachment{}).
|
||||
Where("id = ?", mediaId).
|
||||
Update("rejected", true).RowsAffected > 0
|
||||
}
|
||||
|
||||
// Get a post which approve and rejet are false (For admin panel)
|
||||
func (ps *PostService) GetMedia() models.MediaAttachment {
|
||||
var media models.MediaAttachment
|
||||
ps.db.Model(&models.MediaAttachment{}).
|
||||
func (ps *PostService) GetMedia() domain.MediaAttachment {
|
||||
var media domain.MediaAttachment
|
||||
ps.db.Model(&domain.MediaAttachment{}).
|
||||
Where("approved = ?", false).
|
||||
Where("rejected = ?", false).
|
||||
Order("RANDOM()").
|
||||
|
@@ -1,56 +1,59 @@
|
||||
package handlers_admin
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal"
|
||||
"net/http"
|
||||
|
||||
requestmodels "CatsOfMastodonBotGo/internal/models/requestModels"
|
||||
"CatsOfMastodonBotGo/internal/config"
|
||||
requestmodels "CatsOfMastodonBotGo/internal/domain/requestModels"
|
||||
"CatsOfMastodonBotGo/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AdminDashboardHandler struct {
|
||||
AppContext *internal.AppContext
|
||||
PostService services.PostService
|
||||
}
|
||||
|
||||
func NewAdminDashboardHandler(appContext *internal.AppContext) *AdminDashboardHandler {
|
||||
return &AdminDashboardHandler{
|
||||
AppContext: appContext,
|
||||
var AdminDashboardHandlerInstance *AdminDashboardHandler
|
||||
|
||||
func InitAdminDashboardHandler() {
|
||||
AdminDashboardHandlerInstance = &AdminDashboardHandler{
|
||||
PostService: *services.PostServiceInstance,
|
||||
}
|
||||
}
|
||||
|
||||
func (appContext *AdminDashboardHandler) ApproveMedia(c *gin.Context) {
|
||||
func (ps *AdminDashboardHandler) ApproveMedia(c *gin.Context) {
|
||||
var input requestmodels.ApproveMediaInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if appContext.AppContext.PostService.ApproveMedia(input.MediaId) {
|
||||
if ps.PostService.ApproveMedia(input.MediaId) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Media approved successfully"})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to approve media"})
|
||||
}
|
||||
}
|
||||
|
||||
func (appContext *AdminDashboardHandler) RejectMedia(c *gin.Context) {
|
||||
func (ps *AdminDashboardHandler) RejectMedia(c *gin.Context) {
|
||||
var input requestmodels.RejectMediaInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if appContext.AppContext.PostService.RejectMedia(input.MediaId) {
|
||||
if ps.PostService.RejectMedia(input.MediaId) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Media rejected successfully"})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to reject media"})
|
||||
}
|
||||
}
|
||||
|
||||
func (appContext *AdminDashboardHandler) GetMedia(c *gin.Context) {
|
||||
media := appContext.AppContext.PostService.GetMedia()
|
||||
func (ps *AdminDashboardHandler) GetMedia(c *gin.Context) {
|
||||
media := ps.PostService.GetMedia()
|
||||
c.JSON(http.StatusOK, media)
|
||||
}
|
||||
|
||||
func (appContext *AdminDashboardHandler) Login(c *gin.Context) {
|
||||
func (ps *AdminDashboardHandler) Login(c *gin.Context) {
|
||||
|
||||
var input requestmodels.LoginInput
|
||||
|
||||
@@ -60,14 +63,14 @@ func (appContext *AdminDashboardHandler) Login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if input.Password == appContext.AppContext.AdminPassword { // Its more than enough for this project
|
||||
token, err := appContext.AppContext.Jwt.GenerateToken(map[string]interface{}{"role": "admin"})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"})
|
||||
return
|
||||
}
|
||||
if input.Password == config.Config.AdminPassword { // Its more than enough for this project
|
||||
// token, err := ps.ps.Jwt.GenerateToken(map[string]interface{}{"role": "admin"})
|
||||
// if err != nil {
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": "Token generation failed"})
|
||||
// return
|
||||
// }
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Login successful", "token": token})
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Login successful", "token": "yoy"}) // TODO: Add token
|
||||
|
||||
} else {
|
||||
c.JSON(401, gin.H{
|
@@ -1,20 +0,0 @@
|
||||
package handlers_api
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ApiEndpointHandler struct {
|
||||
AppContext *internal.AppContext
|
||||
}
|
||||
|
||||
func NewApiEndpointHandler(appContext *internal.AppContext) *ApiEndpointHandler {
|
||||
return &ApiEndpointHandler{
|
||||
AppContext: appContext,
|
||||
}
|
||||
}
|
||||
|
||||
func (appContext *ApiEndpointHandler) GetRandomPost(c *gin.Context) {
|
||||
c.JSON(200,appContext.AppContext.PostService.GetRandomPost())
|
||||
}
|
24
internal/web/handlers/apiEndpoint.go
Normal file
24
internal/web/handlers/apiEndpoint.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ApiEndpointHandler struct {
|
||||
PostService services.PostService
|
||||
}
|
||||
|
||||
var ApiEndpointHandlerInstance *ApiEndpointHandler
|
||||
|
||||
func InitApiEndpointHandler() {
|
||||
ApiEndpointHandlerInstance = &ApiEndpointHandler{
|
||||
PostService: *services.PostServiceInstance,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (ps *ApiEndpointHandler) GetRandomPost(c *gin.Context) {
|
||||
c.JSON(200,ps.PostService.GetRandomPost())
|
||||
}
|
Reference in New Issue
Block a user