Added index

This commit is contained in:
Thomas Forgione 2017-09-23 09:39:42 +00:00
parent d048eccbea
commit 6982704de3
No known key found for this signature in database
GPG Key ID: 95D964F74A96119E
10 changed files with 104 additions and 19 deletions

View File

@ -0,0 +1,4 @@
extends ../../../templates/base.pug
block content
p Login

5
controllers/auth/urls.js Normal file
View File

@ -0,0 +1,5 @@
const url = require('create-url').url;
module.exports = [
url('/login', 'login', 'login'),
]

View File

@ -0,0 +1,9 @@
const getUrl = require('create-url').getUrl;
module.exports.login = function(req, res, render) {
render('login.pug');
}
module.exports.logout = function(req, res, render) {
res.redirect(getUrl("index"));
}

View File

@ -0,0 +1,4 @@
extends ../../../templates/base.pug
block content
p Hello world

View File

@ -1,3 +1,3 @@
module.exports.index = function(req, res, render) { module.exports.index = function(req, res, render) {
res.send('Hello'); render('index.pug');
} }

View File

@ -1,6 +1,7 @@
const config = require('./settings/config.js'); const config = require('./settings/config.js');
require('app-module-path').addPath(config.BASE_DIR); require('app-module-path').addPath(config.BASE_DIR);
require('app-module-path').addPath(config.UTILS_DIR); require('app-module-path').addPath(config.UTILS_DIR);
require('app-module-path').addPath(config.CONTROLLERS_DIR);
const express = require('express'); const express = require('express');
const pug = require('pug'); const pug = require('pug');
@ -37,7 +38,7 @@ function main() {
app.use(require('create-url')); app.use(require('create-url'));
// Load controllers // Load controllers
require('controllers')(app); require('controllers')(app, config.CONTROLLERS_DIR);
// Static files // Static files
app.use('/static', express.static(config.STATIC_DIR)); app.use('/static', express.static(config.STATIC_DIR));

View File

@ -3,6 +3,7 @@ const join = require('path').join;
module.exports.BASE_DIR = join(__dirname, '..'); module.exports.BASE_DIR = join(__dirname, '..');
module.exports.UTILS_DIR = join(module.exports.BASE_DIR, 'utils'); module.exports.UTILS_DIR = join(module.exports.BASE_DIR, 'utils');
module.exports.STATIC_DIR = join(module.exports.BASE_DIR, 'static'); module.exports.STATIC_DIR = join(module.exports.BASE_DIR, 'static');
module.exports.CONTROLLERS_DIR = join(module.exports.BASE_DIR, 'controllers');
module.exports.DEBUG = true; module.exports.DEBUG = true;

48
templates/base.pug Normal file
View File

@ -0,0 +1,48 @@
doctype html
html
head
meta(charset='utf-8')
meta(name='viewport', content='width=device-width, initial-scale=1, shrink-to-fit=no')
block css
link(rel='stylesheet', href='/static/bootstrap/css/bootstrap.min.css')
style.
.navbar .btn {
margin-right: 10px;
}
body {
padding-top: 65px;
}
title ADEjs
body
nav.navbar.navbar-expand-lg.navbar-dark.bg-dark.fixed-top
.container
a.navbar-brand(href=getUrl("index")) ADEjs
button.navbar-toggler(type='button', data-toggle='collapse', data-target='#navbarResponsive', aria-controls='navbarResponsive', aria-expanded='false', aria-label='Toggle navigation')
span.navbar-toggler-icon
#navbarResponsive.collapse.navbar-collapse
ul.navbar-nav
if session.user !== undefined
li.nav-item
a.nav-link(href='#') Total
ul.navbar-nav.ml-auto
if session.user === undefined
li.nav-item
a.btn.btn-light(href=getUrl("login"), role='button')
| Sign in
li.nav-item
a.btn.btn-success(href='#', role='button')
| Sign up
else
li.nav-item
a.nav-link(href='#')
| Username
li.nav-item
a.btn.btn-light(href='#', role='button')
| Log out
.container
block content
script(src='/static/bootstrap/js/jquery.min.js')
script(src='/static/bootstrap/js/popper.min.js')
script(src='/static/bootstrap/js/bootstrap.min.js')
block extrajs

View File

@ -1,18 +1,18 @@
var express = require('express'); const express = require('express');
var fs = require('fs'); const fs = require('fs');
var log = require('log'); const log = require('log');
module.exports = function(app) { module.exports = function(app, controllersDir = __dirname + '/../controllers') {
log.debug("Loading controllers :"); log.debug("Loading controllers :");
fs.readdirSync(__dirname + '/../controllers').forEach(function(name) { fs.readdirSync(controllersDir).forEach(function(name) {
// views.js in controller, with function as pages (views.py for django) // views.js in controller, with function as pages (views.py for django)
var obj = require('./../controllers/' + name + '/views'); let obj = require(controllersDir + '/' + name + '/views');
// urls.js, just like django urls.py // urls.js, just like django urls.py
var urls = require('./../controllers/' + name + '/urls'); let urls = require(controllersDir + '/' + name + '/urls');
name = obj.name || name; name = obj.name || name;
// allow specifying the view engine // allow specifying the view engine
@ -24,10 +24,10 @@ module.exports = function(app) {
app.get(url.url, ((url) => function(req, res, next) { app.get(url.url, ((url) => function(req, res, next) {
var path = obj[url.view](req, res, function(view) { let path = obj[url.view](req, res, function(template) {
let viewPath = __dirname + '/../controllers/' + name + '/views/' + view; let templatePath = controllersDir + '/' + name + '/templates/' + template;
res.render(viewPath, res.locals, function(err, out) { res.render(templatePath, res.locals, function(err, out) {
if (err !== null) { if (err !== null) {
log.pugerror(err); log.pugerror(err);
} }

View File

@ -1,17 +1,30 @@
let urls = {}; let urls = {};
class Url {
constructor(url, view, name) {
this.url = url;
this.view = view;
this.name = name;
}
toString() {
return this.url;
}
}
function getUrl(name) {
return urls[name].url;
}
module.exports = function(req, res, next) { module.exports = function(req, res, next) {
res.locals.urls = urls; res.locals.getUrl = getUrl;
return next(); return next();
} }
module.exports.url = function(url, viewName, urlName) { module.exports.getUrl = getUrl;
let ret = {
url: url,
view: viewName,
name: urlName,
};
module.exports.url = function(url, viewName, urlName) {
let ret = new Url(url, viewName, urlName);
urls[urlName] = ret; urls[urlName] = ret;
return ret; return ret;
} }