Skip to content

Commit

Permalink
use application module for clock instead of actor
Browse files Browse the repository at this point in the history
  • Loading branch information
rawhat committed May 4, 2024
1 parent fcd0fda commit d3817b8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 54 deletions.
4 changes: 4 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ repository = { type = "github", user = "rawhat", repo = "mist" }
target = "erlang"
gleam = ">= 1.0.0"

[erlang]
# application_start_module = "mist/internal/clock"
application_start_module = "mist_clock"

[dependencies]
gleam_stdlib = "~> 0.35 or ~> 1.0"
gleam_erlang = "~> 0.24"
Expand Down
68 changes: 16 additions & 52 deletions src/mist.gleam
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import gleam/bytes_builder.{type BytesBuilder}
import gleam/bit_array
import gleam/bytes_builder.{type BytesBuilder}
import gleam/erlang/process.{type ProcessDown, type Selector, type Subject}
import gleam/function
import gleam/http.{type Scheme, Http, Https} as gleam_http
import gleam/http/request.{type Request}
import gleam/http/response.{type Response}
import gleam/http.{type Scheme, Http, Https} as gleam_http
import gleam/int
import gleam/io
import gleam/iterator.{type Iterator}
Expand Down Expand Up @@ -379,31 +379,13 @@ fn convert_glisten_error(err: glisten.StartError) -> actor.StartError {
pub fn start_http(
builder: Builder(Connection, ResponseData),
) -> Result(Subject(supervisor.Message), glisten.StartError) {
let clock = supervisor.worker(fn(_argument) { clock.start() })
let glisten_pool =
supervisor.supervisor(fn(_argument) {
fn(req) { convert_body_types(builder.handler(req)) }
|> handler.with_func
|> glisten.handler(handler.init, _)
|> glisten.serve(builder.port)
|> result.map(fn(subj) {
builder.after_start(builder.port, Http)
subj
})
|> result.map_error(convert_glisten_error)
})

supervisor.start(fn(children) {
children
|> supervisor.add(clock)
|> supervisor.add(glisten_pool)
})
|> result.map_error(fn(err) {
case err {
actor.InitTimeout -> glisten.AcceptorTimeout
actor.InitFailed(reason) -> glisten.AcceptorFailed(reason)
actor.InitCrashed(reason) -> glisten.AcceptorCrashed(reason)
}
fn(req) { convert_body_types(builder.handler(req)) }
|> handler.with_func
|> glisten.handler(handler.init, _)
|> glisten.serve(builder.port)
|> result.map(fn(subj) {
builder.after_start(builder.port, Http)
subj
})
}

Expand Down Expand Up @@ -443,31 +425,13 @@ pub fn start_https(

use _ <- result.then(res)

let clock = supervisor.worker(fn(_argument) { clock.start() })

let glisten_pool =
supervisor.supervisor(fn(_argument) {
fn(req) { convert_body_types(builder.handler(req)) }
|> handler.with_func
|> glisten.handler(handler.init, _)
|> glisten.serve_ssl(builder.port, certfile, keyfile)
|> result.map(fn(subj) {
builder.after_start(builder.port, Https)
subj
})
|> result.map_error(convert_glisten_error)
})
supervisor.start(fn(children) {
children
|> supervisor.add(clock)
|> supervisor.add(glisten_pool)
})
|> result.map_error(fn(err) {
case err {
actor.InitTimeout -> glisten.AcceptorTimeout
actor.InitFailed(reason) -> glisten.AcceptorFailed(reason)
actor.InitCrashed(reason) -> glisten.AcceptorCrashed(reason)
}
fn(req) { convert_body_types(builder.handler(req)) }
|> handler.with_func
|> glisten.handler(handler.init, _)
|> glisten.serve_ssl(builder.port, certfile, keyfile)
|> result.map(fn(subj) {
builder.after_start(builder.port, Https)
subj
})
|> result.map_error(GlistenError)
}
Expand Down
11 changes: 9 additions & 2 deletions src/mist/internal/clock.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import birl
import gleam/erlang/process.{type Subject}
import gleam/erlang/atom
import gleam/erlang/process.{type Pid}
import gleam/function
import gleam/otp/actor
import gleam/result
import logging

pub type ClockMessage {
Expand All @@ -23,7 +25,7 @@ pub type EtsOpts {
ReadConcurrency(Bool)
}

pub fn start() -> Result(Subject(ClockMessage), actor.StartError) {
pub fn start(_type, _args) -> Result(Pid, actor.StartError) {
actor.start_spec(
actor.Spec(
init: fn() {
Expand All @@ -47,6 +49,11 @@ pub fn start() -> Result(Subject(ClockMessage), actor.StartError) {
},
),
)
|> result.map(process.subject_owner)
}

pub fn stop(_state) {
atom.create_from_string("ok")
}

pub fn get_date() -> String {
Expand Down
11 changes: 11 additions & 0 deletions src/mist_clock.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-module(mist_clock).

-behaviour(application).

-export([start/2, stop/1]).

start(_Type, _Args) ->
{ok, self()}.

stop(_State) ->
ok.

0 comments on commit d3817b8

Please sign in to comment.