This repository has been archived by the owner on Dec 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdinolog.go
91 lines (67 loc) · 1.52 KB
/
dinolog.go
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
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
"github.com/bauripalash/dinolog/lib"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetLevel(log.DebugLevel)
log.SetFormatter(&log.TextFormatter{
PadLevelText: true,
FullTimestamp: true,
})
log.SetOutput(os.Stdout)
}
func handleCon(c net.Conn) {
cf := lib.OpenServerConfig("server.toml")
log.Info("NEW CLIENT : " , c.RemoteAddr().String())
for {
rawData, err := bufio.NewReader(c).ReadString('\n')
nw := bufio.NewWriter(c)
if err != nil {
//fmt.Println(err)
log.Fatal("Failed to read from Network buffer with err => " , err)
return
}
tmp := strings.TrimSpace(string(rawData))
rawRequest := strings.TrimSpace(tmp)
log.Info("NEW REQ : " , rawRequest)
raw_res := lib.ParseRequest(rawRequest, cf)
res := lib.NewResponse(raw_res)
nw.Write(res)
nw.Flush()
if rawRequest == "+0" { //Not related to any spec; just for convenience
log.Info("CLIENT QUIT : ", c.RemoteAddr().String())
break
}
log.Debug("NEW UNKNOWN CMD: " , rawRequest)
nw.WriteString("Unknown command\n")
}
c.Close()
}
func main() {
x := true
if x {
var PORT int = 2001
var ADDRESS string = fmt.Sprintf("127.0.0.1:%d", PORT)
l, err := net.Listen("tcp4", ADDRESS)
log.Info("SERVER LIVE " , ADDRESS)
if err != nil {
fmt.Println(err)
return
}
defer l.Close()
for {
c, err := l.Accept()
if err != nil {
fmt.Println(err)
return
}
go handleCon(c)
}
}
}