switch from Fatalf to Panicf

This commit is contained in:
AustrianToast 2024-06-03 21:19:36 +02:00
parent a36fccfa1f
commit 9e605e34b1
No known key found for this signature in database
GPG Key ID: 5CD422268E489EB4

47
main.go
View File

@ -15,7 +15,6 @@ import (
)
var dbpool *pgxpool.Pool
var uploadFolder string
func main() {
@ -38,7 +37,7 @@ func main() {
`
_, err = dbpool.Exec(context.Background(), sqlStmt)
if err != nil {
log.Fatalf("main: create table: %v\n", err)
log.Panicf("main: %v\n", err)
}
faker := faker.New()
@ -46,13 +45,13 @@ func main() {
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.Fatalf("main: fill with fake data: %v\n", err)
log.Panicf("main: %v\n", err)
}
}
currentDir, err := os.Getwd()
if err != nil {
log.Fatalf("main: %v\n", err)
log.Panicf("main: %v\n", err)
}
uploadFolder = fmt.Sprintf("%s/videos", currentDir)
@ -84,7 +83,7 @@ func initUpload(c *gin.Context) {
err = rows.Scan(&filepath)
if err != nil {
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("initUpload: %v\n", err)
log.Panicf("initUpload: %v\n", err)
}
if filepath == fmt.Sprintf("%s/%s", uploadFolder, fileName) {
c.JSON(http.StatusForbidden, "File already exists")
@ -94,7 +93,7 @@ func initUpload(c *gin.Context) {
err = rows.Err()
if err != nil {
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("initUpload: %v\n", err)
log.Panicf("initUpload: %v\n", err)
}
c.JSON(http.StatusOK, "Upload can proceed")
@ -134,10 +133,12 @@ func finishUpload(c *gin.Context) {
_, err = dbpool.Exec(context.Background(), "insert into videos(filepath) values($1)", fmt.Sprintf("%s/%s", uploadFolder, fileName))
if err != nil {
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("finishUpload: %v\n", err)
log.Panicf("finishUpload: %v\n", err)
}
c.JSON(http.StatusOK, gin.H{}) // return list of videos
allVideos := gin.H{}
c.JSON(http.StatusOK, allVideos)
}
func listVideos(c *gin.Context) {
@ -149,40 +150,40 @@ func listVideos(c *gin.Context) {
err = rows.Scan(&id, &filepath)
if err != nil {
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("listVideos: %v\n", err)
log.Panicf("listVideos: %v\n", err)
}
fmt.Println(id, filepath)
}
err = rows.Err()
if err != nil {
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("listVideos: %v\n", err)
log.Panicf("listVideos: %v\n", err)
}
c.JSON(http.StatusOK, gin.H{})
allVideos := gin.H{}
c.JSON(http.StatusOK, allVideos)
}
func getVideo(c *gin.Context) {
var err error
inputId, err := strconv.Atoi(c.Param("id"))
if err != nil {
log.Fatal(err)
log.Panicf("getVideo: %v\n", err)
}
rows, _ := dbpool.Query(context.Background(), "select * from videos where id = $1", inputId)
rows, _ := dbpool.Query(context.Background(), "select filepath from videos where id = $1", inputId)
rows.Next()
var id int
var filepath string
err = rows.Scan(&id, &filepath)
err = rows.Scan(&filepath)
if err != nil {
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("getVideo: %v\n", err)
log.Panicf("getVideo: %v\n", err)
}
fmt.Println(id, filepath)
err = rows.Err()
if err != nil {
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("getVideo: %v\n", err)
log.Panicf("getVideo: %v\n", err)
}
c.JSON(http.StatusOK, gin.H{})
@ -191,7 +192,7 @@ func getVideo(c *gin.Context) {
func deleteVideo(c *gin.Context) {
inputId, err := strconv.Atoi(c.Param("id"))
if err != nil {
log.Fatal(err)
log.Panicf("deleteVideo: %v\n", err)
}
rows, _ := dbpool.Query(context.Background(), "select filepath from videos where id = $1", inputId)
@ -199,14 +200,14 @@ func deleteVideo(c *gin.Context) {
var filepath string
err = rows.Scan(&filepath)
if err != nil {
log.Printf("deleteVideo: %v\n", err)
log.Panicf("deleteVideo: %v\n", err)
c.JSON(http.StatusBadRequest, "Video does not exist")
return
}
fmt.Println(filepath)
err = rows.Err()
if err != nil {
log.Printf("deleteVideo: %v\n", err)
log.Panicf("deleteVideo: %v\n", err)
c.JSON(http.StatusInternalServerError, "Scanning the row didn't work")
return
}
@ -214,12 +215,12 @@ func deleteVideo(c *gin.Context) {
_, err = dbpool.Exec(context.Background(), "delete from videos where id = $1", inputId)
if err != nil {
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("deleteVideo: %v\n", err)
log.Panicf("deleteVideo: %v\n", err)
}
if err = os.Remove(filepath); err != nil {
c.JSON(http.StatusInternalServerError, "")
log.Fatalf("deleteVideo: %v\n", err)
log.Panicf("deleteVideo: %v\n", err)
}
c.JSON(http.StatusOK, gin.H{}) // return list of videos