Skip to content

Commit

Permalink
added README.md, some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
halilcakar committed Jun 5, 2020
1 parent 8635b2c commit 5e0aabb
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .env.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
APP_ENV=local
HOST=localhost
PORT=3000
SOCKET_PORT=8080

#APP_END=production
#SSL_KEY=
#SSL_CERT=
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# private-chat

This is a small application for private chat. 1 Room can only have 2 people inside.

It uses `express, socket.io, ejs`.

## How to use

first of all clone the repo

`git clone https://github.com/halilcakar/private-chat.git && cd private-chat`

and then install

`npm i`

then you need to copy .env.default to .env. bash script might be differen for you i'm using git bash generally so

`cp .env.default .env`

after these steps you can just run

`npm run dev` or `yarn dev`

I also added an option for SSL usage, if you have a generated ssl that you can use, you need to set the `SSL_KEY` and `SSL_CERT` parameters on `.env`

If you set those and make `APP_ENV=production` it will try to run the server on `https`. But for local testing you can just leave `.env` as it is.

## Thanks to:
[favicon.io](https://favicon.io/) that provides that beautiful favicons.


## Some photos :)
<img src="./public/images/1.png">
<img src="./public/images/2.png">
<img src="./public/images/3.png">


Binary file added public/images/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 18 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
const { host, PORT, SOCKET_PORT, APP_ENV } = require('./src/config');
const { host, PORT, SOCKET_PORT, APP_ENV, SSL } = require('./src/config');
console.clear();
const { UUID, Last } = require('./src/functions');
const express = require('express')
const path = require('path')
const app = express();
const server = require('http').createServer(app);


let protocol = 'http';
let options = {};
if (APP_ENV === 'production') {
protocol = 'https';
options = {
key: fs.readFileSync(SSL.KEY),
cert: fs.readFileSync(SSL.CERT)
};
}

const server = require(protocol).createServer(options, app);
server.listen({ host, port: SOCKET_PORT }, () => {
console.log(`HTTP server listening on http://${host}:${SOCKET_PORT}`)
console.log(`Server listening on ${protocol}://${host}:${SOCKET_PORT}`)
});

const io = require('socket.io')(server);


app.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res) => res.render('pages/index', { url: APP_ENV === 'local' ? `http://${host}:${SOCKET_PORT}` : `window.location.href` }))
.listen(PORT, () => console.log(`Listening on http://${host}:${PORT}`))
.get('/', (req, res) => res.render('pages/index', { url: `${protocol}://${host}:${SOCKET_PORT}` }))
.listen(PORT, () => console.log(`Listening on ${protocol}://${host}:${PORT}`))

const channels = [];

Expand Down
7 changes: 6 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ const host = process.env.HOST;
const PORT = process.env.PORT || 3000;
const SOCKET_PORT = process.env.SOCKET_PORT || 8080;

const SSL = {
KEY: process.env.SSL_KEY,
CERT: process.env.SSL_CERT,
}

module.exports = {
APP_ENV,
host,
PORT,
SOCKET_PORT
SOCKET_PORT,
SSL
}
7 changes: 5 additions & 2 deletions src/functions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var UUID = function () {
function UUID() {
return 'xxxx-yxxx-4xxx-yxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0;
var v = (c === 'x') ? r : (r & 0x3 | 0x8);
Expand All @@ -7,7 +7,10 @@ var UUID = function () {
});
};

var Last = function (arr) {
function Last(arr) {
if (Array.isArray(arr)) {
return null;
}
return arr[arr.length - 1];
}

Expand Down

0 comments on commit 5e0aabb

Please sign in to comment.