Add CORS_ALLOW env var AND facilitate igapi
This commit is contained in:
parent
74f43731b5
commit
0bfc8779de
5 changed files with 71 additions and 15 deletions
|
@ -17,7 +17,7 @@ import { rootstr } from "./rooturl";
|
||||||
const POST = z.object({
|
const POST = z.object({
|
||||||
id: z.number(),
|
id: z.number(),
|
||||||
content: z.string(),
|
content: z.string(),
|
||||||
signing: z.string(),
|
signing: z.nullable(z.string()),
|
||||||
post_at: z.coerce.date(),
|
post_at: z.coerce.date(),
|
||||||
phase: z.optional(z.string()),
|
phase: z.optional(z.string()),
|
||||||
enclosure: z.nullable(z.array(z.string()))
|
enclosure: z.nullable(z.array(z.string()))
|
||||||
|
|
|
@ -208,6 +208,11 @@ func AdminVerify(c *fiber.Ctx) error {
|
||||||
return c.SendStatus(fiber.StatusInternalServerError)
|
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)
|
return tx.Commit(ctx)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,6 +66,7 @@ func Delete_post(c *fiber.Ctx) error {
|
||||||
hash := c.Query("hash")
|
hash := c.Query("hash")
|
||||||
ctx := c.Context()
|
ctx := c.Context()
|
||||||
post_id, err := strconv.Atoi(post)
|
post_id, err := strconv.Atoi(post)
|
||||||
|
post_id_i32 := int32(post_id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.Status(500).SendString("Failed to Parse Int")
|
return c.Status(500).SendString("Failed to Parse Int")
|
||||||
}
|
}
|
||||||
|
@ -76,10 +77,10 @@ func Delete_post(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
defer tx.Rollback(ctx)
|
defer tx.Rollback(ctx)
|
||||||
qtx := internal.NIMDB.WithTx(tx)
|
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
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,6 +88,10 @@ func Delete_post(c *fiber.Ctx) error {
|
||||||
return c.Status(500).SendString("Failed to Commit")
|
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)
|
return c.SendStatus(200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,31 @@
|
||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"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 (
|
var (
|
||||||
SALT string
|
SALT string
|
||||||
POSTGRES_URL string
|
POSTGRES_URL string
|
||||||
JWT_SECRET string
|
JWT_SECRET string
|
||||||
FETCH_LENGTH int32
|
FETCH_LENGTH int32
|
||||||
PREFORK bool
|
PREFORK bool
|
||||||
|
IGAPI_HOST string
|
||||||
|
CORS_ALLOW string
|
||||||
|
IGAPI_ACTIVATE bool
|
||||||
|
IGAPI_CHAN = make(chan PR, 20)
|
||||||
err error
|
err error
|
||||||
conv int64
|
conv int64
|
||||||
)
|
)
|
||||||
|
@ -52,5 +66,31 @@ func ReadFromENV() error {
|
||||||
return err
|
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
|
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)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
16
server.go
16
server.go
|
@ -7,7 +7,7 @@ import (
|
||||||
|
|
||||||
jwtware "github.com/gofiber/contrib/jwt"
|
jwtware "github.com/gofiber/contrib/jwt"
|
||||||
"github.com/gofiber/fiber/v2"
|
"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/gofiber/fiber/v2/middleware/limiter"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
"nim.jasinco.work/app/internal"
|
"nim.jasinco.work/app/internal"
|
||||||
|
@ -30,6 +30,12 @@ func main() {
|
||||||
internal.POOL = dbpool
|
internal.POOL = dbpool
|
||||||
internal.NIMDB = nimdb.New(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 := fiber.New(fiber.Config{Prefork: internal.PREFORK})
|
||||||
|
|
||||||
app.Get("/", func(c *fiber.Ctx) error {
|
app.Get("/", func(c *fiber.Ctx) error {
|
||||||
|
@ -49,10 +55,10 @@ func main() {
|
||||||
return c.SendFile("./toofast.html")
|
return c.SendFile("./toofast.html")
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
// app.Use(cors.New(cors.Config{
|
app.Use(cors.New(cors.Config{
|
||||||
// AllowOrigins: "http://localhost:5173, http://localhost:4173",
|
AllowOrigins: internal.CORS_ALLOW,
|
||||||
// AllowCredentials: true,
|
AllowCredentials: true,
|
||||||
// }))
|
}))
|
||||||
app.Get("/api/post", handlers.Fetch_post)
|
app.Get("/api/post", handlers.Fetch_post)
|
||||||
app.Post("/api/post", handlers.Insert_Post)
|
app.Post("/api/post", handlers.Insert_Post)
|
||||||
app.Delete("/api/post", handlers.Delete_post)
|
app.Delete("/api/post", handlers.Delete_post)
|
||||||
|
|
Loading…
Add table
Reference in a new issue