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
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)
}