0%

go实现http服务的两种方式

1. Go本身自己封装实现了非常简单的httpServer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main

import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"strings"
)

func main() {
//http请求处理
http.HandleFunc("/", handler1)
//绑定监听地址和端口
http.ListenAndServe("localhost:8080", nil)
}

//请求处理函数
func handler1(w http.ResponseWriter, r *http.Request) {
//获取请求资源
path := r.URL.Path
if strings.Contains(path[1:], "") {
//返回请求资源
fmt.Fprintf(w, getHtmlFile("index.html"))
} else {
if strings.Contains(path[1:], ".html") {
w.Header().Set("content-type", "text/html")
fmt.Fprintf(w, getHtmlFile(path[1:]))
}
if strings.Contains(path[1:], ".css") {
w.Header().Set("content-type", "text/css")
fmt.Fprintf(w, getHtmlFile(path[1:]))
}
if strings.Contains(path[1:], ".js") {
w.Header().Set("content-type", "text/javascript")
fmt.Fprintf(w, getHtmlFile(path[1:]))
}
if strings.Contains(path[1:], "") {
fmt.Print(strings.Contains(path[1:], ""))
}
}

}

func getHtmlFile(path string) (fileHtml string) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()

rd := bufio.NewReader(file)
for {
line, err := rd.ReadString('\n')

if err != nil || io.EOF == err {
break
}
fileHtml += line
}
return fileHtml
}

2. 但对于自己实现简易的服务器,最好还是用socket去实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main

import (
"bufio"
"fmt"
"io"
"log"
"net"
"os"
"strings"
)

func main() {
//确定协议及绑定IP及端口
netListen, err := net.Listen("tcp", "localhost:8080")
CheckError(err)
defer netListen.Close()
Log("waiting for client request")
for {
//接受请求连接
conn, err := netListen.Accept()
if err != nil {
CheckError(err)
break
} else {
Log(conn.RemoteAddr().String(), "tcp connect success")
//处理请求连接
handleConnection(conn)
}
conn.Close()
}
}

//处理请求连接函数
func handleConnection(conn net.Conn) {
buffer := make([]byte, 2048)
n, err := conn.Read(buffer)
if err != nil {
Log(conn.RemoteAddr().String(), " connection error: ", err)
conn.Close()
} else {
Log(conn.RemoteAddr().String(), "receive data string:\n", string(buffer[:n]))
//返回数据给客户端
responseInfoToClient(conn, string(buffer[:n]), err)
}
}

//返回数据的函数
func responseInfoToClient(conn net.Conn, requestInfo string, err error) {
//获取http协议头
conn.Write([]byte(getFileContent("head.md")))
conn.Write([]byte("\n"))
var path string = strings.Replace(getMidStr(requestInfo, "GET /", "HTTP"), " ", "", -1)
fmt.Println(path)
if path != "" {
if path == "favicon.ico" {
fmt.Println("every connect hava favicon.ico resource request")
} else {
_, err = os.Open(path)
if err != nil {
fmt.Println("RESTful")
} else {
conn.Write([]byte(getFileContent(path)))
}
}
} else {
conn.Write([]byte(getFileContent("index.html")))
}
}

func getMidStr(data string, startStr string, endStr string) (reqSouce string) {
var startIndex int = strings.Index(data, startStr)
var info string
if startIndex >= 0 {
startIndex += len(startStr)
var endIndex int = strings.Index(data, endStr)
info = data[startIndex:endIndex]
}
return info
}

func getFileContent(path string) (fileInfo string) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()

rd := bufio.NewReader(file)
for {
line, err := rd.ReadString('\n')

if err != nil || io.EOF == err {
break
}
fileInfo += line
}
return fileInfo
}

func Log(v ...interface{}) {
log.Println(v...)
}

func CheckError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}