aboutsummaryrefslogtreecommitdiff
path: root/app/server.go
diff options
context:
space:
mode:
Diffstat (limited to 'app/server.go')
-rw-r--r--app/server.go50
1 files changed, 32 insertions, 18 deletions
diff --git a/app/server.go b/app/server.go
index 59e20bf..93a45d0 100644
--- a/app/server.go
+++ b/app/server.go
@@ -2,26 +2,40 @@ package main
import (
"fmt"
- // Uncomment this block to pass the first stage
- // "net"
- // "os"
+ "net"
+ "os"
)
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!")
+ l, err := net.Listen("tcp", "0.0.0.0:4221")
+ if err != nil {
+ fmt.Println("Failed to bind to port 4221")
+ os.Exit(1)
+ }
- // 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)
- // }
- //
- // _, err = l.Accept()
- // if err != nil {
- // fmt.Println("Error accepting connection: ", err.Error())
- // os.Exit(1)
- // }
+ conn, err := l.Accept()
+ if err != nil {
+ fmt.Println("Error accepting connection: ", err.Error())
+ os.Exit(1)
+ }
+
+ reader := NewRequestReader(conn)
+
+ target, err := reader.Target()
+ if err != nil {
+ fmt.Println("Error getting target: ", err.Error())
+ os.Exit(1)
+ }
+
+ if target == "/" {
+ _, err = conn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
+ if err != nil {
+ fmt.Println("Error connection write: ", err.Error())
+ }
+ } else {
+ _, err = conn.Write([]byte("HTTP/1.1 404 Not Found\r\n\r\n"))
+ if err != nil {
+ fmt.Println("Error connection write: ", err.Error())
+ }
+ }
}