-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from ghivert/refactor/improvements
Upgrade the whole application
- Loading branch information
Showing
305 changed files
with
5,754 additions
and
23,490 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Runtimes used by the stack. | ||
[tools] | ||
gleam = "1.4.1" | ||
gleam = "1.6.2" | ||
erlang = "27" | ||
node = "22" | ||
deno = "latest" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,57 @@ | ||
import backend/config | ||
import backend/context.{type Context, Context} | ||
import backend/gleam/type_search/state as type_search | ||
import backend/postgres/postgres | ||
import backend/router | ||
import backend/setup | ||
import backend/workers | ||
import envoy | ||
import gleam/erlang/process | ||
import gleam/function | ||
import gleam/int | ||
import gleam/option.{Some} | ||
import gleam/otp/supervisor | ||
import gleam/result | ||
import mist | ||
import periodic | ||
import setup | ||
import tasks/hex | ||
import tasks/popularity | ||
import tasks/ranking | ||
import tasks/timeseries | ||
import wisp | ||
import wisp/logger | ||
import wisp/wisp_mist | ||
|
||
pub fn main() { | ||
wisp.configure_logger() | ||
|
||
let secret_key_base = config.get_secret_key_base() | ||
let cnf = config.read_config() | ||
let ctx = postgres.connect(cnf) | ||
configure_logger() | ||
let assert Ok(ctx) = context.init() | ||
let assert Ok(ctx) = start_type_search_worker(ctx) | ||
let assert Ok(_) = start_http_server(ctx) | ||
let assert Ok(_) = start_periodic_workers(ctx) | ||
process.sleep_forever() | ||
} | ||
|
||
logger.set_level(cnf.level) | ||
fn configure_logger() { | ||
let level = logger.read_level() | ||
wisp.configure_logger() | ||
logger.set_level(level) | ||
setup.radiate() | ||
} | ||
|
||
let assert Ok(subject) = type_search.init(ctx.db) | ||
|
||
let ctx = ctx |> config.add_type_search_subject(subject) | ||
|
||
let assert Ok(_) = | ||
router.handle_request(_, ctx) | ||
|> wisp_mist.handler(secret_key_base) | ||
|> mist.new() | ||
|> mist.port(cnf.port) | ||
|> mist.start_http() | ||
|
||
let assert Ok(_) = { | ||
use periodic_children <- supervisor.start() | ||
use periodic_children <- function.tap(periodic_children) | ||
let assert Ok(_) = { | ||
use children <- supervisor.start() | ||
// Every 10 seconds | ||
add_periodic_worker(periodic_children, waiting: 10 * 1000, do: fn() { | ||
hex.sync_new_gleam_releases(ctx, children) | ||
}) | ||
// Every day | ||
add_periodic_worker(periodic_children, waiting: 86_400_000, do: fn() { | ||
ranking.compute_ranking(ctx) | ||
}) | ||
// Every day | ||
add_periodic_worker(periodic_children, waiting: 86_400_000, do: fn() { | ||
popularity.compute_popularity(ctx) | ||
}) | ||
// Every hour | ||
add_periodic_worker(periodic_children, waiting: 3600 * 1000, do: fn() { | ||
timeseries.store_timeseries(ctx) | ||
}) | ||
} | ||
} | ||
fn start_type_search_worker(ctx: Context) { | ||
use subject <- result.map(type_search.init(ctx.db)) | ||
Context(..ctx, type_search_subject: Some(subject)) | ||
} | ||
|
||
process.sleep_forever() | ||
fn start_http_server(ctx) { | ||
use port <- result.try(envoy.get("PORT")) | ||
use port <- result.map(int.parse(port)) | ||
let secret_key_base = context.get_secret_key_base() | ||
router.handle_request(_, ctx) | ||
|> wisp_mist.handler(secret_key_base) | ||
|> mist.new() | ||
|> mist.port(port) | ||
|> mist.start_http() | ||
} | ||
|
||
fn add_periodic_worker(children, waiting delay, do work) { | ||
use _ <- function.tap(children) | ||
supervisor.add(children, { | ||
use _ <- supervisor.worker() | ||
periodic.periodically(do: work, waiting: delay) | ||
}) | ||
fn start_periodic_workers(ctx) { | ||
use children <- supervisor.start() | ||
use children <- function.tap(children) | ||
workers.sync_new_gleam_releases_ten_secondly(ctx, children) | ||
workers.compute_ranking_daily(ctx, children) | ||
workers.compute_popularity_daily(ctx, children) | ||
workers.store_timeseries_hourly(ctx, children) | ||
} |
Oops, something went wrong.