Add base app with i18n support

This commit is contained in:
Samuel Ortion 2022-08-17 14:27:37 +02:00
parent 87689761ea
commit daaf8524d7
15 changed files with 2104 additions and 76 deletions

81
.gitignore vendored
View File

@ -1,15 +1,9 @@
# ---> Node
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
@ -22,12 +16,11 @@ lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
@ -43,11 +36,8 @@ build/Release
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
@ -55,15 +45,6 @@ web_modules/
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
@ -73,60 +54,8 @@ web_modules/
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
# dotenv environment variables file
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
# next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

52
app.js Normal file
View File

@ -0,0 +1,52 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
const i18n = require('i18n-2');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
i18n.expressBind(app, {
locales: ['en', 'es', 'fr', 'de'],
defaultLocale: 'en',
cookieName: 'locale'
});
app.use(function(req, res, next) {
req.i18n.setLocaleFromQuery();
req.i18n.setLocaleFromCookie();
next();
});
app.use('/', indexRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;

90
bin/www Executable file
View File

@ -0,0 +1,90 @@
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('soundbirder:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

8
controllers/home.js Normal file
View File

@ -0,0 +1,8 @@
function getIndex(req, res, next) {
res.render('index', { title: 'SoundBirder' });
}
module.exports = {
getIndex,
}

1
locales/de.js Normal file
View File

@ -0,0 +1 @@
{}

4
locales/en.js Normal file
View File

@ -0,0 +1,4 @@
{
"Home": "Home",
"About": "About"
}

4
locales/es.js Normal file
View File

@ -0,0 +1,4 @@
{
"Home": "Home",
"About": "About"
}

1
locales/fr.js Normal file
View File

@ -0,0 +1 @@
{}

1830
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

18
package.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "soundbirder",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"axios": "^0.27.2",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"i18n-2": "^0.7.3",
"morgan": "~1.9.1",
"pug": "^3.0.2"
}
}

View File

@ -0,0 +1,49 @@
* {
box-sizing: border-box;
}
body {
padding: 0;
margin: 0;
}
.link {
position: relative;
display: inline-block;
padding: 5px 0;
text-decoration: none;
color: #fff;
}
.link:after {
content: '';
height: 1px;
display: block;
border-bottom: 2px dotted darkblue;
width: 0;
-webkit-transition: width 0.2s;
transition: width 0.2s;
}
.link:hover:after {
width: 100%;
}
.author a {
text-decoration: none;
cursor: pointer;
}
footer {
color: white;
background-color: black;
position: absolute;
text-align: center;
bottom: 0;
width: 100vw;
padding: 2rem;
}
footer .link:after {
border-bottom: 2px dotted white;
}

8
routes/index.js Normal file
View File

@ -0,0 +1,8 @@
var express = require('express');
var router = express.Router();
var homeController = require('../controllers/home');
/* GET home page. */
router.route('/')
.get(homeController.getIndex);
module.exports = router;

6
views/error.pug Normal file
View File

@ -0,0 +1,6 @@
extends layout
block content
h1= message
h2= error.status
pre #{error.stack}

4
views/index.pug Normal file
View File

@ -0,0 +1,4 @@
extends layout
block content

24
views/layout.pug Normal file
View File

@ -0,0 +1,24 @@
doctype html
html
head
title= title
meta(name="viewport", content="width=device-width, initial-scale=1.0")
link(rel='stylesheet', href='/stylesheets/style.css')
body
header
h1= title
nav
ul
li
a(href='/') #{ __('Home') }
li
a(href='/about') #{ __('About') }
li
a(href='/contact') Contact
block content
footer
.description
.copyright Copyright © 2022 -
span.author
a(href="//samuel.ortion.fr", class="link") Samuel ORTION