Add CORS_ALLOW env var AND facilitate igapi

This commit is contained in:
jasinco 2025-06-08 22:52:37 +08:00
parent 74f43731b5
commit 0bfc8779de
5 changed files with 71 additions and 15 deletions

View file

@ -17,7 +17,7 @@ import { rootstr } from "./rooturl";
const POST = z.object({
id: z.number(),
content: z.string(),
signing: z.string(),
signing: z.nullable(z.string()),
post_at: z.coerce.date(),
phase: z.optional(z.string()),
enclosure: z.nullable(z.array(z.string()))

View file

@ -208,6 +208,11 @@ func AdminVerify(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusInternalServerError)
}
// Verified and send to IGAPI to handle synchronization of IG posts
if internal.IGAPI_ACTIVATE {
internal.IGAPI_CHAN <- internal.PR{ACT_TYPE: 0, ID: post_verify.Id}
}
return tx.Commit(ctx)
}

View file

@ -66,6 +66,7 @@ func Delete_post(c *fiber.Ctx) error {
hash := c.Query("hash")
ctx := c.Context()
post_id, err := strconv.Atoi(post)
post_id_i32 := int32(post_id)
if err != nil {
return c.Status(500).SendString("Failed to Parse Int")
}
@ -76,10 +77,10 @@ func Delete_post(c *fiber.Ctx) error {
}
defer tx.Rollback(ctx)
qtx := internal.NIMDB.WithTx(tx)
if err = qtx.DeletePost(ctx, nimdb.DeletePostParams{ID: int32(post_id), Hash: hash}); err != nil {
if err = qtx.DeletePost(ctx, nimdb.DeletePostParams{ID: post_id_i32, Hash: hash}); err != nil {
return err
}
if err = qtx.InvalidateMedia(ctx, pgtype.Int4{Int32: int32(post_id)}); err != nil {
if err = qtx.InvalidateMedia(ctx, pgtype.Int4{Int32: post_id_i32}); err != nil {
return err
}
@ -87,6 +88,10 @@ func Delete_post(c *fiber.Ctx) error {
return c.Status(500).SendString("Failed to Commit")
}
if internal.IGAPI_ACTIVATE {
internal.IGAPI_CHAN <- internal.PR{ACT_TYPE: 1, ID: post_id_i32}
}
return c.SendStatus(200)
}

View file

@ -1,19 +1,33 @@
package internal
import (
"context"
"errors"
"log"
"os"
"strconv"
"google.golang.org/grpc"
"nim.jasinco.work/app/igapi"
)
type PR struct {
ACT_TYPE int //0 for post, 1 for revoke
ID int32
}
var (
SALT string
POSTGRES_URL string
JWT_SECRET string
FETCH_LENGTH int32
PREFORK bool
err error
conv int64
SALT string
POSTGRES_URL string
JWT_SECRET string
FETCH_LENGTH int32
PREFORK bool
IGAPI_HOST string
CORS_ALLOW string
IGAPI_ACTIVATE bool
IGAPI_CHAN = make(chan PR, 20)
err error
conv int64
)
func ReadFromENV() error {
@ -52,5 +66,31 @@ func ReadFromENV() error {
return err
}
}
IGAPI_HOST = os.Getenv("IGAPI_HOST")
IGAPI_ACTIVATE = true
if len(IGAPI_HOST) == 0 {
log.Println("Didn't get IGAPI_HOST, it will work without it")
IGAPI_ACTIVATE = false
}
CORS_ALLOW = os.Getenv("CORS_ALLOW")
return nil
}
func IGAPI_establish() (*grpc.ClientConn, igapi.IGAPIClient) {
conn, err := grpc.NewClient(IGAPI_HOST)
if err != nil {
log.Panicf("Can't connect to igapi: %s", err.Error())
}
c := igapi.NewIGAPIClient(conn)
return conn, c
}
func IGAPI_chan_exec(c igapi.IGAPIClient) {
for id := range IGAPI_CHAN {
if id.ACT_TYPE == 0 {
c.Upload(context.Background(), &igapi.Request{Id: int64(id.ID)})
}
if id.ACT_TYPE == 1 {
c.Delete(context.Background(), &igapi.Request{Id: int64(id.ID)})
}
}
}

View file

@ -7,7 +7,7 @@ import (
jwtware "github.com/gofiber/contrib/jwt"
"github.com/gofiber/fiber/v2"
// "github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/jackc/pgx/v5/pgxpool"
"nim.jasinco.work/app/internal"
@ -30,6 +30,12 @@ func main() {
internal.POOL = dbpool
internal.NIMDB = nimdb.New(dbpool)
if internal.IGAPI_ACTIVATE {
conn, client := internal.IGAPI_establish()
defer conn.Close()
go internal.IGAPI_chan_exec(client)
}
app := fiber.New(fiber.Config{Prefork: internal.PREFORK})
app.Get("/", func(c *fiber.Ctx) error {
@ -49,10 +55,10 @@ func main() {
return c.SendFile("./toofast.html")
},
}))
// app.Use(cors.New(cors.Config{
// AllowOrigins: "http://localhost:5173, http://localhost:4173",
// AllowCredentials: true,
// }))
app.Use(cors.New(cors.Config{
AllowOrigins: internal.CORS_ALLOW,
AllowCredentials: true,
}))
app.Get("/api/post", handlers.Fetch_post)
app.Post("/api/post", handlers.Insert_Post)
app.Delete("/api/post", handlers.Delete_post)