switch to std lib http handler

This commit is contained in:
Muaz Ahmad 2023-08-21 12:39:38 +05:00
parent 72948ff24c
commit d39763a2e0

View file

@ -1,34 +1,27 @@
package http package http
import ( import (
"net" "net/http"
"fmt" "fmt"
) )
func NewServer(port string) (error) { func NewServer(port string) (error) {
l, err := net.Listen("tcp", ":" + port) server := http.NewServeMux()
if err != nil { server_setup(server)
if err := http.ListenAndServe(":" + port, server); err != nil {
return err return err
} }
go start(l)
return nil return nil
} }
func start(l net.Listener) { func server_setup(server *http.ServeMux) {
for { server.HandleFunc("/", test)
conn, err := l.Accept()
if err != nil {
continue
}
go handle_conn(conn)
}
} }
func handle_conn(conn net.Conn) { func test(w http.ResponseWriter, r *http.Request) {
defer conn.Close() test_buf := make([]byte, 32)
buf := make([]byte, 1024) copy(test_buf, "test\ntest")
if _, err := conn.Read(buf); err != nil { if _, err := w.Write(test_buf); err != nil {
return fmt.Println(err)
} }
fmt.Println(buf)
} }