Other filetypes now work, or at least CSS

This commit is contained in:
Hannah-Dagemark 2024-04-25 16:22:27 +02:00
commit d48cfbcf11
4 changed files with 63 additions and 17 deletions

View file

@ -19,7 +19,8 @@ class Router
if match != nil
print("\nMatch: ", match, "\n")
return 200
returner = [match, 200]
return returner
else
if request.resource.match?(@routes[0][:resource]) != true
p "failed to find: #{request.resource} resource.\nExpected: #{String(@routes[0][:resource])} resource"
@ -28,7 +29,8 @@ class Router
else
p "Failed to match, got no reason, just kinda didn't feel like it"
end
return 404
returner = [request, 404]
return returner
end
end

View file

@ -14,7 +14,18 @@ class HTTPServer
router = Router.new
router.add_route("GET",/\/grillkorv\/\d/) do |id, senap|
puts "woot #{senap}"
if File.read("./public/grillkorv.html") != nil
File.read("./public/grillkorv.html")
else
"Html not found"
end
end
router.add_route("GET",/\/grillkorv\/\w.css/) do |id, senap|
if File.read("./public/grillkorv.html") != nil
File.read("./public/grillkorv.html")
else
"Html not found"
end
end
router.add_route("GET","/favicon.ico")
@ -32,25 +43,37 @@ class HTTPServer
pp request
routeReturn = router.match_route(request)
if routeReturn == 200
html = "<h1>Hello, World!</h1>"
status = 200
elsif routeReturn == 404
html = "<h1>Not found!</h1>"
status = 404
outputContent = {}
if routeReturn[1] == 200
if routeReturn[0][:block] != nil
outputContent[:info] = routeReturn[0][:block].call
print("Recieved HTML, enabling as called:\n #{outputContent[:info]}")
outputContent[:type] = "text/html"
status = 200
else
outputContent[:info] = nil
status = 200
end
elsif routeReturn[1] == 404
print("HTML route not found, searching for target file...\n")
if File.file?("./public/#{routeReturn[0].resource}")
outputContent[:info] = File.read("./public#{routeReturn[0].resource}")
print("Successfully opened file \"./public#{routeReturn[0].resource}\" with contents:\n#{outputContent[:info]}\n ")
outputContent[:type] = "text/css"
status = 200
else
print("Could not find file: \"./public/#{routeReturn[0].resource}\"\n")
status = 404
end
end
#Sen kolla om resursen (filen finns)
# Nedanstående bör göras i er Response-klass
session.print "HTTP/1.1 #{status}\r\n"
session.print "Content-Type: text/html\r\n"
session.print "Content-Type: #{outputContent[:type]}\r\n"
print("Reading content type: #{outputContent[:type]}\n")
session.print "\r\n"
session.print html
if outputContent[:info]; session.print outputContent[:info] end
session.close
end
end

4
public/css/style.css Normal file
View file

@ -0,0 +1,4 @@
li {
color: green;
background-color: blueviolet;
}

17
public/grillkorv.html Normal file
View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/css/style.css">
<title>HOTDAWGGGG</title>
</head>
<body>
<ol>
<li>This is the best</li>
<li><h1>WEBSITE YOU HAVE EVER SEEEN</h1></li>
<ol><h3>Cuz we got hotdogs :D</h3></ol>
</ol>
</body>
</html>