joy-framework / joy

A full stack web framework written in janet
https://joy.swlkr.com
MIT License
535 stars 30 forks source link

PostgreSQL only works with ENV variable #48

Closed katafrakt closed 4 years ago

katafrakt commented 4 years ago

Right now when you put DATABASE_URL for PostgreSQL in .env it won't work, because db will still try to load sqlite3. This is because db checks for a real ENV variable to determine database type and dotenv loader does not set real variable, I think. Perhaps it should? Or perhaps there should be some mandatory setup step in db where you pass an URL?

swlkr commented 4 years ago

Ahhh, I added an optional arg on the sqlite side, but left out the postgres side 🤦‍♂️

I didn't want to "pollute" everyone's environment when they start up joy, but maybe it doesn't matter?

I did do this here:

(defn load []
  (let [dict (with [f (file/open ".env")]
               (-> (file/read f :all)
                   (parse-dotenv)))]
    (loop [[k v] :pairs dict]
      (os/setenv k v))))

and you can use it like:

(import dotenv)
(dotenv/load)

I haven't added dotenv to joy yet, but it's coming soon.

In the meantime, I duplicated your work because I didn't see it, but then merged instead 😅

swlkr commented 4 years ago

One of the downsides of multiple repos

katafrakt commented 4 years ago

This is not exactly related issue. I have this .env:

ENCRYPTION_KEY=[blah]
JOY_ENV=development
DATABASE_URL=postgres://localhost/database

And when I joy server I get this:

error: unable to open database file
  in sqlite3/open
  in connect [/home/katafrakt/.janet/db/sqlite/db.janet] on line 12, column 26
  in start [src/server.janet] (tailcall) on line 25, column 5
  in _thunk [main.janet] on line -1, column -1
  in cli-main [boot.janet] on line 2446, column 35

Sorry, I should have put the details in the original comment.

But if you plan to add dotenv to Joy then this will probably resolve itself. Meanwhile I can work on manually setup env vars or direnv anyway.

swlkr commented 4 years ago

I wound up doing two things:

  1. Adding an optional argument to (db/connect) like this:
(db/connect (env :database-url))
  1. Adding dotenv/load to the server.janet file in the template like this:
(import dotenv)
(dotenv/load)

(import joy :prefix "")

... rest of code goes here

Hopefully this solves the problem 🤞

swlkr commented 4 years ago

I'm going to close this since (db/connect (env :database-url)) should do it