当前位置: 首页 > news >正文

GO启动一个视频下载接口 前端可以边下边放

1.main.go 代码

package mainimport ("encoding/json""fmt""log""net/http""os""time"
)
type Response struct {Status  stringMessage stringData    string
}func main() {// 设置路由http.HandleFunc("/", homeHandler)http.HandleFunc("/api/video/", videoHandler)// 启动服务器port := ":8080"fmt.Printf("🚀 服务器运行中,访问地址: http://localhost%s\n", port)fmt.Println("=========================================")err := http.ListenAndServe(port, nil)if err != nil {log.Fatalf("❌ 启动服务器失败: %v", err)}
}// 主页处理器
func homeHandler(w http.ResponseWriter, r *http.Request) {if r.URL.Path != "/" {http.NotFound(w, r)return}response := Response{Status:  "success",Message: "欢迎使用用户API服务",Data:    "请访问 /api/users 获取用户数据",}sendJSON(w, http.StatusOK, response)
}// 获取视频
func videoHandler(w http.ResponseWriter, r *http.Request) {const videoDir = "./videos/"// 提取视频文件名path := r.URL.Path[len("/api/video/"):]if path == "" {http.Error(w, "Missing video filename", http.StatusBadRequest)return}filePath := videoDir + path_, err := os.Stat(filePath)if os.IsNotExist(err) {http.Error(w, "Video not found", http.StatusNotFound)return}if err != nil {log.Printf("Error getting file info: %v", err)http.Error(w, "Internal Server Error", http.StatusInternalServerError)return}// 设置响应头w.Header().Set("Content-Type", "video/mp4")w.Header().Set("Access-Control-Allow-Origin", "*") // 允许所有来源跨域访问w.Header().Set("Cache-Control", "max-age=3600, public")w.Header().Set("Accept-Ranges", "bytes")http.ServeFile(w, r, filePath)
}// 发送JSON响应
func sendJSON(w http.ResponseWriter, statusCode int, data interface{}) {w.Header().Set("Content-Type", "application/json")w.WriteHeader(statusCode)if err := json.NewEncoder(w).Encode(data); err != nil {log.Printf("❌ JSON编码失败: %v", err)http.Error(w, "Internal Server Error", http.StatusInternalServerError)}
}

1.1在main.go同级建一个videos文件夹放一个 tem1.mp4

2.前端代码

这里简单使用html

<!doctype html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Document</title>
</head>
<body>
<video controls><source src="http://localhost:8080/api/video/tem1.mp4" type="video/mp4">您的浏览器不支持视频播放。
</video></body>
</html>

3.启动go服务

在这里插入图片描述

4.使用浏览器打开html

视频可以正常播放
可以跳转进度条
暂停等操作
在这里插入图片描述

http://www.dtcms.com/a/272415.html

相关文章:

  • 【PyTorch】PyTorch中的数据预处理操作
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | DoubleVerticalSlider(双垂直滑块)
  • 图解LeetCode:79递归实现单词搜索
  • Django+DRF 实战:自定义异常处理流程
  • 20.4 量子安全加密算法
  • 案例分享--福建洋柄水库大桥智慧桥梁安全监测(二)之数字孪生和系统平台
  • 机器学习13——支持向量机下
  • TCP传输控制层协议深入理解
  • 当CCLinkIE撞上Modbus TCP:照明控制系统的“方言战争”终结术
  • VIP可读
  • 线性回归与正则化
  • Django专家成长路线知识点——AI教你学Django
  • 【PTA数据结构 | C语言版】顺序栈的3个操作
  • 【深度学习系列--经典论文解读】Gradient-Based Learning Applied to Document Recognition
  • LINUX710 MYSQL
  • linux-用户与用户组管理
  • serialVersionUID
  • 配置 msvsmon.exe 以无身份验证启动
  • 力扣打卡第23天 二叉搜索树中的众数
  • 算法题(171):组合型枚举
  • Shusen Wang推荐系统学习 --召回 矩阵补充 双塔模型
  • 深度探索:实时交互与增强现实翻译技术(第六篇)
  • Win10用camke+gcc编译opencv库报错error: ‘_hypot‘ has not been declared in ‘std‘
  • 什么是 领域偏好学习(DPO)与多目标强化学习(PPO)
  • 在 Ubuntu 22 部署 vLLM + Qwen3 32B 模型
  • EPLAN 电气制图(六):电机正反转副勾主电路绘制
  • STM32第十九天 ESP8266-01S和电脑实现串口通信(2)
  • 代理模式——Java
  • 机器学习14——线性回归
  • 前端项目vue3项目集成eslint@9.x跟prettier