From c5aa21b75e52709a8a4d60384fbfe88baf1f2131 Mon Sep 17 00:00:00 2001 From: hex Date: Sun, 8 Jun 2025 21:13:32 +0200 Subject: [PATCH] add shit --- .env.example | 2 ++ internal/api/routes/auth.go | 5 ++++- internal/api/routes/files.go | 1 - internal/types/types.go | 13 +++++++------ main.go | 7 ++++++- testing.md | 4 ++++ 6 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 testing.md diff --git a/.env.example b/.env.example index 19c21ad..4d9babe 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,8 @@ IMAGE_PATH=/tmp REDIRECT_URI=http://localhost:8081/api/auth/callback CLIENT_ID= CLIENT_SECRET= +FRONTEND_URI= +DOMAIN=localhost # can be either postgres or sqlite DATABASE_TYPE= diff --git a/internal/api/routes/auth.go b/internal/api/routes/auth.go index b4a2f38..90ef30f 100644 --- a/internal/api/routes/auth.go +++ b/internal/api/routes/auth.go @@ -38,10 +38,13 @@ func RegisterAuthRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) { } // TODO: redirect to dashboard - c.JSON(http.StatusOK, gin.H{ + /*c.JSON(http.StatusOK, gin.H{ "jwt": jwt, "known": res.RowsAffected == 0, }) + */ + c.SetCookie("jwt", jwt, int(t.ExpiresIn), "", cfg.Domain, true, true) + c.Redirect(http.StatusTemporaryRedirect, cfg.FrontendUri) }) api.GET("/auth/me", auth.JwtMiddleware(cfg.JWTSecret), func(c *gin.Context) { diff --git a/internal/api/routes/files.go b/internal/api/routes/files.go index c54489d..0d987f4 100644 --- a/internal/api/routes/files.go +++ b/internal/api/routes/files.go @@ -48,7 +48,6 @@ func RegisterFileRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) { fileMeta := types.File{ ID: uid + "_" + file.Filename, - Path: filePath, Owner: uid, CreatedAt: time.Now(), Size: file.Size, diff --git a/internal/types/types.go b/internal/types/types.go index 505c1fe..abae4ee 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -15,16 +15,17 @@ type Route struct { } type StereoConfig struct { - ImagePath string - Router *gin.Engine - Client client.Client - Database *gorm.DB - JWTSecret string + ImagePath string + Router *gin.Engine + Client client.Client + Database *gorm.DB + JWTSecret string + FrontendUri string + Domain string } type File struct { ID string `gorm:"primaryKey"` - Path string `gorm:"not null;index"` Owner string `gorm:"not null;index"` Size int64 `gorm:"not null;type:bigint"` CreatedAt time.Time `gorm:"autoCreateTime"` diff --git a/main.go b/main.go index 92466c0..5b58333 100644 --- a/main.go +++ b/main.go @@ -54,9 +54,14 @@ func main() { requireEnv("CLIENT_ID"), requireEnv("CLIENT_SECRET"), ), + FrontendUri: requireEnv("FRONTEND_URI"), + Domain: requireEnv("DOMAIN"), JWTSecret: requireEnv("JWT_SECRET"), } + log.Println(c.Domain) + log.Println(c.FrontendUri) + switch databaseType { case "sqlite": db, err := gorm.Open(sqlite.Open(sqliteFile), &gorm.Config{}) @@ -86,6 +91,6 @@ func main() { c.Database.AutoMigrate(&auth.User{}, &types.File{}) api.Register(&c) - fmt.Printf("Running on port %s\n", getEnv("PORT", "8080")) + fmt.Printf("Running on port %s\n", getEnv("PORT", "8081")) c.Router.Run() } diff --git a/testing.md b/testing.md new file mode 100644 index 0000000..eb1e991 --- /dev/null +++ b/testing.md @@ -0,0 +1,4 @@ +## curl +### uploading + +curl -F "file=@(path_to_test_img)" "http://localhost:8081/api/upload" -H "Authorization: Bearer (jwt here)"