This commit is contained in:
2024-08-28 09:36:43 +07:00
parent cfc3a7cbe1
commit d976bb4a59
7 changed files with 61 additions and 51 deletions

53
server/server.go Normal file
View File

@ -0,0 +1,53 @@
package server
import (
"fmt"
"net"
"git.rouggy.com/rouggy/RaceBot/internal/domain"
)
type Server struct {
listenAddr string
listener net.Listener
Conn net.Conn
}
func NewServer(listenAddr string) *Server {
return &Server{
listenAddr: listenAddr,
}
}
func (s *Server) Start(cfg *domain.Config) {
tcpAddr, _ := net.ResolveTCPAddr("tcp", cfg.Host+":"+cfg.Port)
s.listener, _ = net.ListenTCP("tcp", tcpAddr)
fmt.Println("[Server] Server is running on port", cfg.Port)
for {
s.Conn, _ = s.listener.Accept()
go s.handleRequest()
}
}
func (s *Server) handleRequest() {
//we make a buffer to hold the incoming data.
buf := make([]byte, 1024)
// Read the incoming connection into the buffer.
reqLen, err := s.Conn.Read(buf)
if err != nil {
fmt.Println("Error reading:", err.Error())
}
// Print the message to the console.
fmt.Println("Received data:", string(buf[:reqLen]))
// Write a response back to the client.
s.Conn.Write([]byte("Message received."))
// Close the connection when you're done with it.
s.Conn.Close()
}