switch to std lib http handler
This commit is contained in:
parent
72948ff24c
commit
d39763a2e0
1 changed files with 11 additions and 18 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue