Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

English translation #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions en/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
6 changes: 6 additions & 0 deletions en/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
npm-debug.log
build/
coverage/
node_modules/
package-lock.json
config.json
21 changes: 21 additions & 0 deletions en/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) VarnaLab <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
151 changes: 151 additions & 0 deletions en/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@

# varnalab-static

Using http://mustache.github.io/ logic-less templates.

## Install

```bash
# install Node Version Manager
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.4/install.sh | bash

# install Node 8
nvm install 8

# clone this repo
git clone https://github.com/VarnaLab/varnalab-static.git

# enter project's folder
cd varnalab-static

# install deps
npm install

# create config.json
cp config.json.example config.json

# make changes to config.json with your favorite editor
$EDITOR config.json

# render in the current folder
node bin/ --config config.json --env minimum-render-config-on-localhost --render ./build/

# serve the static files using NodeJS
node bin/ --config config.json --env minimum-server-config-on-localhost --server ./build/

# navigate to http://localhost:3000 in your favorite browser
```

## Config

```json
{
"production": {
"url": {
"scheme": "https",
"host": "varnalab.org",
"path": "",
"api": "https://box.outofindex.com/api/varnalab"
},
"server": {
"assets": "/home/varnalab/projects/varnalab-static",
"port": 5050
},
"fs": {
"articles": "/path/to/articles.json",
"events": "/path/to/events.json",
"members": "/path/to/users.json",
"cashbox": "/path/to/invbg.json"
},
"git": {
"repo": "/home/s/projects/varnalab-static",
"remote": "varnalab",
"branch": "master",
"secret": "..."
}
}
}
```

- `url` ___(required)___
- used to generate relative paths for the templates
- used to generate absolute paths for server-side meta tag rendering
- the api endpoint is used for rendering when the `fs` key is not present, and inside the whois widget
- `server` _(optional)_
- used by the built-in static nodejs server
- otherwise use the nginx config below to serve the static content
- `fs` _(optional)_
- used to load the dynamic data locally. Renders only when any of the files is modified in the last 10mins. Use the `--force` flag to force the render.
- otherwise falls back to making HTTP requests to the varnalab-api
- `git` _(optional)_
- githook configuration for the varnalab-static repo
- used only in production

## Render

```bash
node varnalab-static/bin/ \
--config /path/to/config.json \
--env production \
--render /path/to/build/location/
```

## Server

```bash
node varnalab-static/bin/ \
--config /path/to/config.json \
--env production \
--server /path/to/build/location/
```

## NginX Router

```nginx
location ~ /api/?(.*)/? {
set $endpoint $1;
proxy_pass http://127.0.0.1:5050/api/$endpoint;
}

location ~ /(css|js|images)/(.*) {
root /home/varnalab/projects/varnalab-static;
try_files /$1/$2 =404;
}

root /home/varnalab/config/varnalab-static;

location ~ /about {
try_files /build/about.html =404;
}
location ~ /events/(\d+) {
try_files /build/events/$1.html =404;
}
location ~ /events {
try_files /build/events.html =404;
}
location ~ /blogs/(\d+)/(\d+)/(\d+)/(.+) {
try_files /build/articles/$4.html =404;
}
location ~ /blogs {
try_files /build/articles.html =404;
}
location ~ /members {
try_files /build/members.html =404;
}
location ~ /links {
try_files /build/links.html =404;
}
location ~ /contacts {
try_files /build/contacts.html =404;
}
location ~ /finance {
try_files /build/finance.html =404;
}
location ~ /donate {
try_files /build/donate.html =404;
}
location ~ / {
try_files /build/home.html =404;
}
```

46 changes: 46 additions & 0 deletions en/bin/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

var fs = require('fs')
var transform = require('./transform')()


// varnalab-api
var upcoming = (events) => {
var now = new Date().getTime()
var upcoming = []

for (var event of events) {
var end = new Date(event.end_time || event.start_time).getTime()
if (end >= now) {
upcoming.push(event)
}
else {
break
}
}

return upcoming
.sort((a, b) => new Date(a.start_time) - new Date(b.start_time))
}


module.exports = (config, force) => {

var modified = Object.keys(config.fs)
.some((file) => fs.statSync(config.fs[file]).mtimeMs > Date.now() - (1000 * 60 * 10))

if (modified || force) {
var articles = require(config.fs.articles)
var events = require(config.fs.events)
var members = require(config.fs.members)
var cashbox = require(config.fs.cashbox)

return {
upcoming: transform.events(upcoming(events)),
events: transform.events(
events.filter((event) => new Date(event.start_time) < new Date())),
articles: transform.articles(articles),
members,
cashbox: transform.cashbox(cashbox),
}
}
}
81 changes: 81 additions & 0 deletions en/bin/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

var cp = require('child_process')
var crypto = require('crypto')
var express = require('express')


var verify = (signature, payload, secret) =>
crypto.timingSafeEqual(
Buffer.from(signature),
Buffer.from('sha1=' +
crypto.createHmac('sha1', secret).update(payload).digest('hex')
)
)


module.exports = (config) => {
var api = express()

api.use((req, res, next) => {
var error = new Error()
error.code = 400

var sig = req.headers['x-hub-signature']
var event = req.headers['x-github-event']
var id = req.headers['x-github-delivery']

if (!sig) {
error.message = 'No X-Hub-Signature found on request'
next(error)
return
}

if (!event) {
error.message = 'No X-Github-Event found on request'
next(error)
return
}

if (!id) {
error.message = 'No X-Github-Delivery found on request'
next(error)
return
}

if (event !== 'push') {
error.message = 'X-Github-Event is not acceptable'
next(error)
return
}

if (!verify(sig, JSON.stringify(req.body), config.git.secret)) {
error.message = 'X-Hub-Signature does not match blob signature'
next(error)
return
}

next()
})

api.post('/git/pull', (req, res, next) => {
var cmd = [
'cd', config.git.repo, '&& git pull', config.git.remote, config.git.branch,
].join(' ')

cp.exec(cmd, (err) => {
if (err) {
next(err)
return
}
res.end('OK')
})
})

api.use((err, req, res, next) => {
console.error(err.message)
console.error(err.stack)
res.status(400).end(err.message)
})

return api
}
50 changes: 50 additions & 0 deletions en/bin/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

var request = require('@request/client')
var purest = require('purest')({request, promise: Promise})
var transform = require('./transform')()


module.exports = (config) => {
var varnalab = purest({
provider: 'varnalab',
config: {
varnalab: {
[config.url.api]: {'{endpoint}': {__path: {alias: '__default'}}}
}
}
})

var upcoming = () =>
varnalab
.get('events/upcoming')
.request()
.then(([res, body]) => transform.events(body))

var events = () =>
varnalab
.get('events')
.qs({limit: 100000})
.request()
.then(([res, body]) => transform.events(
body.filter((event) => new Date(event.start_time) < new Date())))

var members = () =>
varnalab
.get('whois/known')
.request()
.then(([res, body]) => body)

var articles = () =>
varnalab
.get('articles')
.request()
.then(([res, body]) => transform.articles(body))

var cashbox = () =>
varnalab
.get('finance/invbg/cashbox')
.request()
.then(([res, body]) => transform.cashbox(body))

return {upcoming, events, members, articles, cashbox}
}
Loading