pas/main.go

48 lines
1.2 KiB
Go
Raw Permalink 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/Constants"
"git.gay/exhq/pas/helper"
"git.gay/exhq/pas/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)
2024-07-18 19:17:10 +02:00
body := strings.SplitN(raw, "\n\n", 2)
if len(body) < 2 {
conn.Write(helper.StringToByteWithEnd(helper.IntentGenerator(Constants.Response{OperationSuccessClassification: Constants.INTERNALSERVERERROR, ResponseHeader: map[string]string{}, Body: "shit you sent wasnt pas bruh"})))
return
}
request := Constants.Request{Intent: parsed, Body: body[1]}
2024-07-15 20:24:27 +02:00
handler(request, func(c Constants.Response) {
conn.Write(helper.StringToByteWithEnd(helper.IntentGenerator(c)))
})
2024-06-08 20:28:43 +02:00
}