From 6ced549deaecb42b9bb93ea9efcb4c1bbaabe8a4 Mon Sep 17 00:00:00 2001 From: mhsanaei Date: Sat, 20 Sep 2025 09:35:50 +0200 Subject: docs: add comments for all functions --- web/controller/util.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'web/controller/util.go') diff --git a/web/controller/util.go b/web/controller/util.go index e180b1fe..b11203bd 100644 --- a/web/controller/util.go +++ b/web/controller/util.go @@ -12,6 +12,7 @@ import ( "github.com/gin-gonic/gin" ) +// getRemoteIp extracts the real IP address from the request headers or remote address. func getRemoteIp(c *gin.Context) string { value := c.GetHeader("X-Real-IP") if value != "" { @@ -27,14 +28,17 @@ func getRemoteIp(c *gin.Context) string { return ip } +// jsonMsg sends a JSON response with a message and error status. func jsonMsg(c *gin.Context, msg string, err error) { jsonMsgObj(c, msg, nil, err) } +// jsonObj sends a JSON response with an object and error status. func jsonObj(c *gin.Context, obj any, err error) { jsonMsgObj(c, "", obj, err) } +// jsonMsgObj sends a JSON response with a message, object, and error status. func jsonMsgObj(c *gin.Context, msg string, obj any, err error) { m := entity.Msg{ Obj: obj, @@ -52,6 +56,7 @@ func jsonMsgObj(c *gin.Context, msg string, obj any, err error) { c.JSON(http.StatusOK, m) } +// pureJsonMsg sends a pure JSON message response with custom status code. func pureJsonMsg(c *gin.Context, statusCode int, success bool, msg string) { c.JSON(statusCode, entity.Msg{ Success: success, @@ -59,6 +64,7 @@ func pureJsonMsg(c *gin.Context, statusCode int, success bool, msg string) { }) } +// html renders an HTML template with the provided data and title. func html(c *gin.Context, name string, title string, data gin.H) { if data == nil { data = gin.H{} @@ -81,6 +87,7 @@ func html(c *gin.Context, name string, title string, data gin.H) { c.HTML(http.StatusOK, name, getContext(data)) } +// getContext adds version and other context data to the provided gin.H. func getContext(h gin.H) gin.H { a := gin.H{ "cur_ver": config.GetVersion(), @@ -91,6 +98,7 @@ func getContext(h gin.H) gin.H { return a } +// isAjax checks if the request is an AJAX request. func isAjax(c *gin.Context) bool { return c.GetHeader("X-Requested-With") == "XMLHttpRequest" } -- cgit v1.2.3