diff --git a/main.go b/main.go index 4957994..f393247 100644 --- a/main.go +++ b/main.go @@ -16,85 +16,6 @@ import ( var dbpool *pgxpool.Pool var uploadFolder string -func main() { - var err error - dbpool, err = pgxpool.New(context.Background(), "postgresql://postgres:postgres@172.18.0.3:5432/postgres") - if err != nil { - log.Fatal(err) - } - defer dbpool.Close() - - sqlStmt := ` - DROP TABLE IF EXISTS videos; - - CREATE TABLE IF NOT EXISTS videos - ( - id serial NOT NULL, - filepath text, - CONSTRAINT videos_pkey PRIMARY KEY (id) - ) - ` - _, err = dbpool.Exec(context.Background(), sqlStmt) - if err != nil { - log.Panicf("main: %v\n", err) - } - - faker := faker.New() - - for i := 0; i < 10; i++ { - _, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", faker.File().AbsoluteFilePathForUnix(2)) - if err != nil { - log.Panicf("main: %v\n", err) - } - } - - currentDir, err := os.Getwd() - if err != nil { - log.Panicf("main: %v\n", err) - } - uploadFolder = fmt.Sprintf("%s/upload", currentDir) - - if _, err := os.Stat(uploadFolder); err != nil { - if !os.IsNotExist(err) { - log.Panicf("main: %v\n", err) - } - - if err := os.Mkdir(uploadFolder, 0750); err != nil { - log.Panicf("main: %v\n", err) - } - } - - files, err := os.ReadDir(uploadFolder) - if err != nil { - panic(err) - } - - if len(files) != 0 { - for _, file := range files { - if file.IsDir() { - continue - } - - _, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", fmt.Sprintf("%s/%s", uploadFolder, file.Name())) - if err != nil { - log.Panicf("main: %v\n", err) - } - } - } - - router := gin.Default() - router.SetTrustedProxies(nil) - - router.POST("/video/init", initUpload) - router.POST("/video/chunk", receiveChunk) - router.POST("/video/completed", finishUpload) - router.GET("/videos", listVideos) - router.GET("/videos/:id", getVideo) - router.DELETE("/videos/:id", deleteVideo) - - router.Run("localhost:8080") -} - func initUpload(c *gin.Context) { fileName, err := io.ReadAll(c.Request.Body) if err != nil { @@ -235,3 +156,82 @@ func deleteVideo(c *gin.Context) { c.JSON(http.StatusOK, "File deleted successfully") } + +func main() { + var err error + dbpool, err = pgxpool.New(context.Background(), "postgresql://postgres:postgres@172.18.0.3:5432/postgres") + if err != nil { + log.Fatal(err) + } + defer dbpool.Close() + + sqlStmt := ` + DROP TABLE IF EXISTS videos; + + CREATE TABLE IF NOT EXISTS videos + ( + id serial NOT NULL, + filepath text, + CONSTRAINT videos_pkey PRIMARY KEY (id) + ) + ` + _, err = dbpool.Exec(context.Background(), sqlStmt) + if err != nil { + log.Panicf("main: %v\n", err) + } + + faker := faker.New() + + for i := 0; i < 10; i++ { + _, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", faker.File().AbsoluteFilePathForUnix(2)) + if err != nil { + log.Panicf("main: %v\n", err) + } + } + + currentDir, err := os.Getwd() + if err != nil { + log.Panicf("main: %v\n", err) + } + uploadFolder = fmt.Sprintf("%s/upload", currentDir) + + if _, err := os.Stat(uploadFolder); err != nil { + if !os.IsNotExist(err) { + log.Panicf("main: %v\n", err) + } + + if err := os.Mkdir(uploadFolder, 0750); err != nil { + log.Panicf("main: %v\n", err) + } + } + + files, err := os.ReadDir(uploadFolder) + if err != nil { + panic(err) + } + + if len(files) != 0 { + for _, file := range files { + if file.IsDir() { + continue + } + + _, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", fmt.Sprintf("%s/%s", uploadFolder, file.Name())) + if err != nil { + log.Panicf("main: %v\n", err) + } + } + } + + router := gin.Default() + router.SetTrustedProxies(nil) + + router.POST("/video/init", initUpload) + router.POST("/video/chunk", receiveChunk) + router.POST("/video/completed", finishUpload) + router.GET("/videos", listVideos) + router.GET("/videos/:id", getVideo) + router.DELETE("/videos/:id", deleteVideo) + + router.Run("localhost:8080") +}