diff --git a/docker-compose.yaml b/docker-compose.yaml index ecc369a..de02e4a 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -6,10 +6,10 @@ services: container_name: mainapp ports: - "3000:3000" - networks: - - nodeapp-network environment: REDISURL: redis + DATABASE_HOST: postgres + DATABASE_SSL: false depends_on: - postgres - redis @@ -20,20 +20,14 @@ services: ports: - "5432:5432" environment: - POSTGRES_DB: data + POSTGRES_DB: postgres POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres - networks: - - nodeapp-network + volumes: + - ./init.sql:/docker-entrypoint-initdb.d/init.sql redis: image: redis:alpine container_name: redis ports: - "6379:6379" - networks: - - nodeapp-network - -networks: - nodeapp-network: - driver: bridge diff --git a/init.sql b/init.sql new file mode 100644 index 0000000..53c13da --- /dev/null +++ b/init.sql @@ -0,0 +1,4 @@ +CREATE SCHEMA IF NOT EXISTS emma_test; + +-- Switch to the "emma_test" schema +SET search_path TO emma_test; \ No newline at end of file diff --git a/src/routes/root.ts b/src/routes/root.ts index 9e964a0..5cf3682 100644 --- a/src/routes/root.ts +++ b/src/routes/root.ts @@ -7,6 +7,26 @@ import config from '../config' const logger = customLogger(path.basename(__filename)) module.exports = app => { //:TODO need to secure these apis too. + const cache = app.get('redisCache') + const db = app.get('db') + const { sequelize, Sequelize } = db + + // Todo table + const Todo = sequelize.define('Todo', { + title: { + type: Sequelize.STRING, + }, + description: { + type: Sequelize.TEXT, + }, + }); + + Todo.sync().then(() => { + logger.info("Todo Model synced"); + }).catch((err) => { + logger.error(`Error in Todo: ${err}`) + }); + app.use(function (req, res, next) { req.realmName = req.headers.realmname req.redisKey = config.redis.serviceName @@ -21,6 +41,7 @@ module.exports = app => { // } }) app.use(checkCache) + app.get('/', (req, res, next) => { try { req.dbData = `Hello world!` @@ -30,4 +51,62 @@ module.exports = app => { logger.error(`Error in get ${e}`) } }) + + app.post('/todo', async (req, res) => { + try { + const { title, description } = req.body; + const todo = await Todo.create({ + title, + description, + }); + + res.json(todo); + } catch (error) { + console.error(error); + res.status(500).json({ error: 'Internal Server Error' }); + } + }); + + // Route to get all TODO items + app.get('/todo', async (req, res) => { + try { + const key = 'todos_all'; + const cachedData = await cache.get(key) + if (cachedData !== null) { + logger.info(`Cache Hit for ${key}`); + res.json(JSON.parse(cachedData)); + } else { + logger.info(`Cache Miss for ${key}`) + const todos = await Todo.findAll(); + cache.set(key, JSON.stringify(todos), 60); + res.json(todos); + } + } catch (error) { + console.error(error); + res.status(500).json({ error: 'Internal Server Error' }); + } + }); + + // Route to get Todo by Id + app.get('/todo/:id', async (req, res) => { + try { + const { id } = req.params; + const key = `todos_${id}` + const cachedData = await cache.get(key) + if (cachedData !== null) { + logger.info(`Cache Hit for ${key}`); + res.json(JSON.parse(cachedData)); + } else { + logger.info(`Cache Miss for ${key}`) + const todo = await Todo.findByPk(id); + cache.set(key, JSON.stringify(todo), 60); + res.json(todo); + } + + } catch (error) { + console.error(error); + res.status(500).json({ error: 'Internal Server Error' }); + } + }); + }