模块二-作业完成
编写一个 HTTP 服务器,大家视个人不同情况决定完成到哪个环节,但尽量把 1 都做完:
接收客户端 request,并将 request 中带的 header 写入 response header
读取当前系统的环境变量中的 VERSION 配置,并写入 response header
Server 端记录访问日志包括客户端 IP,HTTP 返回码,输出到 server 端的标准输出
当访问 localhost/healthz 时,应返回 200
代码如下
package main
import (
"fmt"
"log"
"net"
"net/http"
"net/http/pprof"
"os"
"strings"
)
func index(w http.ResponseWriter, r *http.Request) {
os.Setenv("VERSION", "v0.0.1")
version := os.Getenv("VERSION")
w.Header().Set("VERSION", version)
fmt.Printf("os version: %s \n", version)
for k, v := range r.Header {
for _, vv := range v {
fmt.Printf("Header key: %s, Header value: %s \n", k, v)
w.Header().Set(k, vv)
}
}
clientip := getCurrentIP(r)
log.Printf("Success! Response code: %d", 200)
log.Printf("Success! clientip: %s", clientip)
}
func healthz(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "working")
}
func getCurrentIP(r *http.Request) string {
ip := r.Header.Get("X-Real-IP")
if ip == "" {
ip = strings.Split(r.RemoteAddr, ":")[0]
}
return ip
}
func ClientIP(r *http.Request) string {
xForwardedFor := r.Header.Get("X-Forwarded-For")
ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
if ip != "" {
return ip
}
ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
if ip != "" {
return ip
}
if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
return ip
}
return ""
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.HandleFunc("/", index)
mux.HandleFunc("/healthz", healthz)
if err := http.ListenAndServe(":8080", mux); err != nil {
log.Fatalf("start http server failed, error: %s\n", err.Error())
}
}
测试
1、接收客户端 request,并将 request 中带的 header 写入 response header
2、读取当前系统的环境变量中的 VERSION 配置,并写入 response header
3、Server 端记录访问日志包括客户端 IP,HTTP 返回码,输出到 server 端的标准输出
测试
4、当访问 localhost/healthz 时,应返回 200