Skip to content

Commit

Permalink
refactored server
Browse files Browse the repository at this point in the history
  • Loading branch information
cecoeco committed Apr 7, 2024
1 parent 0a5e211 commit da380ef
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 71 deletions.
138 changes: 69 additions & 69 deletions app/backend/src/app.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ using Oxygen
using HTTP
using Readability

const ALLOWED_ORIGINS::Vector{Pair{String, String}} = [
const ALLOWED_ORIGINS::Vector{Pair{String,String}} = [
"Access-Control-Allow-Origin" => "*"
]

const CORS_HEADERS::Vector{Pair{String,String}} = [
ALLOWED_ORIGINS...,
"Access-Control-Allow-Headers" => "*",
"Access-Control-Allow-Methods" => "GET, POST, OPTIONS",
"Access-Control-Allow-Methods" => "GET, POST, OPTIONS"
]

function CorsHandler(handle)
function CORS_handler(handle::Function)
return function (req::HTTP.Request)
if HTTP.method(req) == "OPTIONS"
return HTTP.Response(200, CORS_HEADERS)
Expand All @@ -24,74 +24,74 @@ function CorsHandler(handle)
end
end

Oxygen.post("/ari") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.ARI(text)
end

Oxygen.post("/average_reading_time") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.reading_time(text)
end

Oxygen.post("/average_speaking_time") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.speaking_time(text)
end

Oxygen.post("/characters") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.characters(text)
end

Oxygen.post("/coleman-liau") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.ColemanLiau(text)
end

Oxygen.post("/dale-chall") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.DaleChall(text)
end

Oxygen.post("/fres") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.FleschReadingEase(text)
end

Oxygen.post("/fkgl") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.FleschKincaidGradeLevel(text)
end

Oxygen.post("/gunning_fog") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.GunningFog(text)
end

Oxygen.post("/sentences") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.sentences(text)
end

Oxygen.post("/smog") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.SMOG(text)
end

Oxygen.post("/spache") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.Spache(text)
function post_request_handler(endpoint::String, readability_function::Function)
Oxygen.post(endpoint) do req::HTTP.Request
text::String = Base.String(req.body)
return readability_function(text)
end
end

Oxygen.post("/syllables") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.syllables(text)
end
readability_endpoints::Vector{Tuple{String,Function}} = [
(
"/ari",
Readability.ARI
),
(
"/average_reading_time",
Readability.reading_time
),
(
"/average_speaking_time",
Readability.speaking_time
),
(
"/characters",
Readability.characters
),
(
"/coleman-liau",
Readability.ColemanLiau
),
(
"/dale-chall",
Readability.DaleChall
),
(
"/fkgl",
Readability.FleschKincaidGradeLevel
),
(
"/fres",
Readability.FleschReadingEase
),
(
"/gunning_fog",
Readability.GunningFog
),
(
"/sentences",
Readability.sentences
),
(
"/smog",
Readability.SMOG
),
(
"/spache",
Readability.Spache
),
(
"/syllables",
Readability.syllables
),
(
"/words",
Readability.words
)
]

Oxygen.post("/words") do req::HTTP.Request
text::String = Base.String(req.body)
return Readability.words(text)
for (endpoint, readability_function) in readability_endpoints
post_request_handler(endpoint, readability_function)
end

Oxygen.serve(port=5050, middleware=[CorsHandler])
Oxygen.serve(port=5050, middleware=[CORS_handler])
4 changes: 2 additions & 2 deletions app/frontend/src/pages/metrics.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ function Metrics() {

async function postData(metricType, route) {
const text = document.querySelector("textarea").value;
const endpoint = `http://127.0.0.1:5050/${route}`;
const endpoint = `https://127.0.0.1:5050/${route}`;
try {
const response = await fetch(endpoint, {
method: "POST",
headers: { "Content-Type": "text/plain", },
headers: { "Content-Type": "text/plain" },
body: text,
});

Expand Down

0 comments on commit da380ef

Please sign in to comment.