forked from handuy/gin-gonic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
DuyTechmaster
committed
Oct 12, 2019
1 parent
7106a37
commit 4e1d627
Showing
6 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
## Ghi lỗi ra file log | ||
|
||
1. Hàm bắt lỗi và ghi ra file log | ||
```go | ||
func LogError(c *gin.Context, message string) { | ||
var errorInfo ErrorInfo | ||
errorInfo.Time = time.Now() | ||
errorInfo.Path = c.FullPath() | ||
errorInfo.Method = c.Request.Method | ||
|
||
var log = logrus.New() | ||
log.Out = os.Stdout | ||
file, err := os.OpenFile("log/logging.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) | ||
if err == nil { | ||
log.Out = file | ||
} else { | ||
log.Println(err) | ||
} | ||
|
||
log.WithFields(logrus.Fields{ | ||
"Path": errorInfo.Path, | ||
"Method": errorInfo.Method, | ||
}).Error(message) | ||
} | ||
``` | ||
|
||
2. Trong mỗi route, gọi hàm **LogError** khi kiểm tra lỗi, ví dụ như trong **POST /upload-video** | ||
```go | ||
file, err := c.FormFile("video-file") | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, err.Error()) | ||
LogError(c, err.Error()) | ||
return | ||
} | ||
|
||
fileType := file.Header["Content-Type"][0] | ||
|
||
if GetAllowFormat(fileType, allowedMediaType) == "" { | ||
c.JSON(http.StatusBadRequest, "Định dạng không hợp lệ. Yêu cầu: mp4") | ||
LogError(c, "Định dạng không hợp lệ. Yêu cầu: mp4") | ||
return | ||
} | ||
|
||
err = c.SaveUploadedFile(file, path.Join("./upload", file.Filename)) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, err.Error()) | ||
LogError(c, err.Error()) | ||
return | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package controller | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var allowedMediaType = []string{"video/mp4"} | ||
|
||
type ErrorInfo struct { | ||
Time time.Time | ||
Path string | ||
Method string | ||
} | ||
|
||
func GetAllowFormat(format string, allowFormat []string) string { | ||
var allow string | ||
for _, item := range allowFormat { | ||
if format == item { | ||
allow = item | ||
} | ||
} | ||
return allow | ||
} | ||
|
||
func LogError(c *gin.Context, message string) { | ||
var errorInfo ErrorInfo | ||
errorInfo.Time = time.Now() | ||
errorInfo.Path = c.FullPath() | ||
errorInfo.Method = c.Request.Method | ||
|
||
var log = logrus.New() | ||
log.Out = os.Stdout | ||
file, err := os.OpenFile("log/logging.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) | ||
if err == nil { | ||
log.Out = file | ||
} else { | ||
log.Println(err) | ||
} | ||
|
||
log.WithFields(logrus.Fields{ | ||
"Path": errorInfo.Path, | ||
"Method": errorInfo.Method, | ||
}).Error(message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http" | ||
"path" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func HomePage(c *gin.Context) { | ||
c.HTML(http.StatusOK, "index.html", gin.H{}) | ||
} | ||
|
||
func UploadVideo(c *gin.Context) { | ||
file, err := c.FormFile("video-file") | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, err.Error()) | ||
LogError(c, err.Error()) | ||
return | ||
} | ||
|
||
fileType := file.Header["Content-Type"][0] | ||
|
||
if GetAllowFormat(fileType, allowedMediaType) == "" { | ||
c.JSON(http.StatusBadRequest, "Định dạng không hợp lệ. Yêu cầu: mp4") | ||
LogError(c, "Định dạng không hợp lệ. Yêu cầu: mp4") | ||
return | ||
} | ||
|
||
err = c.SaveUploadedFile(file, path.Join("./upload", file.Filename)) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, err.Error()) | ||
LogError(c, err.Error()) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
time="2019-10-12T09:10:53+07:00" level=error msg="Định dạng không hợp lệ. Yêu cầu: mp4" Error="Định dạng không hợp lệ. Yêu cầu: mp4" Path=/upload-video Time="2019-10-12 09:10:53.860303 +0700 +07 m=+19.269711617" | ||
time="2019-10-12T09:12:47+07:00" level=error msg="Định dạng không hợp lệ. Yêu cầu: mp4" Path=/upload-video Time="2019-10-12 09:12:47.071071 +0700 +07 m=+8.262930654" | ||
time="2019-10-12T09:14:08+07:00" level=error msg="Định dạng không hợp lệ. Yêu cầu: mp4" Path=/upload-video | ||
time="2019-10-12T09:17:06+07:00" level=error msg="Định dạng không hợp lệ. Yêu cầu: mp4" Method=POST Path=/upload-video |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"gin-gonic/logging/controller" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func main() { | ||
router := gin.Default() | ||
router.LoadHTMLGlob("view/*") | ||
|
||
router.GET("/", controller.HomePage) | ||
router.POST("/upload-video", controller.UploadVideo) | ||
router.Run(":8085") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>Upload file</title> | ||
</head> | ||
<body> | ||
<h2>Upload video</h2> | ||
<form action="/upload-video" method="POST" enctype="multipart/form-data"> | ||
<input type="file" name="video-file" accept="video/*"> | ||
<input type="submit"> | ||
</form> | ||
</body> | ||
</html> |