Uptimeow/web/handler.go
2024-10-17 18:47:37 +08:00

25 lines
615 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package web
import (
"net/http"
"path/filepath"
)
// IndexHandler 处理除/api/以外的所有HTTP请求
func IndexHandler(w http.ResponseWriter, r *http.Request) {
// 获取请求的文件路径
path := r.URL.Path
// 如果请求的是根目录则重定向到默认的index文件
if path == "/" {
path = "/index.html"
}
// 获取文件的真实路径,这里假设网站的内容都放在名为"static"的目录下
// 请根据实际情况修改这个路径
staticFilePath := filepath.Join("public", path)
// 使用http.ServeFile来响应文件请求
http.ServeFile(w, r, staticFilePath)
}