https://github.com/bwireman/glow_server
Defines macros to use GenServers where the core logic is defined in Gleam
https://github.com/bwireman/glow_server
Last synced: 5 months ago
JSON representation
Defines macros to use GenServers where the core logic is defined in Gleam
- Host: GitHub
- URL: https://github.com/bwireman/glow_server
- Owner: bwireman
- License: mit
- Created: 2021-08-28T17:00:57.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-02-22T23:53:41.000Z (over 4 years ago)
- Last Synced: 2025-06-09T14:05:06.804Z (about 1 year ago)
- Language: Elixir
- Homepage:
- Size: 8.79 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GlowServer
Defines macros to use GenServers where the core logic is defined in Gleam
## Usage
```elixir
# elixir server API
defmodule ExampleGlowServer do
use GlowServer, server: :example
def start_link(_), do: GenServer.start_link(__MODULE__, "initial-state", name: __MODULE__)
def init(init_arg), do: {:ok, init_arg}
def read(), do: call(__MODULE__, :read)
def reverse(), do: cast(__MODULE__, :reverse)
def clear(), do: set("")
def set(s), do: cast(__MODULE__, {:set, s})
def concat(s), do: call(__MODULE__, {:concat, s})
end
```
The `GlowServer.__using__` macro takes in the name of the implementing server and defines
`call` & `cast` which are forwarded to the server's `dispatch` function
```gleam
// core logic in gleam
import glow_server/core
import gleam/string
pub type Request {
Read
Reverse
Concat(String)
Set(String)
}
pub type Response =
String
pub type State =
String
pub fn dispatch(
request: core.Request(Request, State),
) -> core.Response(Response, State) {
case request {
core.Call(Read, state) -> core.build_reply(state, state)
core.Cast(Reverse, state) -> core.build_noreply(string.reverse(state))
core.Call(Concat(s), state) ->
core.build_reply(string.concat([s, state]), s)
core.Cast(Set(s), _state) -> core.build_noreply(s)
}
}
```