mirror of
https://github.com/cupcakearmy/codecrafters-http-server-go.git
synced 2024-12-22 16:16:31 +00:00
restructure
This commit is contained in:
parent
8de565cb41
commit
55ae922fa0
97
app/http.go
Normal file
97
app/http.go
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
HTTPDelimiter = "\r\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Header struct {
|
||||||
|
Name string
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
type Request struct {
|
||||||
|
Method string
|
||||||
|
Path string
|
||||||
|
Version string
|
||||||
|
Body string
|
||||||
|
Headers []Header
|
||||||
|
}
|
||||||
|
|
||||||
|
type HttpCode struct {
|
||||||
|
Code uint
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
BadRequest = HttpCode{Code: 400, Message: "Bad Response"}
|
||||||
|
NotFound = HttpCode{Code: 404, Message: "Not Found"}
|
||||||
|
OK = HttpCode{Code: 200, Message: "OK"}
|
||||||
|
)
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Code HttpCode
|
||||||
|
Version string
|
||||||
|
Body string
|
||||||
|
BodyRaw []byte
|
||||||
|
Headers []Header
|
||||||
|
}
|
||||||
|
|
||||||
|
type StringRoute struct {
|
||||||
|
path string
|
||||||
|
method string
|
||||||
|
handler func(req Request) Response
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegexRoute struct {
|
||||||
|
regex *regexp.Regexp
|
||||||
|
method string
|
||||||
|
handler func(req Request, matches []string) Response
|
||||||
|
}
|
||||||
|
|
||||||
|
type Routes struct {
|
||||||
|
stringRoutes []StringRoute
|
||||||
|
regexpRoutes []RegexRoute
|
||||||
|
}
|
||||||
|
|
||||||
|
// func createHandler(routes Routes) func(conn net.Conn) {
|
||||||
|
// return func(conn net.Conn) {
|
||||||
|
|
||||||
|
// for _, route := range(routes.stringRoutes){
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
func Respond(conn net.Conn, response Response) {
|
||||||
|
fmt.Fprintf(conn, "%s %d %s%s", response.Version, response.Code.Code, response.Code.Message, HTTPDelimiter)
|
||||||
|
bodySize := 0
|
||||||
|
if response.Body != "" {
|
||||||
|
bodySize = len(response.Body)
|
||||||
|
} else {
|
||||||
|
bodySize = len(response.BodyRaw)
|
||||||
|
}
|
||||||
|
if bodySize > 0 {
|
||||||
|
response.Headers = append(response.Headers, Header{Name: "Content-Length", Value: strconv.Itoa(bodySize)})
|
||||||
|
}
|
||||||
|
for _, header := range response.Headers {
|
||||||
|
fmt.Fprintf(conn, "%s: %s%s", header.Name, header.Value, HTTPDelimiter)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprint(conn, HTTPDelimiter)
|
||||||
|
if bodySize > 0 {
|
||||||
|
if response.Body != "" {
|
||||||
|
fmt.Fprint(conn, response.Body)
|
||||||
|
} else {
|
||||||
|
conn.Write(response.BodyRaw)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fmt.Fprintf(conn, "HTTP/1.1 %d %s%s%s", response.Code, response.Message, HTTPDelimiter, HTTPDelimiter)
|
||||||
|
}
|
232
app/server.go
232
app/server.go
@ -3,79 +3,25 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
// Uncomment this block to pass the first stage
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
func getFilepath(filename string) string {
|
||||||
HTTPDelimiter = "\r\n"
|
if len(os.Args) != 3 {
|
||||||
)
|
log.Fatal("Not enough args")
|
||||||
|
}
|
||||||
type Header struct {
|
dir, err := filepath.Abs(os.Args[2])
|
||||||
Name string
|
if err != nil {
|
||||||
Value string
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
type Request struct {
|
return filepath.Join(dir, filename)
|
||||||
Method string
|
|
||||||
Path string
|
|
||||||
Version string
|
|
||||||
Body string
|
|
||||||
Headers []Header
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type HttpCode struct {
|
func handleConnection(conn net.Conn, routes Routes) {
|
||||||
Code uint
|
|
||||||
Message string
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
BadRequest = HttpCode{Code: 400, Message: "Bad Response"}
|
|
||||||
NotFound = HttpCode{Code: 404, Message: "Not Found"}
|
|
||||||
OK = HttpCode{Code: 200, Message: "OK"}
|
|
||||||
)
|
|
||||||
|
|
||||||
type Response struct {
|
|
||||||
Code HttpCode
|
|
||||||
Version string
|
|
||||||
Body string
|
|
||||||
BodyRaw []byte
|
|
||||||
Headers []Header
|
|
||||||
}
|
|
||||||
|
|
||||||
func Respond(conn net.Conn, response Response) {
|
|
||||||
fmt.Fprintf(conn, "%s %d %s%s", response.Version, response.Code.Code, response.Code.Message, HTTPDelimiter)
|
|
||||||
bodySize := 0
|
|
||||||
if response.Body != "" {
|
|
||||||
bodySize = len(response.Body)
|
|
||||||
} else {
|
|
||||||
bodySize = len(response.BodyRaw)
|
|
||||||
}
|
|
||||||
if bodySize > 0 {
|
|
||||||
response.Headers = append(response.Headers, Header{Name: "Content-Length", Value: strconv.Itoa(bodySize)})
|
|
||||||
}
|
|
||||||
for _, header := range response.Headers {
|
|
||||||
fmt.Fprintf(conn, "%s: %s%s", header.Name, header.Value, HTTPDelimiter)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprint(conn, HTTPDelimiter)
|
|
||||||
if bodySize > 0 {
|
|
||||||
if response.Body != "" {
|
|
||||||
fmt.Fprint(conn, response.Body)
|
|
||||||
} else {
|
|
||||||
conn.Write(response.BodyRaw)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// fmt.Fprintf(conn, "HTTP/1.1 %d %s%s%s", response.Code, response.Message, HTTPDelimiter, HTTPDelimiter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleConnection(conn net.Conn) {
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
buffer := make([]byte, 1024)
|
buffer := make([]byte, 1024)
|
||||||
n, err := conn.Read(buffer)
|
n, err := conn.Read(buffer)
|
||||||
@ -116,92 +62,136 @@ func handleConnection(conn net.Conn) {
|
|||||||
}
|
}
|
||||||
fmt.Println(request)
|
fmt.Println(request)
|
||||||
|
|
||||||
if request.Path == "/" {
|
for _, route := range routes.stringRoutes {
|
||||||
Respond(conn, Response{Version: request.Version, Code: OK})
|
if request.Path == route.path && request.Method == route.method {
|
||||||
|
Respond(conn, route.handler(request))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if request.Path == "/user-agent" {
|
for _, route := range routes.regexpRoutes {
|
||||||
for _, header := range request.Headers {
|
if request.Method != route.method {
|
||||||
if header.Name != "User-Agent" {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
Respond(conn, Response{
|
if matches := route.regex.FindStringSubmatch(request.Path); len(matches) > 0 {
|
||||||
Version: request.Version,
|
Respond(conn, route.handler(request, matches))
|
||||||
Code: OK,
|
|
||||||
Body: header.Value,
|
|
||||||
Headers: []Header{{Name: "Content-Type", Value: "text/plain"}},
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Respond(conn, Response{Version: request.Version, Code: BadRequest})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reFiles := regexp.MustCompile(`^/files/([A-Za-z0-9_\-.]+)$`)
|
// if request.Path == "/" {
|
||||||
if matches := reFiles.FindStringSubmatch(request.Path); len(matches) > 0 {
|
// Respond(conn, Response{Version: request.Version, Code: OK})
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
if len(os.Args) != 3 {
|
// if request.Path == "/user-agent" {
|
||||||
log.Fatal("Not enough args")
|
// for _, header := range request.Headers {
|
||||||
}
|
// if header.Name != "User-Agent" {
|
||||||
dir, err := filepath.Abs(os.Args[2])
|
// continue
|
||||||
if err != nil {
|
// }
|
||||||
log.Fatal(err)
|
// Respond(conn, Response{
|
||||||
}
|
// Version: request.Version,
|
||||||
filename := filepath.Join(dir, matches[1])
|
// Code: OK,
|
||||||
|
// Body: header.Value,
|
||||||
|
// Headers: []Header{{Name: "Content-Type", Value: "text/plain"}},
|
||||||
|
// })
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// Respond(conn, Response{Version: request.Version, Code: BadRequest})
|
||||||
|
// }
|
||||||
|
|
||||||
// _, err:=os.Stat(filename)
|
// reFiles := regexp.MustCompile(`^/files/([A-Za-z0-9_\-.]+)$`)
|
||||||
// fmt.Println(file)
|
// if matches := reFiles.FindStringSubmatch(request.Path); len(matches) > 0 {
|
||||||
file, err := os.ReadFile(filename)
|
// filename := getFilepath(matches[1])
|
||||||
if os.IsNotExist(err) {
|
// file, err := os.ReadFile(filename)
|
||||||
Respond(conn, Response{Version: request.Version, Code: NotFound})
|
// if os.IsNotExist(err) {
|
||||||
return
|
// Respond(conn, Response{Version: request.Version, Code: NotFound})
|
||||||
}
|
// return
|
||||||
if err != nil {
|
// }
|
||||||
log.Fatal(err)
|
// if err != nil {
|
||||||
}
|
// log.Fatal(err)
|
||||||
|
// }
|
||||||
|
|
||||||
Respond(conn, Response{
|
// Respond(conn, Response{
|
||||||
Version: request.Version,
|
// Version: request.Version,
|
||||||
Code: OK,
|
// Code: OK,
|
||||||
BodyRaw: file,
|
// BodyRaw: file,
|
||||||
Headers: []Header{{Name: "Content-Type", Value: "application/octet-stream"}},
|
// Headers: []Header{{Name: "Content-Type", Value: "application/octet-stream"}},
|
||||||
})
|
// })
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
reEcho := regexp.MustCompile(`^/echo/([A-Za-z]+)$`)
|
// reEcho := regexp.MustCompile(`^/echo/([A-Za-z]+)$`)
|
||||||
if matches := reEcho.FindStringSubmatch(request.Path); len(matches) > 0 {
|
// if matches := reEcho.FindStringSubmatch(request.Path); len(matches) > 0 {
|
||||||
Respond(conn, Response{
|
// Respond(conn, Response{
|
||||||
Version: request.Version,
|
// Version: request.Version,
|
||||||
Code: OK,
|
// Code: OK,
|
||||||
Body: matches[1],
|
// Body: matches[1],
|
||||||
Headers: []Header{{Name: "Content-Type", Value: "text/plain"}},
|
// Headers: []Header{{Name: "Content-Type", Value: "text/plain"}},
|
||||||
})
|
// })
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
Respond(conn, Response{Version: request.Version, Code: NotFound})
|
Respond(conn, Response{Version: request.Version, Code: NotFound})
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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!")
|
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")
|
l, err := net.Listen("tcp", "0.0.0.0:4221")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to bind to port 4221")
|
fmt.Println("Failed to bind to port 4221")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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"}},
|
||||||
|
}
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
conn, err := l.Accept()
|
conn, err := l.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error accepting connection: ", err.Error())
|
fmt.Println("Error accepting connection: ", err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
go handleConnection(conn)
|
go handleConnection(conn, routes)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user