[UMU 学 golang](5) HTTP Echo Server

前端之家收集整理的这篇文章主要介绍了[UMU 学 golang](5) HTTP Echo Server前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

  做加速时经常需要用到 HTTP Echo Server 来测试加速有没有成功,如果成功了,是显示请求来自加速代理服务器。原来用 node.js 写了一个,代码如下:

var http = require('http');
http.createServer(
    function (req,res) {
        res.writeHead(200,{'Content-Type': 'text/plain'});
        var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress;
        var ua = req.headers['user-agent'];
        res.end(ip + '\n' + ua);
    }
).listen(80);
console.log('Server is running...');

  现在改为 golang,并稍微改进了一下:

package main

import (
	"fmt"
	"net/http"
	"os"
	"time"
)

func main() {
	var addr string
	if len(os.Args) > 1 {
		addr = ":" + os.Args[1]
	} else {
		addr = ":80"
	}
	server := http.Server{
		Addr:        addr,Handler:     &MyHandler{},ReadTimeout: 5 * time.Second,}
	err := server.ListenAndServe()
	fmt.Println(err)
}

type MyHandler struct{}

func (*MyHandler) ServeHTTP(w http.ResponseWriter,r *http.Request) {
	info := r.RemoteAddr + " @ " + time.Now().String() + "\r\n" +
		r.Method + " " + r.RequestURI + "\r\n" +
		"Host: " + r.Host + "\r\n" +
		"UserAgent: " + r.UserAgent() + "\r\n"
	w.Write([]byte(info))
	fmt.Println(info)
}
原文链接:https://www.f2er.com/go/190157.html

猜你在找的Go相关文章