Skip to content

Commit

Permalink
feat: try full text search
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Hivert <[email protected]>
  • Loading branch information
ghivert committed May 6, 2024
1 parent 7209ed0 commit 537a6be
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
54 changes: 52 additions & 2 deletions apps/backend/src/backend/router.gleam
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import api/hex
import backend/config.{type Config, type Context}
import backend/error
import backend/postgres/postgres
import backend/web
import cors_builder as cors
import gleam/dynamic
import gleam/function
import gleam/http
import gleam/io
import gleam/json
import gleam/list
import gleam/pgo
import gleam/result
import gleam/string_builder
import tasks/hex as syncing
Expand All @@ -19,7 +25,52 @@ fn empty_json() {

pub fn handle_get(req: Request, ctx: Context) {
case wisp.path_segments(req) {
[] -> empty_json()
["search"] -> {
wisp.get_query(req)
|> list.find(fn(item) { item.0 == "q" })
|> result.replace_error(error.EmptyError)
|> result.try(fn(item) {
let query = pgo.text("'" <> item.1 <> "'")
"SELECT
s.name,
s.json_signature,
m.name,
p.name
FROM package_type_fun_signature s
JOIN package_module m
ON m.id = s.package_module_id
JOIN package_release r
ON r.id = m.package_release_id
JOIN package p
ON p.id = r.package_id
WHERE to_tsvector(s.signature_) @@ to_tsquery($1)"
|> pgo.execute(
ctx.db,
[query],
dynamic.decode4(
fn(a, b, c, d) {
json.object([
#("name", json.string(a)),
#("json_signature", dynamic.unsafe_coerce(b)),
#("module_name", json.string(c)),
#("package_name", json.string(d)),
])
},
dynamic.element(0, dynamic.string),
dynamic.element(1, dynamic.dynamic),
dynamic.element(2, dynamic.string),
dynamic.element(3, dynamic.string),
),
)
|> result.map_error(error.DatabaseError)
})
|> result.map(fn(r) { r.rows })
|> result.unwrap([])
|> function.tap(fn(a) { io.debug(list.length(a)) })
|> json.preprocessed_array()
|> json.to_string_builder()
|> wisp.json_response(200)
}
_ -> wisp.not_found()
}
}
Expand All @@ -40,7 +91,6 @@ pub fn handle_post(req: Request, ctx: Context) {
pub fn handle_request(req: Request, cnf: Config) -> Response {
use req <- cors.wisp_handle(req, web.cors())
use req <- web.foundations(req)
use req <- web.reroute_non_json_request(req)
use ctx <- postgres.middleware(cnf)
case req.method {
http.Get -> handle_get(req, ctx)
Expand Down
9 changes: 0 additions & 9 deletions apps/backend/src/backend/web.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import backend/config
import backend/request
import cors_builder as cors
import gleam/http
import wisp.{type Request, type Response}
Expand All @@ -14,14 +13,6 @@ pub fn foundations(req: Request, handler: Handler) -> Response {
handler(req)
}

pub fn reroute_non_json_request(req: Request, handler: Handler) -> Response {
case req.method, request.is_json_request(req) {
http.Get, True -> handler(req)
http.Get, False -> wisp.ok()
_, _ -> handler(req)
}
}

pub fn cors() {
let origin = case config.is_dev() {
True -> "http://localhost:5173"
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<link rel="stylesheet" href="/main.css" />
<link rel="stylesheet" href="/normalize.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>gloogle</title>
<title>Gloogle</title>
</head>
<body>
<div id="app"></div>
Expand Down
3 changes: 3 additions & 0 deletions apps/frontend/src/data/msg.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import lustre_http as http

pub type Msg {
None
SubmitSearch
SearchResults(Result(String, http.HttpError))
UpdateInput(String)
}
18 changes: 17 additions & 1 deletion apps/frontend/src/frontend.gleam
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import data/model.{type Model}
import data/msg.{type Msg}
import frontend/view
import gleam/dynamic
import gleam/io
import lustre
import lustre/effect
import lustre/update
import lustre_http as http
import sketch/lustre as sketch
import sketch/options as sketch_options
import tardis
Expand Down Expand Up @@ -32,7 +35,20 @@ fn update(model: Model, msg: Msg) {
model
|> model.update_input(content)
|> update.none()
msg.SubmitSearch -> update.none(model)
msg.SubmitSearch -> {
let input = model.input
#(
model,
http.get(
"http://localhost:3000/search?q=" <> input,
http.expect_text(msg.SearchResults),
),
)
}
msg.SearchResults(res) -> {
io.debug(res)
update.none(model)
}
msg.None -> update.none(model)
}
}

0 comments on commit 537a6be

Please sign in to comment.