44 lines
897 B
Go
44 lines
897 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Ensures gofmt doesn't remove the "net" and "os" imports above (feel free to remove this!)
|
|
var (
|
|
_ = net.Listen
|
|
_ = os.Exit
|
|
)
|
|
|
|
func main() {
|
|
// You can use print statements as follows for debugging, they'll be visible when running tests.
|
|
fmt.Println("Logs from your program will appear here!")
|
|
|
|
// Uncomment this block to pass the first stage
|
|
|
|
l, err := net.Listen("tcp", "0.0.0.0:4221")
|
|
if err != nil {
|
|
fmt.Println("Failed to bind to port 4221")
|
|
os.Exit(1)
|
|
}
|
|
defer l.Close()
|
|
conn, err := l.Accept()
|
|
if err != nil {
|
|
fmt.Println("Error accepting connection: ", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
req := make([]byte, 1024)
|
|
|
|
conn.Read(req)
|
|
|
|
if !strings.HasPrefix(string(req), "GET / HTTP/1.1") {
|
|
conn.Write([]byte("HTTP/1.1 404 Not Found\r\n\r\n"))
|
|
conn.Close()
|
|
return
|
|
}
|
|
conn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
|
|
}
|