Skip to content

Commit

Permalink
refactor: rename from store to adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Hill committed Nov 16, 2024
1 parent a96f7db commit 1c8968a
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 75 deletions.
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ import mist
import wisp
import wisp/wisp_mist
import wisp_kv_sessions
import wisp_kv_sessions/actor_store
import wisp_kv_sessions/actor_adapter
import wisp_kv_sessions/session
import wisp_kv_sessions/session_config
pub fn main() {
// Setup session_store
use actor_store <- result.map(actor_store.try_create_session_store())
use cache_store <- result.map(actor_store.try_create_session_store())
use actor_adapter <- result.map(actor_adapter.try_create_session_store())
use cache_store <- result.map(actor_adapter.try_create_session_store())
// Create session config
let session_config =
session_config.Config(
default_expiry: session.ExpireIn(60 * 60),
cookie_name: "SESSION_COOKIE",
store: actor_store,
store: actor_adapter,
cache: option.Some(cache_store),
)
Expand Down Expand Up @@ -131,65 +131,65 @@ tried.
Session data will be automatically added and removed from the cache.

# SessionStore

A SessionStore is an type that can be used to implement different storage
providers, such as postgres/reddis/sqlite. You can use one of the prebuild
storage providers from down below, or implement a new one if the one you
There are different places you may want to store sessions such as
postgres/ETS/sqlite. The way you integrate with them is through storage adapters
that implement the type SessionStore. You can use one of the prebuild storage
adapters from down below, or implement a new one if the one you
are looking for does not exist.

For an example implementation, see `./src/wisp_kv_sessions/actor_store.gleam`.
For an example implementation, see `./src/wisp_kv_sessions/ets_adapter.gleam`.

## Existing SessionStores
## Included SessionStore adapters

### actor_store (Included by Default)
The actor_store driver is suitable for development and testing purposes,
### actor_adapter
The actor_adapter driver is suitable for development and testing purposes,
but not recommended for production due to its non-concurrent nature.
Internally, it uses an [actor](https://hexdocs.pm/gleam_otp/gleam/otp/actor.html),
which may become a bottleneck under heavy loads.

*Usage Example:*

```gleam
import wisp_kv_sessions/actor_store
import wisp_kv_sessions/actor_adapter
use session_store <- result.map(actor_store.new())
use session_store <- result.map(actor_adapter.new())
// ...
```
See `./example/src/app.gleam` for full example

### postgress_store
Also included is the postgres_store, that allows you to use postgres as
### postgres_adapter
Also included is the postgres_adapter, that allows you to use postgres as
the storage implementation

```gleam
import wisp_kv_sessions/postgres_store
import wisp_kv_sessions/postgres_adapter
let db =
pog.default_config()
|> pog.connect()
// Migrate
use _ <- result.try(postgres_store.migrate_up(conn))
use _ <- result.try(postgres_adapter.migrate_up(conn))
// Setup session_store
use session_store <- result.map(postgres_store.new(conn))
use session_store <- result.map(postgres_adapter.new(conn))
//...
```


### ets_store
### ets_adapter

The ets_store uses [Erlang Term Storage](https://www.erlang.org/doc/apps/stdlib/ets.html)
The ets_adapter uses [Erlang Term Storage](https://www.erlang.org/doc/apps/stdlib/ets.html)
and [carpenter](https://hexdocs.pm/carpenter/) to store session information.
*This will NOT be persistant after restarts*. But is a good option for caching.

```gleam
import wisp_kv_sessions/ets_store
import wisp_kv_sessions/ets_adapter
// Setup session_store
use session_store <- result.map(ets_store.new(conn))
use session_store <- result.map(ets_adapter.new(conn))
//...
```
26 changes: 16 additions & 10 deletions example/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,37 @@
# You typically do not need to edit this file

packages = [
{ name = "backoff", version = "1.1.6", build_tools = ["rebar3"], requirements = [], otp_app = "backoff", source = "hex", outer_checksum = "CF0CFFF8995FB20562F822E5CC47D8CCF664C5ECDC26A684CBE85C225F9D7C39" },
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
{ name = "carpenter", version = "0.3.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "carpenter", source = "hex", outer_checksum = "7F5AF15A315CF32E8EDD0700BC1E6711618F8049AFE66DFCE82D1161B33F7F1B" },
{ name = "directories", version = "1.1.0", build_tools = ["gleam"], requirements = ["envoy", "gleam_stdlib", "platform", "simplifile"], otp_app = "directories", source = "hex", outer_checksum = "BDA521A4EB9EE3A7894F0DC863797878E91FF5C7826F7084B2E731E208BDB076" },
{ name = "envoy", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "CFAACCCFC47654F7E8B75E614746ED924C65BD08B1DE21101548AC314A8B6A41" },
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" },
{ name = "gleam_crypto", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "8AE56026B3E05EBB1F076778478A762E9EB62B31AEEB4285755452F397029D22" },
{ name = "gleam_erlang", version = "0.30.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "760618870AE4A497B10C73548E6E44F43B76292A54F0207B3771CBB599C675B4" },
{ name = "gleam_http", version = "3.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "EA66440C2269F7CED0F6845E5BD0DB68095775D627FA709A841CA78A398D6D56" },
{ 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_otp", version = "0.11.2", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "517FFB679E44AD71D059F3EF6A17BA6EFC8CB94FA174D52E22FB6768CF684D78" },
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
{ 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_stdlib", version = "0.42.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "CF1C19DAB36C82EF6A8C60CF38884825641EBEA45E5495760D642C2ABB266192" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "glisten", version = "5.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], otp_app = "glisten", source = "hex", outer_checksum = "48EF7F6D1DCA877C2F49AF35CC33946C7129EEB05A114758A2CC569C708BFAF8" },
{ 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 = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
{ name = "marceau", version = "1.2.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "5188D643C181EE350D8A20A3BDBD63AF7B6C505DE333CFBE05EF642ADD88A59B" },
{ name = "marceau", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "2D1C27504BEF45005F5DFB18591F8610FB4BFA91744878210BDC464412EC44E9" },
{ name = "mist", version = "2.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 = "981F12FC8BA0656B40099EC876D6F2BEE7B95593610F342E9AB0DC4E663A932F" },
{ name = "opentelemetry_api", version = "1.4.0", build_tools = ["rebar3", "mix"], requirements = [], otp_app = "opentelemetry_api", source = "hex", outer_checksum = "3DFBBFAA2C2ED3121C5C483162836C4F9027DEF469C41578AF5EF32589FCFC58" },
{ name = "pg_types", version = "0.4.0", build_tools = ["rebar3"], requirements = [], otp_app = "pg_types", source = "hex", outer_checksum = "B02EFA785CAECECF9702C681C80A9CA12A39F9161A846CE17B01FB20AEEED7EB" },
{ name = "pgo", version = "0.14.0", build_tools = ["rebar3"], requirements = ["backoff", "opentelemetry_api", "pg_types"], otp_app = "pgo", source = "hex", outer_checksum = "71016C22599936E042DC0012EE4589D24C71427D266292F775EBF201D97DF9C9" },
{ name = "platform", version = "1.0.0", build_tools = ["gleam"], requirements = [], otp_app = "platform", source = "hex", outer_checksum = "8339420A95AD89AAC0F82F4C3DB8DD401041742D6C3F46132A8739F6AEB75391" },
{ name = "pog", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "pgo"], otp_app = "pog", source = "hex", outer_checksum = "00D57120936AFBF486BE357C472E483C1F0CA507FF9C3668075E87C733CA53F8" },
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
{ name = "simplifile", version = "2.0.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "5FFEBD0CAB39BDD343C3E1CCA6438B2848847DC170BA2386DF9D7064F34DF000" },
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
{ 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 = "wisp", version = "1.1.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 = "5B25E37E08A94039668D2ACF7B2D2A665BEAF4B48EFA8613010B4E3164137664" },
{ name = "wisp_kv_sessions", version = "1.0.0", build_tools = ["gleam"], requirements = ["birl", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_otp", "gleam_stdlib", "wisp"], source = "local", path = ".." },
{ name = "wisp", version = "1.2.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 = "F71265D2F1DE11426535A2FA1DA3B11D2FFB783B116DF9496BC8C41983EBADB4" },
{ name = "wisp_kv_sessions", version = "0.1.0", build_tools = ["gleam"], requirements = ["birl", "carpenter", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_otp", "gleam_stdlib", "pog", "wisp"], source = "local", path = ".." },
]

[requirements]
Expand Down
10 changes: 5 additions & 5 deletions example/src/app.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import mist
import wisp
import wisp/wisp_mist
import wisp_kv_sessions
import wisp_kv_sessions/actor_store
import wisp_kv_sessions/actor_adapter
import wisp_kv_sessions/session
import wisp_kv_sessions/session_config

pub fn main() {
// Setup session_store
use actor_store <- result.map(actor_store.try_create_session_store())
use cache_store <- result.map(actor_store.try_create_session_store())
// Setup session_adapter
use store <- result.map(actor_adapter.new())
use cache_store <- result.map(actor_adapter.new())

// Create session config
let session_config =
session_config.Config(
default_expiry: session.ExpireIn(60 * 60),
cookie_name: "SESSION_COOKIE",
store: actor_store,
store:,
cache: option.Some(cache_store),
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 9 additions & 11 deletions test/test_helpers.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ import gleam/dynamic
import gleam/erlang/os
import gleam/http/response
import gleam/int
import gleam/io
import gleam/json
import gleam/list
import gleam/option
import gleam/result
import gleeunit/should
import pog
import wisp
import wisp_kv_sessions/actor_store
import wisp_kv_sessions/postgres_store
import wisp_kv_sessions/actor_adapter
import wisp_kv_sessions/postgres_adapter
import wisp_kv_sessions/session
import wisp_kv_sessions/session_config

pub fn new_db() {
io.debug(os.get_all_env())
let db_host = os.get_env("DB_HOST") |> result.unwrap("127.0.0.1")
let db_password =
os.get_env("DB_PASSWORD") |> result.unwrap("mySuperSecretPassword!")
Expand All @@ -39,8 +37,8 @@ pub fn new_db() {
|> pog.pool_size(1)
|> pog.connect()

let assert Ok(_) = postgres_store.migrate_down(db)
let assert Ok(_) = postgres_store.migrate_up(db)
let assert Ok(_) = postgres_adapter.migrate_down(db)
let assert Ok(_) = postgres_adapter.migrate_up(db)
db
}

Expand Down Expand Up @@ -87,22 +85,22 @@ pub fn get_session_cookie_from_response(res, req) {

pub fn test_session_config() {
let expiration = birl.now() |> birl.add(duration.days(3))
use actor_store <- result.map(actor_store.new())
use store <- result.map(actor_adapter.new())
let session_config =
session_config.Config(
default_expiry: session.ExpireAt(expiration),
cookie_name: "SESSION_COOKIE",
store: actor_store,
store:,
cache: option.None,
)

#(session_config, actor_store, expiration)
#(session_config, store, expiration)
}

pub fn test_session_config_with_cache() {
let expiration = birl.now() |> birl.add(duration.days(3))
use main_store <- result.try(actor_store.new())
use cache_store <- result.map(actor_store.new())
use main_store <- result.try(actor_adapter.new())
use cache_store <- result.map(actor_adapter.new())

let session_config =
session_config.Config(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gleeunit/should
import wisp_kv_sessions/ets_store
import wisp_kv_sessions/ets_adapter
import wisp_kv_sessions/session

pub fn set_get_session_test() {
Expand All @@ -8,7 +8,7 @@ pub fn set_get_session_test() {
|> session.with_entry("test", "hello")
|> session.build

let session_store = ets_store.new("test_table")
let session_store = ets_adapter.new("test_table")

session_store.save_session(session)
|> should.be_ok()
Expand All @@ -26,7 +26,7 @@ pub fn set_delete_session_test() {
|> session.with_entry("test", "hello")
|> session.build

let session_store = ets_store.new("test_table2")
let session_store = ets_adapter.new("test_table2")

session_store.save_session(session)
|> should.be_ok()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import gleeunit/should
import test_helpers
import wisp_kv_sessions/postgres_store
import wisp_kv_sessions/postgres_adapter
import wisp_kv_sessions/session

pub fn set_get_session_test() {
Expand All @@ -10,7 +10,7 @@ pub fn set_get_session_test() {
|> session.with_entry("test", "hello")
|> session.build

let session_store = postgres_store.new(db)
let session_store = postgres_adapter.new(db)

session_store.save_session(session)
|> should.be_ok()
Expand All @@ -29,7 +29,7 @@ pub fn set_delete_session_test() {
|> session.with_entry("test", "hello")
|> session.build

let session_store = postgres_store.new(db)
let session_store = postgres_adapter.new(db)

session_store.save_session(session)
|> should.be_ok()
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn encode_data_test() {
)
|> dict.insert("key_three", "value3")

let json = postgres_store.encode_data(data)
let json = postgres_adapter.encode_data(data)

json
|> json.to_string
Expand All @@ -86,7 +86,7 @@ pub fn encode_data_test() {

json
|> json.to_string
|> postgres_store.decode_data_from_string()
|> postgres_adapter.decode_data_from_string()
|> should.be_ok
|> should.equal(data)
}
Loading

0 comments on commit 1c8968a

Please sign in to comment.