瀏覽代碼

Routing (#6920)

* Dockerfile test

* fix run.sh

* Working Docker build and plaintext/json tests implemented and passing..

* julia_server.jl patch

* Update julia_server.jl

Added test for Single Database Query

* Update benchmark_config.json

Updated to reflect addition of SDQ test

* Update julia_server.jl

Now sends JSON object instead of string. Packages have also been fixed. Single Query Test is now passing!!!!

* Testing multiple db queries test

* Testing multiple db queries test

* multiple db queries response formatting changes

* benchmark_config updates

* More multiple queries formatting

* Exception handling for multiple queries

* More handling...

* More handling

* Even more exception handling.....

* Exception handling for multiple queries test

* Exception handling for 0

* Condensing exception handling for multi db queries

* Exception handling fixes...

* exception changes

* Rand num changes

* Rand num changes

* Array changes

* Added StructTypes

* struct fix

* test

* revert test

* Fix reduncancy/formatting code...

* Fixed response headers

* Update README.md

* Update README.md

* Create README.md

* Update julia_server.jl

Added Fortunes test

* Update benchmark_config.json

added fortunes test

* Update julia_server.jl

changed header type

* Update README.md

Added Fortunes test "checkmark"

* Added solution for db updates test

* Update julia_server.jl

Closed DB connections

* Update README.md

added updates

* Update julia_server.jl

ROUTING WORKS!!!!!!!

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* Update julia_server.jl

* addressed usage of json string

Co-authored-by: donavindebartolo <[email protected]>
Co-authored-by: donavindebartolo <[email protected]>
Co-authored-by: Jayenn <[email protected]>
Co-authored-by: KadeBerry <[email protected]>
Austin Brown 3 年之前
父節點
當前提交
053452e6d0
共有 1 個文件被更改,包括 65 次插入51 次删除
  1. 65 51
      frameworks/Julia/Jewelia/julia_server.jl

+ 65 - 51
frameworks/Julia/Jewelia/julia_server.jl

@@ -7,32 +7,39 @@ using MySQL
 using JSON3
 using JSON3
 using StructTypes
 using StructTypes
 
 
+struct jsonMsgObj
+    message
+end
+
 struct jsonObj
 struct jsonObj
-    id::Int
-    randomNumber::Int
+    id
+    randomNumber
 end
 end
 
 
+StructTypes.StructType(::Type{jsonMsgObj}) = StructTypes.Struct()
 StructTypes.StructType(::Type{jsonObj}) = StructTypes.Struct()
 StructTypes.StructType(::Type{jsonObj}) = StructTypes.Struct()
 
 
-HTTP.listen("0.0.0.0" , 8080, reuseaddr = true) do http
-    target = http.message.target
-
-    HTTP.setstatus(http, 200)
-    HTTP.setheader(http, "Server" => "Julia-HTTP")
-    HTTP.setheader(http, "Date" => Dates.format(Dates.now(), Dates.RFC1123Format) * " GMT")
-
-    if endswith(target, "/plaintext")
-        HTTP.setheader(http, "Content-Type" => "text/plain")
-        HTTP.startwrite(http)
-        write(http, "Hello, World!")
-
-    elseif endswith(target, "/json")
-        HTTP.setheader(http, "Content-Type" => "application/json")
-        startwrite(http)
-        JSON3.write(http, (;message = "Hello, World!"))
+    function plaintext(req::HTTP.Request)
+        headers = [ "Content-Type" => "text/plain", 
+                    "Server" => "Julia-HTTP",
+                    "Date" => Dates.format(Dates.now(), Dates.RFC1123Format) * " GMT" ]
+    
+        return HTTP.Response(200, headers, body = "Hello, World!")
+    end
+    
+    function jsonSerialization(req::HTTP.Request)
+        headers = [ "Content-Type" => "application/json",
+                    "Server" => "Julia-HTTP",
+                    "Date" => Dates.format(Dates.now(), Dates.RFC1123Format) * " GMT" ]
 
 
-    elseif endswith(target, "/db")
-        HTTP.setheader(http, "Content-Type" => "application/json")
+        return HTTP.Response(200, headers, body = JSON3.write(jsonMsgObj("Hello, World!")))
+    end
+        
+    function singleQuery(req::HTTP.Request)
+        headers = [ "Content-Type" => "application/json",
+                    "Server" => "Julia-HTTP",
+                    "Date" => Dates.format(Dates.now(), Dates.RFC1123Format) * " GMT" ]
+    
         randNum = rand(1:10000)
         randNum = rand(1:10000)
 
 
         conn = DBInterface.connect(MySQL.Connection, "tfb-database", "benchmarkdbuser", "benchmarkdbpass", db="hello_world")
         conn = DBInterface.connect(MySQL.Connection, "tfb-database", "benchmarkdbuser", "benchmarkdbpass", db="hello_world")
@@ -40,19 +47,20 @@ HTTP.listen("0.0.0.0" , 8080, reuseaddr = true) do http
         results = DBInterface.execute(conn, sqlQuery)
         results = DBInterface.execute(conn, sqlQuery)
         row = first(results)
         row = first(results)
         dbNumber = row[2]
         dbNumber = row[2]
-        jsonString = "{\"id\":$randNum,\"randomNumber\":$dbNumber}"
-
-        startwrite(http)
-        JSON3.write(http, (JSON3.read(jsonString)))
         
         
         DBInterface.close!(conn)
         DBInterface.close!(conn)
+        return HTTP.Response(200, headers, body = JSON3.write(jsonObj(randNum, dbNumber)))
+    end
+        
+    function multipleQueries(req::HTTP.Request)
+        headers = [ "Content-Type" => "application/json",
+                    "Server" => "Julia-HTTP",
+                    "Date" => Dates.format(Dates.now(), Dates.RFC1123Format) * " GMT" ]
         
         
-    elseif occursin("/queries", target)
-        HTTP.setheader(http, "Content-Type" => "application/json")
         numQueries = -1
         numQueries = -1
 
 
         try
         try
-            numQueries = parse(Int64, (split(target, "="))[2])
+            numQueries = parse(Int64, (split(req.target, "="))[2])
 
 
         catch ArgumentError
         catch ArgumentError
             numQueries = 1
             numQueries = 1
@@ -77,20 +85,23 @@ HTTP.listen("0.0.0.0" , 8080, reuseaddr = true) do http
             results = DBInterface.execute(conn, sqlQuery)
             results = DBInterface.execute(conn, sqlQuery)
             row = first(results)
             row = first(results)
             dbNumber = row[2]
             dbNumber = row[2]
-            responseArray[i] = JSON3.read("{\"id\":$randNum,\"randomNumber\":$dbNumber}", jsonObj)
+            responseArray[i] = jsonObj(randNum, dbNumber)
         end
         end
-
-        startwrite(http)
-        JSON3.write(http, responseArray)
         
         
         DBInterface.close!(conn)
         DBInterface.close!(conn)
-    
-    elseif occursin("/updates", target)
-        HTTP.setheader(http, "Content-Type" => "application/json")
+        
+        return HTTP.Response(200, headers, body = JSON3.write(responseArray))
+    end
+        
+    function updates(req::HTTP.Request)
+        headers = [ "Content-Type" => "application/json",
+                    "Server" => "Julia-HTTP",
+                    "Date" => Dates.format(Dates.now(), Dates.RFC1123Format) * " GMT" ]
+            
         numQueries = -1
         numQueries = -1
 
 
         try
         try
-            numQueries = parse(Int64, (split(target, "="))[2])
+            numQueries = parse(Int64, (split(req.target, "="))[2])
 
 
         catch ArgumentError
         catch ArgumentError
             numQueries = 1
             numQueries = 1
@@ -118,16 +129,17 @@ HTTP.listen("0.0.0.0" , 8080, reuseaddr = true) do http
 
 
             sqlQuery = "UPDATE World SET randomnumber = $randNum WHERE id = $randId"
             sqlQuery = "UPDATE World SET randomnumber = $randNum WHERE id = $randId"
             results = DBInterface.execute(conn, sqlQuery)
             results = DBInterface.execute(conn, sqlQuery)
-            responseArray[i] = JSON3.read("{\"id\":$randId,\"randomNumber\":$randNum}", jsonObj)
+            responseArray[i] = jsonObj(randId, randNum)
         end
         end
-
-        startwrite(http)
-        JSON3.write(http, responseArray)
         
         
         DBInterface.close!(conn)
         DBInterface.close!(conn)
-
-    elseif endswith(target, "/fortunes")
-        HTTP.setheader(http, "Content-Type" => "text/html; charset=utf-8")
+        return HTTP.Response(200, headers, body = JSON3.write(responseArray))
+    end
+        
+    function fortunes(req::HTTP.Request)
+        headers = [ "Content-Type" => "text/html; charset=utf-8",
+                    "Server" => "Julia-HTTP",
+                    "Date" => Dates.format(Dates.now(), Dates.RFC1123Format) * " GMT" ]
     
     
         fortunesList = []
         fortunesList = []
         sqlQuery = "SELECT * FROM fortune"
         sqlQuery = "SELECT * FROM fortune"
@@ -150,15 +162,17 @@ HTTP.listen("0.0.0.0" , 8080, reuseaddr = true) do http
         end
         end
     
     
         output = string(output, "</table></body></html>")
         output = string(output, "</table></body></html>")
-    
-        write(http, output)
         
         
         DBInterface.close!(conn)
         DBInterface.close!(conn)
-        
-    else
-        HTTP.setstatus(http, 404)
-        startwrite(http)
-        write(http, "Not Found")
-
+        return HTTP.Response(200, headers, body = output)
     end
     end
-end
+            
+const ROUTER = HTTP.Router()
+HTTP.@register(ROUTER, "GET", "/plaintext", plaintext)
+HTTP.@register(ROUTER, "GET", "/json", jsonSerialization)
+HTTP.@register(ROUTER, "GET", "/db", singleQuery)
+HTTP.@register(ROUTER, "GET", "/queries", multipleQueries)
+HTTP.@register(ROUTER, "GET", "/updates", updates)
+HTTP.@register(ROUTER, "GET", "/fortunes", fortunes)
+
+HTTP.serve(ROUTER, "0.0.0.0" , 8080, reuseaddr=true)