codecrafters-http-server-go/app/server.go

128 lines
2.8 KiB
Go
Raw Normal View History

2024-05-24 22:03:55 +02:00
package main
import (
"fmt"
2024-05-25 11:51:11 +02:00
"net"
"os"
2024-05-25 00:22:42 +02:00
"regexp"
2024-05-24 22:03:55 +02:00
)
2024-05-25 11:51:11 +02:00
func handleConnection(conn net.Conn, routes Routes) {
2024-05-24 23:55:58 +02:00
defer conn.Close()
2024-05-25 12:11:36 +02:00
2024-05-25 12:31:01 +02:00
request, ok := parseRequest(conn)
if !ok {
Respond(conn, Response{Version: "HTTP/1.1", Code: BadRequest})
return
2024-05-24 23:55:58 +02:00
}
fmt.Println(request)
2024-05-25 11:51:11 +02:00
for _, route := range routes.stringRoutes {
if request.Path == route.path && request.Method == route.method {
Respond(conn, route.handler(request))
2024-05-25 00:29:26 +02:00
return
}
}
2024-05-25 11:51:11 +02:00
for _, route := range routes.regexpRoutes {
if request.Method != route.method {
continue
2024-05-25 00:45:53 +02:00
}
2024-05-25 11:51:11 +02:00
if matches := route.regex.FindStringSubmatch(request.Path); len(matches) > 0 {
Respond(conn, route.handler(request, matches))
2024-05-25 00:49:48 +02:00
return
}
2024-05-25 00:45:53 +02:00
}
2024-05-25 00:22:42 +02:00
Respond(conn, Response{Version: request.Version, Code: NotFound})
2024-05-24 22:56:28 +02:00
}
2024-05-24 22:03:55 +02:00
func main() {
fmt.Println("Logs from your program will appear here!")
2024-05-24 22:46:34 +02:00
l, err := net.Listen("tcp", "0.0.0.0:4221")
if err != nil {
fmt.Println("Failed to bind to port 4221")
os.Exit(1)
}
2024-05-25 11:51:11 +02:00
routes := Routes{
stringRoutes: []StringRoute{
// ROOT
{path: "/", method: "GET", handler: func(req Request) Response {
return Response{Version: req.Version, Code: OK}
}},
// USER AGENT
{path: "/user-agent", method: "GET", handler: func(req Request) Response {
for _, header := range req.Headers {
if header.Name != "User-Agent" {
continue
}
return Response{
Version: req.Version,
Code: OK,
Body: header.Value,
Headers: []Header{{Name: "Content-Type", Value: "text/plain"}},
}
}
return Response{Version: req.Version, Code: BadRequest}
}},
},
regexpRoutes: []RegexRoute{
// PATH PARAMETER
{
regex: regexp.MustCompile(`^/echo/([A-Za-z]+)$`),
method: "GET",
handler: func(req Request, matches []string) Response {
return Response{
Version: req.Version,
Code: OK,
Body: matches[1],
Headers: []Header{{Name: "Content-Type", Value: "text/plain"}},
}
2024-05-25 12:11:36 +02:00
},
},
{
regex: regexp.MustCompile(`^/files/([A-Za-z0-9_\-.]+)`),
method: "GET",
handler: func(req Request, matches []string) Response {
file, notFound := readFile(matches[1])
if notFound {
return Response{Version: req.Version, Code: NotFound}
}
return Response{
Version: req.Version,
Code: OK,
BodyRaw: file,
Headers: []Header{{Name: "Content-Type", Value: "application/octet-stream"}},
}
},
},
{
regex: regexp.MustCompile(`^/files/([A-Za-z0-9_\-.]+)`),
method: "POST",
handler: func(req Request, matches []string) Response {
writeFile(matches[1], []byte(req.Body))
2024-05-25 12:12:32 +02:00
return Response{Version: req.Version, Code: Created}
2024-05-25 12:11:36 +02:00
},
},
2024-05-25 11:51:11 +02:00
},
}
2024-05-24 22:56:28 +02:00
for {
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting connection: ", err.Error())
os.Exit(1)
}
2024-05-25 11:51:11 +02:00
go handleConnection(conn, routes)
2024-05-24 22:46:34 +02:00
}
2024-05-24 22:56:28 +02:00
2024-05-24 22:03:55 +02:00
}