-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
67 lines (54 loc) · 1.17 KB
/
Copy pathmain.go
File metadata and controls
67 lines (54 loc) · 1.17 KB
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
package main
import (
"bufio"
"fmt"
"net"
"os"
"./parser"
"./response"
//"github.com/johnlonganecker/web-server/parser"
)
const (
CONN_HOST = "localhost"
CONN_PORT = "3333"
CONN_TYPE = "tcp"
)
func main() {
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
defer l.Close()
fmt.Println("listening on " + CONN_HOST + ":" + CONN_PORT)
for {
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
go HandleRequest(conn)
}
}
func HandleRequest(conn net.Conn) {
resp := response.Response{
Version: "HTTP/1.1",
Status: "200",
ReasonPhrase: "OK",
Body: "Hello, World",
Headers: "",
}
//t := time.Now()
//date := fmt.Printf("%d, %d ", t.Weekday().t.Day())
//t.Format("Mon, 02 Jan 2017 12:00:00 GMT")
resp.AddHeader("Content-Length", "12")
resp.AddHeader("Content-Type", "text/plain; charset=iso-latin-1")
//resp.AddHeader("Date", date)
scanner := bufio.NewScanner(conn)
parser.Parse(scanner)
conn.Write([]byte(resp.String()))
err := conn.Close()
if err != nil {
fmt.Println(err)
}
}