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

1.main.go 代码

go 复制代码
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"
)
type Response struct {
	Status  string
	Message string
	Data    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

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

视频可以正常播放

可以跳转进度条

暂停等操作

相关推荐
踢球的打工仔10 分钟前
前端html(1)
前端·算法·html
yinmaisoft11 分钟前
6 大数据库一键连!JNPF 数据中心数据源链接,表单数据互通无压力
前端·数据库·低代码·信息可视化
黛色正浓12 分钟前
【React】极客园案例实践-发布文章模块
前端·react.js·前端框架
EasyCVR12 分钟前
视频融合平台EasyCVR:构建智慧货运汽车安全监控与管理新体系
大数据·汽车·音视频
开发者小天14 分钟前
react的组件库antd design表格多选,删除的基础示例
前端·javascript·react.js
by__csdn20 分钟前
Vue3响应式系统详解:ref与reactive全面解析
前端·javascript·vue.js·typescript·ecmascript·css3·html5
渴望成为python大神的前端小菜鸟21 分钟前
react 面试题
前端·react.js·前端框架·react·面试题
wan_da_ren22 分钟前
Windows 环境下使用 Go Modules 拉取带外层 Basic Auth 的私有 GitLab 仓库 — 完整解决方案
windows·golang·gitlab
Greatlifeee22 分钟前
基于vue3+ts的前端网页,实现网页点击按钮打开本地exe运行文件的完整实例
前端