update gleam & deps

This commit is contained in:
stuebinm 2023-10-30 11:49:03 +01:00
parent b231e47108
commit 194bccdc10
2 changed files with 151 additions and 116 deletions

View file

@ -2,32 +2,32 @@
# You typically do not need to edit this file # You typically do not need to edit this file
packages = [ packages = [
{ name = "certifi", version = "2.9.0", build_tools = ["rebar3"], requirements = [], otp_app = "certifi", source = "hex", outer_checksum = "266DA46BDB06D6C6D35FDE799BCB28D36D985D424AD7C08B5BB48F5B5CDD4641" }, { name = "certifi", version = "2.12.0", build_tools = ["rebar3"], requirements = [], otp_app = "certifi", source = "hex", outer_checksum = "EE68D85DF22E554040CDB4BE100F33873AC6051387BAF6A8F6CE82272340FF1C" },
{ name = "decimal", version = "2.1.1", build_tools = ["mix"], requirements = [], otp_app = "decimal", source = "hex", outer_checksum = "53CFE5F497ED0E7771AE1A475575603D77425099BA5FAEF9394932B35020FFCC" }, { name = "decimal", version = "2.1.1", build_tools = ["mix"], requirements = [], otp_app = "decimal", source = "hex", outer_checksum = "53CFE5F497ED0E7771AE1A475575603D77425099BA5FAEF9394932B35020FFCC" },
{ name = "gleam_erlang", version = "0.18.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "C69F59D086AD50B80DE294FB0963550630971C9DC04E92B1F7AEEDD2C0BE226C" }, { name = "gleam_erlang", version = "0.22.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "367D8B41A7A86809928ED1E7E55BFD0D46D7C4CF473440190F324AFA347109B4" },
{ name = "gleam_hackney", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_stdlib", "hackney"], otp_app = "gleam_hackney", source = "hex", outer_checksum = "B3C1E6BD138D57252F9F9E499C741E9227EE7EE9B017CA650EC8193E02F734E1" }, { name = "gleam_hackney", version = "1.1.0", build_tools = ["gleam"], requirements = ["hackney", "gleam_http", "gleam_stdlib"], otp_app = "gleam_hackney", source = "hex", outer_checksum = "CA69AD9061C4A8775A7BD445DE33ECEFD87379AF8E5B028F3DD0216BECA5DD0B" },
{ name = "gleam_http", version = "3.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "D034F5CE0639CD142CBA210B7D5D14236C284B0C5772A043D2E22128594573AE" }, { name = "gleam_http", version = "3.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "FAE9AE3EB1CA90C2194615D20FFFD1E28B630E84DACA670B28D959B37BCBB02C" },
{ name = "gleam_otp", version = "0.5.3", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "6E705B69464237353E0380AC8143BDB29A3F0BF6168755D5F2D6E55A34A8B077" }, { name = "gleam_otp", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "ED7381E90636E18F5697FD7956EECCA635A3B65538DC2BE2D91A38E61DCE8903" },
{ name = "gleam_stdlib", version = "0.28.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "73F0A89FADE5022CBEF6D6C3551F9ADCE7054AFCE0CB1DC4C6D5AB4CA62D0111" }, { name = "gleam_stdlib", version = "0.31.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "6D1BC5B4D4179B9FEE866B1E69FE180AC2CE485AD90047C0B32B2CA984052736" },
{ name = "gleeunit", version = "0.10.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "ECEA2DE4BE6528D36AFE74F42A21CDF99966EC36D7F25DEB34D47DD0F7977BAF" }, { name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" },
{ name = "glisten", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "52B530FF25370590843998D1B6C4EC6169DB1300D5E4407A5CDA1575374B7AEC" }, { name = "glisten", version = "0.9.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_otp", "gleam_erlang"], otp_app = "glisten", source = "hex", outer_checksum = "91809C44C52456D96C8317A19246DE1C06ED494C40D282CD9380565E879A52C4" },
{ name = "hackney", version = "1.18.1", build_tools = ["rebar3"], requirements = ["idna", "metrics", "parse_trans", "ssl_verify_fun", "mimerl", "certifi", "unicode_util_compat"], otp_app = "hackney", source = "hex", outer_checksum = "A4ECDAFF44297E9B5894AE499E9A070EA1888C84AFDD1FD9B7B2BC384950128E" }, { name = "hackney", version = "1.20.1", build_tools = ["rebar3"], requirements = ["certifi", "mimerl", "metrics", "parse_trans", "ssl_verify_fun", "unicode_util_compat", "idna"], otp_app = "hackney", source = "hex", outer_checksum = "FE9094E5F1A2A2C0A7D10918FEE36BFEC0EC2A979994CFF8CFE8058CD9AF38E3" },
{ name = "haystack", version = "0.1.0", build_tools = ["mix"], requirements = ["jason", "stemmer"], otp_app = "haystack", source = "hex", outer_checksum = "27A582513EF933C1B11345B96F8D41EE137D03B25312BD85068FFE8FEC503635" }, { name = "haystack", version = "0.1.0", build_tools = ["mix"], requirements = ["jason", "stemmer"], otp_app = "haystack", source = "hex", outer_checksum = "27A582513EF933C1B11345B96F8D41EE137D03B25312BD85068FFE8FEC503635" },
{ name = "idna", version = "6.1.1", build_tools = ["rebar3"], requirements = ["unicode_util_compat"], otp_app = "idna", source = "hex", outer_checksum = "92376EB7894412ED19AC475E4A86F7B413C1B9FBB5BD16DCCD57934157944CEA" }, { name = "idna", version = "6.1.1", build_tools = ["rebar3"], requirements = ["unicode_util_compat"], otp_app = "idna", source = "hex", outer_checksum = "92376EB7894412ED19AC475E4A86F7B413C1B9FBB5BD16DCCD57934157944CEA" },
{ name = "jason", version = "1.4.0", build_tools = ["mix"], requirements = ["decimal"], otp_app = "jason", source = "hex", outer_checksum = "79A3791085B2A0F743CA04CEC0F7BE26443738779D09302E01318F97BDB82121" }, { name = "jason", version = "1.4.1", build_tools = ["mix"], requirements = ["decimal"], otp_app = "jason", source = "hex", outer_checksum = "FBB01ECDFD565B56261302F7E1FCC27C4FB8F32D56EAB74DB621FC154604A7A1" },
{ name = "metrics", version = "1.0.1", build_tools = ["rebar3"], requirements = [], otp_app = "metrics", source = "hex", outer_checksum = "69B09ADDDC4F74A40716AE54D140F93BEB0FB8978D8636EADED0C31B6F099F16" }, { name = "metrics", version = "1.0.1", build_tools = ["rebar3"], requirements = [], otp_app = "metrics", source = "hex", outer_checksum = "69B09ADDDC4F74A40716AE54D140F93BEB0FB8978D8636EADED0C31B6F099F16" },
{ name = "mimerl", version = "1.2.0", build_tools = ["rebar3"], requirements = [], otp_app = "mimerl", source = "hex", outer_checksum = "F278585650AA581986264638EBF698F8BB19DF297F66AD91B18910DFC6E19323" }, { name = "mimerl", version = "1.2.0", build_tools = ["rebar3"], requirements = [], otp_app = "mimerl", source = "hex", outer_checksum = "F278585650AA581986264638EBF698F8BB19DF297F66AD91B18910DFC6E19323" },
{ name = "mist", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_otp", "glisten", "gleam_http", "gleam_erlang"], otp_app = "mist", source = "hex", outer_checksum = "5AFBABABF738BAB8720F047471051E4E9D102CA4694C120DB899FA12AD5D180B" }, { name = "mist", version = "0.14.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glisten", "gleam_http", "gleam_erlang", "gleam_otp"], otp_app = "mist", source = "hex", outer_checksum = "7CDD0396D9A556F1069D83E9AF2B24388AAC478B9B4846615C6D4797E1D3C6A3" },
{ name = "parse_trans", version = "3.3.1", build_tools = ["rebar3"], requirements = [], otp_app = "parse_trans", source = "hex", outer_checksum = "07CD9577885F56362D414E8C4C4E6BDF10D43A8767ABB92D24CBE8B24C54888B" }, { name = "parse_trans", version = "3.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "parse_trans", source = "hex", outer_checksum = "620A406CE75DADA827B82E453C19CF06776BE266F5A67CFF34E1EF2CBB60E49A" },
{ name = "ssl_verify_fun", version = "1.1.6", build_tools = ["mix", "rebar3", "make"], requirements = [], otp_app = "ssl_verify_fun", source = "hex", outer_checksum = "BDB0D2471F453C88FF3908E7686F86F9BE327D065CC1EC16FA4540197EA04680" }, { name = "ssl_verify_fun", version = "1.1.7", build_tools = ["mix", "rebar3", "make"], requirements = [], otp_app = "ssl_verify_fun", source = "hex", outer_checksum = "FE4C190E8F37401D30167C8C405EDA19469F34577987C76DDE613E838BBC67F8" },
{ name = "stemmer", version = "1.1.0", build_tools = ["mix"], requirements = [], otp_app = "stemmer", source = "hex", outer_checksum = "0CB5FAF73476B84500E371FF39FD9A494F60AB31D991689C1CD53B920556228F" }, { name = "stemmer", version = "1.1.0", build_tools = ["mix"], requirements = [], otp_app = "stemmer", source = "hex", outer_checksum = "0CB5FAF73476B84500E371FF39FD9A494F60AB31D991689C1CD53B920556228F" },
{ name = "unicode_util_compat", version = "0.7.0", build_tools = ["rebar3"], requirements = [], otp_app = "unicode_util_compat", source = "hex", outer_checksum = "25EEE6D67DF61960CF6A794239566599B09E17E668D3700247BC498638152521" }, { name = "unicode_util_compat", version = "0.7.0", build_tools = ["rebar3"], requirements = [], otp_app = "unicode_util_compat", source = "hex", outer_checksum = "25EEE6D67DF61960CF6A794239566599B09E17E668D3700247BC498638152521" },
] ]
[requirements] [requirements]
gleam_hackney = "~> 1.0" gleam_hackney = { version = "~> 1.0" }
gleam_http = "~> 3.0" gleam_http = { version = "~> 3.0" }
gleam_stdlib = "~> 0.19" gleam_stdlib = { version = "~> 0.19" }
gleeunit = "~> 0.6" gleeunit = { version = "~> 0.6" }
haystack = "~> 0.1" haystack = { version = "~> 0.1" }
mist = "~> 0.4" mist = { version = "~> 0.4" }

View file

@ -1,7 +1,7 @@
import gleam/http/response.{Response} import gleam/http/response.{Response}
import gleam/http/request.{Request,get_header} import gleam/http/request.{Request, get_header}
import gleam/http.{Get} import gleam/http.{Get}
import gleam/bit_builder.{BitBuilder} import gleam/bit_builder
import gleam/erlang/process import gleam/erlang/process
import gleam/erlang/atom import gleam/erlang/atom
import gleam/erlang/file import gleam/erlang/file
@ -18,53 +18,69 @@ import gleam/result
import mist import mist
const ds100_domain = "ds100.bahnhof.name" const ds100_domain = "ds100.bahnhof.name"
const ril100_domain = "ril100.bahnhof.name" const ril100_domain = "ril100.bahnhof.name"
const leitpunkt_domain = "leitpunkt.bahnhof.name" const leitpunkt_domain = "leitpunkt.bahnhof.name"
const domain = "bahnhof.name" const domain = "bahnhof.name"
const proto = "https://" const proto = "https://"
external type Index type Index
external type Field
external fn index_new(atom.Atom) -> Index = type Field
"Elixir.Haystack.Index" "new"
external fn index_ref(Index, Field) -> Index = @external(erlang, "Elixir.Haystack.Index", "new")
"Elixir.Haystack.Index" "ref" fn index_new(a: atom.Atom) -> Index
external fn index_field(Index, Field) -> Index = @external(erlang, "Elixir.Haystack.Index", "ref")
"Elixir.Haystack.Index" "field" fn index_ref(a: Index, b: Field) -> Index
external fn field_term(String) -> Field = @external(erlang, "Elixir.Haystack.Index", "field")
"Elixir.Haystack.Index.Field" "term" fn index_field(a: Index, b: Field) -> Index
external fn field_new(String) -> Field = @external(erlang, "Elixir.Haystack.Index.Field", "term")
"Elixir.Haystack.Index.Field" "new" fn field_term(a: String) -> Field
external fn index_add(Index, List(a)) -> Index = @external(erlang, "Elixir.Haystack.Index.Field", "new")
"Elixir.Haystack.Index" "add" fn field_new(a: String) -> Field
pub external fn inspect(a) -> a = @external(erlang, "Elixir.Haystack.Index", "add")
"Elixir.IO" "inspect" fn index_add(a: Index, b: List(a)) -> Index
external type Query @external(erlang, "Elixir.IO", "inspect")
external type Clause pub fn inspect(a: a) -> a
external type Expression
external fn query_new() -> Query =
"Elixir.Haystack.Query" "new"
external fn query_clause(Query, Clause) -> Query =
"Elixir.Haystack.Query" "clause"
external fn query_run(Query, Index) -> List(Map(atom.Atom, String)) =
"Elixir.Haystack.Query" "run"
external fn clause_new(atom.Atom) -> Clause =
"Elixir.Haystack.Query.Clause" "new"
external fn query_expressions(Clause, List(Expression)) -> Clause =
"Elixir.Haystack.Query.Clause" "expressions"
external fn query_expression_new(atom.Atom, List(#(atom.Atom, String))) -> Expression =
"Elixir.Haystack.Query.Expression" "new"
external fn tokenize(String) -> List(Map(atom.Atom, String)) = type Query
"Elixir.Haystack.Tokenizer" "tokenize"
type Clause
type Expression
@external(erlang, "Elixir.Haystack.Query", "new")
fn query_new() -> Query
@external(erlang, "Elixir.Haystack.Query", "clause")
fn query_clause(a: Query, b: Clause) -> Query
@external(erlang, "Elixir.Haystack.Query", "run")
fn query_run(a: Query, b: Index) -> List(Map(atom.Atom, String))
@external(erlang, "Elixir.Haystack.Query.Clause", "new")
fn clause_new(a: atom.Atom) -> Clause
@external(erlang, "Elixir.Haystack.Query.Clause", "expressions")
fn query_expressions(a: Clause, b: List(Expression)) -> Clause
@external(erlang, "Elixir.Haystack.Query.Expression", "new")
fn query_expression_new(
a: atom.Atom,
b: List(#(atom.Atom, String)),
) -> Expression
@external(erlang, "Elixir.Haystack.Tokenizer", "tokenize")
fn tokenize(a: String) -> List(Map(atom.Atom, String))
type IdKind { type IdKind {
DS100 DS100
@ -100,7 +116,7 @@ fn unpercent(encoded: String) -> String {
|> list.prepend(bit_string.from_string(head)) |> list.prepend(bit_string.from_string(head))
|> bit_string.concat |> bit_string.concat
|> bit_string.to_string |> bit_string.to_string
|> result.map(fn (str) { string.replace(str, "_", " ") }) |> result.map(fn(str) { string.replace(str, "_", " ") })
res res
} }
@ -116,7 +132,7 @@ fn lookup_exact(query: String, lookup: Map(String, String)) -> #(Int, String) {
fn lookup_fuzzy( fn lookup_fuzzy(
query: String, query: String,
kind: IdKind, kind: IdKind,
fuzzy: fn(String, IdKind) -> Matched(String) fuzzy: fn(String, IdKind) -> Matched(String),
) -> #(Int, String) { ) -> #(Int, String) {
case fuzzy(query, kind) { case fuzzy(query, kind) {
Exact(res) -> #(200, res) Exact(res) -> #(200, res)
@ -125,7 +141,7 @@ fn lookup_fuzzy(
} }
} }
fn if_not(res: #(Int,t), fallback: fn() -> #(Int,t)) -> #(Int,t) { fn if_not(res: #(Int, t), fallback: fn() -> #(Int, t)) -> #(Int, t) {
inspect(case res { inspect(case res {
#(200, _) -> res #(200, _) -> res
_ -> fallback() _ -> fallback()
@ -136,43 +152,43 @@ fn lookup_station(
request: Request(t), request: Request(t),
ds100_to_name: Map(String, String), ds100_to_name: Map(String, String),
leitpunkt_to_name: Map(String, String), leitpunkt_to_name: Map(String, String),
fuzzy: fn (String, IdKind) -> Matched(String) fuzzy: fn(String, IdKind) -> Matched(String),
) -> Response(BitBuilder) { ) -> Response(mist.ResponseData) {
let #(code, text) = case request { let #(code, text) = case request {
// blackhole favicon.ico requests instead of using the index // blackhole favicon.ico requests instead of using the index
Request(method: Get, path: "/favicon.ico", ..) -> #(404, "") Request(method: Get, path: "/favicon.ico", ..) -> #(404, "")
Request(method: Get, path: "/help", ..) Request(method: Get, path: "/help", ..)
| Request(method: Get, path: "/", ..) -> #( | Request(method: Get, path: "/", ..) -> #(
200, 200,
"ril100 → Name: " <> proto<>ril100_domain<>"/HG\n" <> "ril100 → Name: " <> proto <> ril100_domain <> "/HG\n" <> "Name → ril100: " <> proto <> ril100_domain <> "/Göttingen\n\n" <> "Leitpunkt → Name: " <> proto <> leitpunkt_domain <> "/GOE\n" <> "Name → Leitpunkt: " <> proto <> leitpunkt_domain <> "/Göttingen\n\n" <> "Fuzzy:" <> proto <> domain <> "/...",
"Name → ril100: " <> proto<>ril100_domain <> "/Göttingen\n\n" <>
"Leitpunkt → Name: " <> proto<>leitpunkt_domain<>"/GOE\n" <>
"Name → Leitpunkt: " <> proto<>leitpunkt_domain <> "/Göttingen\n\n"<>
"Fuzzy:" <> proto<>domain<>"/...",
) )
Request(method: Get, path: "/" <> path, ..) -> { Request(method: Get, path: "/" <> path, ..) -> {
let query = unpercent(path) let query = unpercent(path)
case get_header(request, "x-forwarded-host") { case get_header(request, "x-forwarded-host") {
Ok(domain) if domain == leitpunkt_domain -> query Ok(domain) if domain == leitpunkt_domain ->
query
|> lookup_exact(leitpunkt_to_name) |> lookup_exact(leitpunkt_to_name)
|> if_not(fn() {lookup_fuzzy(query,Leitpunkt,fuzzy)}) |> if_not(fn() { lookup_fuzzy(query, Leitpunkt, fuzzy) })
Ok(domain) if domain == ril100_domain || domain == ds100_domain -> query Ok(domain) if domain == ril100_domain || domain == ds100_domain ->
query
|> lookup_exact(ds100_to_name) |> lookup_exact(ds100_to_name)
|> if_not(fn() {lookup_fuzzy(query,DS100, fuzzy)}) |> if_not(fn() { lookup_fuzzy(query, DS100, fuzzy) })
_ -> { _ -> {
let by_ds100 = lookup_exact(query, ds100_to_name) let by_ds100 = lookup_exact(query, ds100_to_name)
let by_lp = lookup_exact(query, leitpunkt_to_name) let by_lp = lookup_exact(query, leitpunkt_to_name)
case #(by_ds100.0, by_lp.0) { case #(by_ds100.0, by_lp.0) {
#(200, _) -> #(302, proto<>ril100_domain<>"/"<>path) #(200, _) -> #(302, proto <> ril100_domain <> "/" <> path)
#(_, 200) -> #(302, proto<>leitpunkt_domain<>"/"<>path) #(_, 200) -> #(302, proto <> leitpunkt_domain <> "/" <> path)
_ -> #(302, proto<>ril100_domain<>"/"<>path) _ -> #(302, proto <> ril100_domain <> "/" <> path)
} }
} }
} }
} }
_ -> #(404, "intended usage is e.g. curl " <> proto<>domain<>"/FF") _ -> #(404, "intended usage is e.g. curl " <> proto <> domain <> "/FF")
} }
let body = bit_builder.from_string(text) let body = text
|> bit_builder.from_string
|> mist.Bytes
response.new(code) response.new(code)
|> response.prepend_header( |> response.prepend_header(
@ -184,17 +200,20 @@ fn lookup_station(
"https://stuebinm.eu/git/bahnhof.name", "https://stuebinm.eu/git/bahnhof.name",
) )
|> response.prepend_header("content-type", "text/plain; charset=utf8") |> response.prepend_header("content-type", "text/plain; charset=utf8")
|> fn (a) { case code == 302 { |> fn(a) {
True -> response.prepend_header(a, "location", text) case code == 302 {
_ -> a True -> response.prepend_header(a, "location", text)
} } _ -> a
}
}
|> response.set_body(body) |> response.set_body(body)
} }
pub fn main() { pub fn main() {
let assert Ok(bahn_ril100) = fetch_data() let assert Ok(bahn_ril100) = fetch_data()
let ds100s = read_csv(bahn_ril100) let ds100s =
read_csv(bahn_ril100)
|> list.filter_map(fn(fields) { |> list.filter_map(fn(fields) {
case fields { case fields {
[_, ds100, name, ..] -> Ok(#(name, ds100)) [_, ds100, name, ..] -> Ok(#(name, ds100))
@ -215,22 +234,31 @@ pub fn main() {
let name_to_leitpunkt = map.from_list(leitpunkte) let name_to_leitpunkt = map.from_list(leitpunkte)
let ds100_to_name = map.from_list(list.map(ds100s, swap)) let ds100_to_name = map.from_list(list.map(ds100s, swap))
let leitpunkt_to_name = map.from_list(list.map(leitpunkte, swap)) let leitpunkt_to_name = map.from_list(list.map(leitpunkte, swap))
let ds100index = index_new(atom.create_from_string("ds100")) let ds100index =
index_new(atom.create_from_string("ds100"))
|> index_ref(field_term("id")) |> index_ref(field_term("id"))
|> index_field(field_new("name")) |> index_field(field_new("name"))
|> index_add(ds100s |> index_add(
|> list.map(fn(tuple) {case tuple { ds100s
#(name, ds100) |> list.map(fn(tuple) {
-> map.from_list([#("id", ds100), #("name", name)] case tuple {
)}})) #(name, ds100) -> map.from_list([#("id", ds100), #("name", name)])
let leitpunkt_index = index_new(atom.create_from_string("leitpunkt")) }
}),
)
let leitpunkt_index =
index_new(atom.create_from_string("leitpunkt"))
|> index_ref(field_term("id")) |> index_ref(field_term("id"))
|> index_field(field_new("name")) |> index_field(field_new("name"))
|> index_add(leitpunkte |> index_add(
|> list.map(fn(tuple) {case tuple { leitpunkte
#(name, leitpunkt) |> list.map(fn(tuple) {
-> map.from_list([#("id", leitpunkt), #("name", name)] case tuple {
)}})) #(name, leitpunkt) ->
map.from_list([#("id", leitpunkt), #("name", name)])
}
}),
)
let ref = atom.create_from_string("ref") let ref = atom.create_from_string("ref")
let fuzzy = fn(searchterm: String, kind: IdKind) -> List(String) { let fuzzy = fn(searchterm: String, kind: IdKind) -> List(String) {
@ -242,26 +270,36 @@ pub fn main() {
let match = atom.create_from_string("match") let match = atom.create_from_string("match")
let field = atom.create_from_string("field") let field = atom.create_from_string("field")
let term = atom.create_from_string("term") let term = atom.create_from_string("term")
let expressions = tokenize(inspect(searchterm)) let expressions =
|> list.filter_map(fn (a) { map.get(a, atom.create_from_string("v")) }) tokenize(inspect(searchterm))
|> list.map(fn (token) { query_expression_new(match, [#(field, "name"), #(term, token)]) }) |> list.filter_map(fn(a) { map.get(a, atom.create_from_string("v")) })
let clause = query_expressions(clause_new(atom.create_from_string("all")), expressions) |> list.map(fn(token) {
query_expression_new(match, [#(field, "name"), #(term, token)])
})
let clause =
query_expressions(clause_new(atom.create_from_string("all")), expressions)
let query = query_clause(query, clause) let query = query_clause(query, clause)
let matches = query_run(query, index) let matches =
|> list.filter_map(fn (a) { map.get(a, ref) }) query_run(query, index)
|> list.filter_map(fn(a) { map.get(a, ref) })
inspect(matches) inspect(matches)
case list.length(matches) > 5 { case list.length(matches) > 5 {
True -> { True -> {
let query = query_new() let query = query_new()
let clause = query_expressions( let clause =
clause_new(atom.create_from_string("all")), query_expressions(
[query_expression_new(match, [#(field, "name"), #(term, "hbf")]) , ..expressions] clause_new(atom.create_from_string("all")),
) [
query_expression_new(match, [#(field, "name"), #(term, "hbf")]),
..expressions
],
)
let query = query_clause(query, clause) let query = query_clause(query, clause)
let narrow = query_run(query, index) let narrow =
|> list.filter_map(fn (a) { map.get(a, ref) }) query_run(query, index)
|> list.filter_map(fn(a) { map.get(a, ref) })
case narrow { case narrow {
[] -> matches [] -> matches
_ -> narrow _ -> narrow
@ -279,10 +317,9 @@ pub fn main() {
case map.get(stations, searchterm) { case map.get(stations, searchterm) {
Ok(id) -> Exact(id) Ok(id) -> Exact(id)
_ -> { _ -> {
let results = fuzzy(searchterm, kind) let results =
|> list.filter_map(fn (res) { fuzzy(searchterm, kind)
map.get(ids, string.uppercase(res)) |> list.filter_map(fn(res) { map.get(ids, string.uppercase(res)) })
})
case results { case results {
[res] -> Fuzzy(res) [res] -> Fuzzy(res)
[res, ..] -> Fuzzy(res) [res, ..] -> Fuzzy(res)
@ -294,16 +331,14 @@ pub fn main() {
io.println("compiled indices, starting server …") io.println("compiled indices, starting server …")
let _ = mist.run_service( let assert Ok(_) =
2345, fn(req: Request(mist.Connection)) -> Response(mist.ResponseData) {
fn(req) { lookup_station( lookup_station(req, ds100_to_name, leitpunkt_to_name, exact_then_fuzzy)
req, }
ds100_to_name, |> mist.new
leitpunkt_to_name, |> mist.port(2345)
exact_then_fuzzy |> mist.start_http
) },
max_body_limit: 100,
)
process.sleep_forever() process.sleep_forever()
} }