feat(*): go 后端项目脚手架
This commit is contained in:
42
internal/api/handler/proxy.go
Normal file
42
internal/api/handler/proxy.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/zhilv666/navsite/pkg/common"
|
||||
)
|
||||
|
||||
func Proxy(c *gin.Context) {
|
||||
_url := c.Query("url")
|
||||
if _url == "" {
|
||||
common.Fail(c, "missing url")
|
||||
return
|
||||
}
|
||||
// 请求目标 URL
|
||||
resp, err := http.Get(_url)
|
||||
if err != nil {
|
||||
common.Fail(c, "unable to fetch image")
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 检查 Content-Type 是否为图片
|
||||
contentType := resp.Header.Get("Content-Type")
|
||||
if !strings.HasPrefix(contentType, "image/") {
|
||||
common.Fail(c, "not an image file")
|
||||
return
|
||||
}
|
||||
|
||||
// 设置响应 Content-Type
|
||||
c.Writer.Header().Set("Content-Type", contentType)
|
||||
|
||||
// 将图片数据直接写入响应
|
||||
_, err = io.Copy(c.Writer, resp.Body)
|
||||
if err != nil {
|
||||
common.Fail(c, "failed to write image")
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user