~edwardloveall/scribe

Remove the need for a DATABASE_URL v1 PROPOSED

Michael Herold: 1
 Remove the need for a DATABASE_URL

 5 files changed, 7 insertions(+), 28 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~edwardloveall/scribe/patches/32424/mbox | git am -3
Learn more about email & git

[PATCH] Remove the need for a DATABASE_URL Export this patch

Since the application does not use a database, it's confusing to have to
set a bogus database URL environment variable. This change follows [the
Lucky guide][1] suggestion for disabling the need for database
configuration. That makes the setup a little easier.

[1]: https://www.luckyframework.org/guides/database/intro-to-avram-and-orms
---
 README.md                |  6 +-----
 config/database.cr       | 24 +++++-------------------
 script/docker_entrypoint |  2 +-
 src/app.cr               |  1 -
 src/app_database.cr      |  2 --
 5 files changed, 7 insertions(+), 28 deletions(-)
 delete mode 100644 src/app_database.cr

diff --git a/README.md b/README.md
index d7ace3e..4226c02 100644
--- a/README.md
+++ b/README.md
@@ -6,8 +6,6 @@ ## Deploying Your Own

I'd love it if you deployed your own version of this app! A [few others](docs/instances.md) have already. To do so currently will take some knowledge of how a webserver runs. This app is built with the [Lucky framework](https://luckyframework.org) and there are a bunch of different ways to deploy. The main instance runs on [Ubuntu](https://luckyframework.org/guides/deploying/ubuntu) but there are also directions for [Heroku](https://luckyframework.org/guides/deploying/heroku) or [Dokku](https://luckyframework.org/guides/deploying/dokku).

One thing to note is that this app doesn't currently use a database. Any instructions around postgres can be safely ignored. However, Lucky (and it's dependency Avram) do require a `DATABASE_URL` formatted for postgres. It doesn't need to be the URL of an actual database server though. Here's mine: `DATABASE_URL=postgres://does@not/mater`

Hopefully a more comprehensive guide will be written at some point, but for now feel free to reach out to the [mailing list](https://lists.sr.ht/~edwardloveall/scribe) if you have any questions.

### Docker (Unsupported)
@@ -23,7 +21,7 @@ ### Docker (Unsupported)
To run (generating a base config from environment variables):

```
$ docker run -it --rm -p 8080:8080 -e SCRIBE_PORT=8080 -e SCRIBE_HOST=0.0.0.0 -e SCRIBE_DB=postgres://does@not/matter scribe:latest
$ docker run -it --rm -p 8080:8080 -e SCRIBE_PORT=8080 -e SCRIBE_HOST=0.0.0.0 scribe:latest
```

To run with mounted config from local fs:
@@ -42,8 +40,6 @@ ### Configuration

* PORT: The port Scribe should run on
* SECRET_KEY_BASE: A 32-bit string. Can be generated with `lucky gen.secret_key`
* DATABASE_URL: May be any valid postgres url since Scribe doesn't use a database
  * Example: `postgres://does@not/matter`
* GITHUB_PERSONAL_ACCESS_TOKEN: to proxy gists with authenticated GitHub API requests
* GITHUB_USERNAME: to proxy gists with authenticated GitHub API requests

diff --git a/config/database.cr b/config/database.cr
index 6ab8be0..b4e751b 100644
--- a/config/database.cr
+++ b/config/database.cr
@@ -1,24 +1,10 @@
database_name = "scribe_#{LuckyEnv.environment}"
class UnusedDB < Avram::Database
end

AppDatabase.configure do |settings|
  if LuckyEnv.production?
    settings.credentials = Avram::Credentials.parse(ENV["DATABASE_URL"])
  else
    settings.credentials = Avram::Credentials.parse?(ENV["DATABASE_URL"]?) || Avram::Credentials.new(
      database: database_name,
      hostname: ENV["DB_HOST"]? || "localhost",
      port: ENV["DB_PORT"]?.try(&.to_i) || 5432,
      username: ENV["DB_USERNAME"]? || "postgres",
      password: ENV["DB_PASSWORD"]? || "postgres"
    )
  end
UnusedDB.configure do |settings|
  settings.credentials = Avram::Credentials.void
end

Avram.configure do |settings|
  settings.database_to_migrate = AppDatabase
  settings.lazy_load_enabled = LuckyEnv.production?

  # Always parse `Time` values with these specific formats.
  # Used for both database values, and datetime input fields.
  # settings.time_formats << "%F"
  settings.database_to_migrate = UnusedDB
end
diff --git a/script/docker_entrypoint b/script/docker_entrypoint
index 718a309..6326e70 100755
--- a/script/docker_entrypoint
+++ b/script/docker_entrypoint
@@ -1,4 +1,4 @@
#!/bin/sh

echo -e "port: ${SCRIBE_PORT}\nhost: ${SCRIBE_HOST}\ndatabase: ${SCRIBE_DB}" > ./config/watch.yml
echo -e "port: ${SCRIBE_PORT}\nhost: ${SCRIBE_HOST}" > ./config/watch.yml
./start_server
diff --git a/src/app.cr b/src/app.cr
index 1d7e49b..9cc1b56 100644
--- a/src/app.cr
+++ b/src/app.cr
@@ -6,7 +6,6 @@ Lucky::AssetHelpers.load_manifest "public/mix-manifest.json"
require "./version"
require "../config/server"
require "../config/**"
require "./app_database"
require "./constants"
require "./models/base_model"
require "./models/mixins/**"
diff --git a/src/app_database.cr b/src/app_database.cr
deleted file mode 100644
index 0efd4f5..0000000
--- a/src/app_database.cr
@@ -1,2 +0,0 @@
class AppDatabase < Avram::Database
end
-- 
2.32.1 (Apple Git-133)
Thanks so much, Michael! I didn't know about Avram::Credentials.void

Much less confusing, indeed.

Edward