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

161 lines
3.5 KiB
Go
Raw Normal View History

2024-05-24 22:03:55 +02:00
package main
import (
"fmt"
2024-05-24 23:55:58 +02:00
"log"
2024-05-25 11:51:11 +02:00
"net"
"os"
2024-05-25 00:22:42 +02:00
"regexp"
2024-05-24 23:55:58 +02:00
"strings"
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-24 23:55:58 +02:00
buffer := make([]byte, 1024)
n, err := conn.Read(buffer)
if err != nil {
log.Fatal(err)
}
contents := string(buffer[:n])
parts := strings.Split(contents, HTTPDelimiter)
request := Request{}
isBody := false
for i, part := range parts {
if i == 0 {
head := strings.Split(part, " ")
if len(head) != 3 {
2024-05-25 00:22:42 +02:00
Respond(conn, Response{Version: "HTTP/1.1", Code: BadRequest})
2024-05-24 23:55:58 +02:00
return
}
request.Method = head[0]
request.Path = head[1]
request.Version = head[2]
continue
}
if isBody {
request.Body = part
break
}
// Headers
if part == "" {
isBody = true
continue
}
h := strings.SplitN(part, ": ", 2)
header := Header{Name: h[0], Value: h[1]}
request.Headers = append(request.Headers, header)
}
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
}