Skip to content

Commit

Permalink
use db URL
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowdHailer committed May 3, 2020
1 parent 6f101f4 commit e4f227d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
16 changes: 8 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ services:
dockerfile: framework.Dockerfile
depends_on:
- db
# volumes:
# - .:/opt/app
# # - gen:/opt/app/gen
# - _build:/opt/app/_build
volumes:
- .:/opt/app
# - gen:/opt/app/gen
- _build:/opt/app/_build
environment:
# Used by diesel
- DATABASE_URL=postgres://postgres:postgres@127.0.0.1:5432/postgres
# Used by pgo config
- POSTGRES_HOST=127.0.0.1
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
# - POSTGRES_HOST=127.0.0.1
# - POSTGRES_USER=postgres
# - POSTGRES_PASSWORD=postgres
# - POSTGRES_DB=postgres
network_mode: host
# command: bash -c "rebar3 release && _build/default/rel/hello_world/bin/hello_world foreground"
# TODO remove these options, a) move to docker image b) investigate rebar
Expand Down
15 changes: 3 additions & 12 deletions src/hello_world/config.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ pub type Config {
Config(
port: Int,
secret_key_base: String,
postgres_host: String,
postgres_db: String,
postgres_user: String,
postgres_password: String,
database_url: String,
)
}

Expand All @@ -23,19 +20,13 @@ pub fn from_env() {

// let Ok(secret_key_base) = required(env, "SECRET_KEY_BASE", fn(x) { Ok(x) })
let port = optional(env, "PORT", int.parse, 8080)
let Ok(postgres_host) = required(env, "POSTGRES_HOST", string)
let Ok(postgres_db) = required(env, "POSTGRES_DB", string)
let Ok(postgres_user) = required(env, "POSTGRES_USER", string)
let Ok(postgres_password) = required(env, "POSTGRES_PASSWORD", string)
let Ok(database_url) = required(env, "DATABASE_URL", string)

Ok(
Config(
port: port,
secret_key_base: "secret_key_base",
postgres_host: postgres_host,
postgres_db: postgres_db,
postgres_user: postgres_user,
postgres_password: postgres_password,
database_url: database_url,
),
)
}
28 changes: 24 additions & 4 deletions src/hello_world/db/client.gleam
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import gleam/atom
import gleam/atom.{Atom}
import gleam/dynamic
import gleam/dynamic.{Dynamic}
import gleam/map.{Map}

import midas/http
import midas_utils

import pg_client/pg_client
import hello_world/config.{Config}

external fn uri_parse(String) -> Map(Atom, Dynamic) = "uri_string" "parse"

pub fn spawn_link(config){
let Config(
postgres_host: postgres_host,
postgres_db: postgres_db,
postgres_user: postgres_user,
postgres_password: postgres_password,
database_url: database_url,
..,
) = config

// TODO move this to the midas framework
let database_url = uri_parse(database_url)
let Ok(postgres_host) = map.get(database_url, atom.create_from_string("host"))
let Ok(postgres_host) = dynamic.string(postgres_host)
let Ok(path) = map.get(database_url, atom.create_from_string("path"))
let Ok(path) = dynamic.string(path)
let Ok(userinfo) = map.get(database_url, atom.create_from_string("userinfo"))
let Ok(userinfo) = dynamic.string(userinfo)
let tuple(postgres_user, Ok(postgres_password)) = midas_utils.split_on(userinfo, ":")
let [postgres_db] = http.split_segments(path)

let options = [
pg_client.Host(postgres_host),
pg_client.Database(postgres_db),
Expand Down
7 changes: 7 additions & 0 deletions test/hello_world_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import gleam/should

pub fn syntax_test() {
// NOTE about list destructuring
let [x] = [5]
should.equal(x, 4)
}

0 comments on commit e4f227d

Please sign in to comment.