DEV Community 👩‍💻👨‍💻

Sokhavuth TIN
Sokhavuth TIN

Posted on • Updated on

Blog Engine with Fresh: Environment Variable & Database


GitHub: https://github.com/Sokhavuth/deno-fresh
Deno Deploy: https://khmerweb-fresh.deno.dev/


// setting.js

function setting(){
    const configure = {
        site_title: "Multimedia",
        page_title: "Home",
        message: "",
    }

    return configure
}


import { config } from "config";
await config({export: true});
const secret_key = Deno.env.get("SECRET_KEY");


import { MongoClient } from "mongodb";
const client = await new MongoClient();
await client.connect(Deno.env.get('DATABASE_URI'));
const mydb = client.database(Deno.env.get('DB_NAME'));


import { connect } from "redis"
const myredis = await connect({
    hostname: Deno.env.get('REDIS_URI'),
    port: parseInt(Deno.env.get('REDIS_PORT')),
    password: Deno.env.get('REDIS_PASSWORD'),
});


export { setting, secret_key, mydb, myredis }
Enter fullscreen mode Exit fullscreen mode
// import_map.json
{
  "imports": {
    "$fresh/": "https://deno.land/x/fresh@1.0.2/",
    "preact": "https://esm.sh/preact@10.10.0",
    "preact/": "https://esm.sh/preact@10.10.0/",
    "preact-render-to-string": "https://esm.sh/preact-render-to-string@5.2.1?external=preact",
    "setting": "./setting.js",
    "config": "https://deno.land/std@0.147.0/dotenv/mod.ts",
    "jwt": "https://deno.land/x/djwt@v2.2/mod.ts",
    "mongodb": "https://deno.land/x/mongo@v0.30.1/mod.ts",
    "redis": "https://deno.land/x/redis@v0.26.0/mod.ts",
    "bcrypt": "./bcrypt.ts",
    "cookies": "https://deno.land/std@0.150.0/http/cookie.ts"
  }
}
Enter fullscreen mode Exit fullscreen mode
// .env

SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DATABASE_URI=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
DB_NAME=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
REDIS_URI=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
REDIS_PASSWORD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
REDIS_PORT=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

🤔 Did you know?

 
🌚 You can turn on dark mode in Settings