refactor - pasdns can now look sane 3.0
This commit is contained in:
parent
57fbfbf059
commit
61f89c8067
7 changed files with 3 additions and 3 deletions
41
main.go
Normal file
41
main.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package pas
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.gay/exhq/pas/src/Constants"
|
||||
"git.gay/exhq/pas/src/helper"
|
||||
"git.gay/exhq/pas/src/parser"
|
||||
)
|
||||
|
||||
func StartServer(port int, handler func(request Constants.Request, response func(Constants.Response)), errr error) error {
|
||||
ln, err := net.Listen("tcp", ":"+strconv.Itoa(port))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go handleConnection(conn, handler)
|
||||
}
|
||||
}
|
||||
|
||||
func handleConnection(conn net.Conn, handler func(request Constants.Request, response func(Constants.Response))) {
|
||||
defer conn.Close()
|
||||
raw, err := helper.ReadUntilNullString(bufio.NewReader(conn))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
parsed := parser.ParseIntent(raw)
|
||||
request := Constants.Request{Intent: parsed, Body: strings.SplitN(raw, "\n\n", 2)[1]}
|
||||
handler(request, func(c Constants.Response) {
|
||||
conn.Write(helper.StringToByteWithEnd(helper.IntentGenerator(c)))
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue