Skip to content

Commit

Permalink
Share data for client/server communication
Browse files Browse the repository at this point in the history
  • Loading branch information
ghivert committed Dec 1, 2024
1 parent 9fe39bd commit f45f5b7
Show file tree
Hide file tree
Showing 77 changed files with 2,448 additions and 734 deletions.
3 changes: 2 additions & 1 deletion apps/backend/gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ gleam_erlang = "~> 0.25"
gleam_hexpm = "~> 1.0"
gleam_http = "~> 3.6"
gleam_httpc = ">= 2.2.0 and < 3.0.0"
gleam_json = "~> 1.0"
gleam_json = ">= 2.1.0 and < 3.0.0"
gleam_otp = "~> 0.10"
gleam_package_interface = ">= 1.0.0 and < 2.0.0"
gleam_regexp = ">= 1.0.0 and < 2.0.0"
gleam_stdlib = ">= 0.44.0 and < 1.0.0"
gleam_yielder = ">= 1.1.0 and < 2.0.0"
glexer = ">= 1.0.1 and < 2.0.0"
interfaces = {path = "../../packages/interfaces"}
mist = ">= 3.0.0 and < 4.0.0"
pog = ">= 1.0.1 and < 2.0.0"
prng = ">= 3.0.3 and < 4.0.0"
Expand Down
9 changes: 5 additions & 4 deletions apps/backend/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ packages = [
{ name = "gleam_hexpm", version = "1.1.0", build_tools = ["gleam"], requirements = ["birl", "gleam_stdlib"], otp_app = "gleam_hexpm", source = "hex", outer_checksum = "D32439FD6AD683FE1094922737904EC2091E2D7B1F236AD23815935694A5221A" },
{ name = "gleam_http", version = "3.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "A9EE0722106FCCAB8AD3BF9D0A3EFF92BFE8561D59B83BAE96EB0BE1938D4E0F" },
{ name = "gleam_httpc", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "CF6CDD88830CC9853F7638ECC0BE7D7CD9522640DA5FAB4C08CFAC8DEBD08028" },
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
{ name = "gleam_json", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "0A57FB5666E695FD2BEE74C0428A98B0FC11A395D2C7B4CDF5E22C5DD32C74C6" },
{ name = "gleam_otp", version = "0.14.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5A8CE8DBD01C29403390A7BD5C0A63D26F865C83173CF9708E6E827E53159C65" },
{ name = "gleam_package_interface", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_package_interface", source = "hex", outer_checksum = "CF3BFC5D0997750D9550D8D73A90F4B8D71C6C081B20ED4E70FFBE1E99AFC3C2" },
{ name = "gleam_regexp", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "A3655FDD288571E90EE9C4009B719FEF59FA16AFCDF3952A76A125AF23CF1592" },
{ name = "gleam_stdlib", version = "0.44.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "A6E55E309A6778206AAD4038D9C49E15DF71027A1DB13C6ADA06BFDB6CF1260E" },
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "glexer", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "BD477AD657C2B637FEF75F2405FAEFFA533F277A74EF1A5E17B55B1178C228FB" },
{ name = "glisten", version = "6.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], otp_app = "glisten", source = "hex", outer_checksum = "912132751031473CB38F454120124FFC96AF6B0EA33D92C9C90DB16327A2A972" },
{ name = "gramps", version = "2.0.3", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "3CCAA6E081225180D95C79679D383BBF51C8D1FDC1B84DA1DA444F628C373793" },
{ name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" },
{ name = "interfaces", version = "1.0.0", build_tools = ["gleam"], requirements = ["birl", "gleam_json", "gleam_stdlib"], source = "local", path = "../../packages/interfaces" },
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
{ name = "marceau", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "2D1C27504BEF45005F5DFB18591F8610FB4BFA91744878210BDC464412EC44E9" },
{ name = "mist", version = "3.0.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "CDA1A74E768419235E16886463EC4722EFF4AB3F8D820A76EAD45D7C167D7282" },
Expand All @@ -48,7 +49,6 @@ packages = [
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
{ name = "stoiridh_version", version = "0.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "stoiridh_version", source = "hex", outer_checksum = "EEF8ADAB9755BD33EB202F169376F1A7797AEF90823FDCA671D8590D04FBF56B" },
{ name = "telemetry", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "telemetry", source = "hex", outer_checksum = "7015FC8919DBE63764F4B4B87A95B7C0996BD539E0D499BE6EC9D7F3875B79E6" },
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
{ name = "tom", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "228E667239504B57AD05EC3C332C930391592F6C974D0EFECF32FFD0F3629A27" },
{ name = "verl", version = "1.1.1", build_tools = ["rebar3"], requirements = [], otp_app = "verl", source = "hex", outer_checksum = "0925E51CD92A0A8BE271765B02430B2E2CFF8AC30EF24D123BD0D58511E8FB18" },
{ name = "wisp", version = "1.3.0", build_tools = ["gleam"], requirements = ["directories", "exception", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_stdlib", "logging", "marceau", "mist", "simplifile"], otp_app = "wisp", source = "hex", outer_checksum = "D12384EE63ADEE833B40A6D26EF8014A6E4BFC10EC2CDC8B57D325DD4B52740E" },
Expand All @@ -65,14 +65,15 @@ gleam_erlang = { version = "~> 0.25" }
gleam_hexpm = { version = "~> 1.0" }
gleam_http = { version = "~> 3.6" }
gleam_httpc = { version = ">= 2.2.0 and < 3.0.0" }
gleam_json = { version = "~> 1.0" }
gleam_json = { version = ">= 2.1.0 and < 3.0.0" }
gleam_otp = { version = "~> 0.10" }
gleam_package_interface = { version = ">= 1.0.0 and < 2.0.0" }
gleam_regexp = { version = ">= 1.0.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.44.0 and < 1.0.0" }
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
gleeunit = { version = "~> 1.0" }
glexer = { version = ">= 1.0.1 and < 2.0.0" }
interfaces = { path = "../../packages/interfaces" }
mist = { version = ">= 3.0.0 and < 4.0.0" }
pog = { version = ">= 1.0.1 and < 2.0.0" }
pprint = { version = ">= 1.0.3 and < 2.0.0" }
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/backend/data/hex_read.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub type HexRead {
pub fn decode(data) {
dynamic.decode2(
HexRead,
dynamic.element(0, dynamic.int),
dynamic.element(1, helpers.decode_time),
dynamic.field("id", dynamic.int),
dynamic.field("last_check", helpers.decode_time),
)(data)
}
12 changes: 6 additions & 6 deletions apps/backend/src/backend/data/hex_user.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub type HexUser {
pub fn decode(data) {
dynamic.decode6(
HexUser,
dynamic.element(0, dynamic.int),
dynamic.element(1, dynamic.string),
dynamic.element(2, dynamic.optional(dynamic.string)),
dynamic.element(3, dynamic.string),
dynamic.element(4, helpers.decode_time),
dynamic.element(5, helpers.decode_time),
dynamic.field("id", dynamic.int),
dynamic.field("username", dynamic.string),
dynamic.field("email", dynamic.optional(dynamic.string)),
dynamic.field("url", dynamic.string),
dynamic.field("created_at", helpers.decode_time),
dynamic.field("updated_at", helpers.decode_time),
)(data)
}
6 changes: 2 additions & 4 deletions apps/backend/src/backend/error.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,13 @@ pub fn log_dynamic_error(error: dynamic.DecodeError) {
pub fn log_decode_error(error: json.DecodeError) {
case error {
json.UnexpectedEndOfInput -> wisp.log_warning("Unexpected end of input")
json.UnexpectedByte(byte, position) -> {
json.UnexpectedByte(byte) -> {
wisp.log_warning("Unexpected byte")
wisp.log_warning(" byte: " <> byte)
wisp.log_warning(" position: " <> int.to_string(position))
}
json.UnexpectedSequence(byte, position) -> {
json.UnexpectedSequence(byte) -> {
wisp.log_warning("Unexpected sequence")
wisp.log_warning(" byte: " <> byte)
wisp.log_warning(" position: " <> int.to_string(position))
}
json.UnexpectedFormat(errors) -> {
wisp.log_warning("Unexpected format")
Expand Down
16 changes: 12 additions & 4 deletions apps/backend/src/backend/gleam/generate/types.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,18 @@ fn type_to_json(ctx: Context, type_: Type) {
}

fn find_package_release(ctx: Context, package: String, requirement: String) {
"SELECT package_release.id, package_release.version
"SELECT package_release.id id, package_release.version version
FROM package
JOIN package_release
ON package.id = package_release.package_id
WHERE package.name = $1"
|> pog.query
|> pog.parameter(pog.text(package))
|> pog.returning(dynamic.tuple2(dynamic.int, dynamic.string))
|> pog.returning(dynamic.decode2(
pair.new,
dynamic.field("id", dynamic.int),
dynamic.field("version", dynamic.string),
))
|> pog.execute(ctx.db)
|> result.map_error(error.DatabaseError)
|> result.map(fn(response) { response.rows })
Expand Down Expand Up @@ -173,7 +177,7 @@ fn find_signature_from_release(
) {
use acc, release <- list.fold(releases, error.empty())
use <- bool.guard(when: result.is_ok(acc), return: acc)
"SELECT release.version, signature.id
"SELECT release.version version, signature.id id
FROM package_release release
JOIN package_module module
ON module.package_release_id = release.id
Expand All @@ -186,7 +190,11 @@ fn find_signature_from_release(
|> pog.parameter(pog.text(name))
|> pog.parameter(pog.text(module))
|> pog.parameter(pog.int(release))
|> pog.returning(dynamic.tuple2(dynamic.string, dynamic.int))
|> pog.returning(dynamic.decode2(
pair.new,
dynamic.field("version", dynamic.string),
dynamic.field("id", dynamic.int),
))
|> pog.execute(ctx.db)
|> result.map_error(error.DatabaseError)
|> result.try(fn(response) {
Expand Down
7 changes: 6 additions & 1 deletion apps/backend/src/backend/gleam/type_search/state.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import gleam/function
import gleam/list
import gleam/option
import gleam/otp/actor
import gleam/pair
import gleam/result
import pog

Expand Down Expand Up @@ -101,7 +102,11 @@ fn compute_rows(
OFFSET $1"
|> pog.query
|> pog.parameter(pog.int(offset))
|> pog.returning(dynamic.tuple2(dynamic.string, dynamic.int))
|> pog.returning(dynamic.decode2(
pair.new,
dynamic.field("signature_", dynamic.string),
dynamic.field("id", dynamic.int),
))
|> pog.execute(db)
|> result.map(fn(r) { r.rows })
|> result.unwrap([])
Expand Down
4 changes: 3 additions & 1 deletion apps/backend/src/backend/postgres/postgres.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import pog.{Config}

pub fn connect(database_url: String) {
let assert Ok(config) = parse_database_url(database_url)
pog.connect(config)
config
|> pog.rows_as_map(True)
|> pog.connect
}

fn parse_database_url(database_url: String) {
Expand Down
Loading

0 comments on commit f45f5b7

Please sign in to comment.