package server import ( "encoding/json" "fmt" "log" "net" "git.rouggy.com/rouggy/RaceBot/internal/domain" "git.rouggy.com/rouggy/RaceBot/models" ) 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) log.Printf("[Server] Server is running on %v:%v", cfg.Host, cfg.Port) for { s.Conn, _ = s.listener.Accept() go s.handleRequest() } } func (s *Server) handleRequest() { r := models.NewRace() d := json.NewDecoder(s.Conn) err := d.Decode(&r) log.Println(r, err) //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. log.Println(string(buf[:reqLen])) // Close the connection when you're done with it. s.Conn.Close() }