From d39763a2e0619c1a052667801a65c080e1f8dac4 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Mon, 21 Aug 2023 12:39:38 +0500 Subject: [PATCH] switch to std lib http handler --- http/server.go | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/http/server.go b/http/server.go index 516426f..788cb7e 100644 --- a/http/server.go +++ b/http/server.go @@ -1,34 +1,27 @@ package http import ( - "net" + "net/http" "fmt" ) func NewServer(port string) (error) { - l, err := net.Listen("tcp", ":" + port) - if err != nil { + server := http.NewServeMux() + server_setup(server) + if err := http.ListenAndServe(":" + port, server); err != nil { return err } - go start(l) return nil } -func start(l net.Listener) { - for { - conn, err := l.Accept() - if err != nil { - continue - } - go handle_conn(conn) - } +func server_setup(server *http.ServeMux) { + server.HandleFunc("/", test) } -func handle_conn(conn net.Conn) { - defer conn.Close() - buf := make([]byte, 1024) - if _, err := conn.Read(buf); err != nil { - return +func test(w http.ResponseWriter, r *http.Request) { + test_buf := make([]byte, 32) + copy(test_buf, "test\ntest") + if _, err := w.Write(test_buf); err != nil { + fmt.Println(err) } - fmt.Println(buf) }