加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 1.11 KB
一键复制 编辑 原始数据 按行查看 历史
xclmwc 提交于 2021-10-08 07:57 . 创建
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func init() {
logFile, err := os.OpenFile("./访问日志.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
log.SetOutput(logFile)
}
func logRequest(req *http.Request, status int) {
l := fmt.Sprintf("client IP: %v\tstatus: %v", req.RemoteAddr, status)
log.Print(l)
fmt.Println(l)
}
func healthz(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
logRequest(req, http.StatusOK)
}
func headers(w http.ResponseWriter, req *http.Request) {
htmlString := ""
version := os.Getenv("VERSION")
w.Header().Add("VERSION", version)
htmlString += fmt.Sprintf("VERSION: %v\n", version)
for name, headers := range req.Header {
for _, h := range headers {
w.Header().Add(name, h)
htmlString += fmt.Sprintf("%v: %v\n", name, h)
}
}
w.WriteHeader(http.StatusAccepted)
fmt.Fprint(w, htmlString)
logRequest(req, http.StatusAccepted)
}
func main() {
http.HandleFunc("/healthz", healthz)
http.HandleFunc("/", headers)
http.ListenAndServe(":8080", nil)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化