pas/main.go

42 lines
979 B
Go
Raw Normal View History

2024-07-15 20:24:27 +02:00
package pas
2024-06-08 20:28:43 +02:00
import (
"bufio"
"log"
"net"
2024-07-15 20:24:27 +02:00
"strconv"
"strings"
2024-06-08 20:28:43 +02:00
"git.gay/exhq/pas/src/Constants"
"git.gay/exhq/pas/src/helper"
"git.gay/exhq/pas/src/parser"
2024-06-08 20:28:43 +02:00
)
2024-07-15 20:24:27 +02:00
func StartServer(port int, handler func(request Constants.Request, response func(Constants.Response)), errr error) error {
ln, err := net.Listen("tcp", ":"+strconv.Itoa(port))
2024-06-08 20:28:43 +02:00
if err != nil {
2024-07-15 20:24:27 +02:00
return err
2024-06-08 20:28:43 +02:00
}
for {
conn, err := ln.Accept()
if err != nil {
2024-07-15 20:24:27 +02:00
return err
2024-06-08 20:28:43 +02:00
}
2024-07-15 20:24:27 +02:00
go handleConnection(conn, handler)
}
}
2024-07-14 04:49:21 +02:00
2024-07-15 20:24:27 +02:00
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
2024-06-08 20:28:43 +02:00
}
2024-07-15 20:24:27 +02:00
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)))
})
2024-06-08 20:28:43 +02:00
}