Skip to content

Commit

Permalink
Start implementing API
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb committed May 31, 2019
1 parent 3d9f71c commit 5a3f90a
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 36 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

If you are a collaborator (with push permissions), you can merge any open PR with the following conditions:

1. It passes the JSON validity test (this is a github integration in travis)
1. It passes the JSON validity test (this is a GitHub integration in Travis CI)
2. Merge anything that adds or updates a `<user>.json` file

If you're unsure, cc @remy into the PR with a question and we can work out what to do.

The site is hosted on heroku and will automatically deploy merges into master, which means once a PR is merged, it'll be live shortly thereafter (so there's nothing to do 🎉).
The site is hosted on Heroku and will automatically deploy merges into master, which means once a PR is merged, it'll be live shortly thereafter (so there's nothing to do 🎉).

Also, thank you, your help is appreciated 💙
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
},
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@octokit/rest": "^16.27.3",
"btoa": "^1.2.1",
"compression": "^1.7.4",
"dayjs": "^1.8.14",
"ejs": "^2.6.1",
Expand Down
36 changes: 34 additions & 2 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import ejs = require('ejs')
import { yearNow, stripTags, trimArray } from './util'
import HTML = require('node-html-parser')
import postcssMiddleware = require('postcss-middleware')
import btoa = require('btoa')

const github = require("@octokit/rest")({
// GitHub personal access token
auth: process.env.github_token,

// User agent with version from package.json
userAgent: `mit-license v${require('./package.json').version}`,
})

// Prepare application
const app = express()
Expand All @@ -35,9 +44,32 @@ app.use((_req, res, next) => {
next()
})

// Parse URL-encoded bodies (as sent by HTML forms)
// app.use(express.urlencoded({ extended: true }))
//
// Parse JSON bodies (as sent by API clients)
app.use(express.json())

// HTTP POST API
app.post('/', (_req, res) => {
res.end()
app.post('/', (req, res) => {
// Get differnet parts of hostname (example: remy.mit-license.org -> ['remy', 'mit-license', 'org'])
const params = req.hostname.split(".")

console.log(req.body)

// If there isn't enough part of the hostname
if (params.length < 2) res.status(400).send("Please specify a subdomain in the URL.")

res.json(req.body)
return

github.repos.createFile({
owner: "remy",
repo: "mit-license",
path: `users/${params[0]}.json`,
message: `Automated creation of user ${params[0]}.`,
content: btoa()
})
})

// Any other HTTP GET request
Expand Down
8 changes: 5 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#!/bin/env node

const path = require('path')
const fs = require('fs')
const users = fs.readdirSync('users')
users.forEach(function(user) {
users.forEach(async user => {
try {
const content = fs.readFileSync('users/' + user).toString()
const content = fs.readFileSync(path.join('users', user), "utf8")
JSON.parse(content)
} catch (e) {
console.error('Invalid JSON in file: ' + user)
console.error(`Invalid JSON in file: ${user} (${e})`)
process.exit(1)
}
})
console.log("All JSON valid!")
8 changes: 8 additions & 0 deletions users/localhost.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"copyright": "Richie Bendall, https://www.richie-bendall.ml",
"url": "https://www.richie-bendall.ml",
"email": "richiebendall@gmail.com",
"format": "html",
"gravatar": true,
"theme": "material-indigo"
}
6 changes: 3 additions & 3 deletions users/lucasmartins.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"copyright": "Lucas N Martins",
"url": "http://www.linkedin.com/in/martinslucas",
"theme": "double-windsor"
"copyright": "Lucas N Martins",
"url": "https://www.linkedin.com/in/martinslucas",
"theme": "double-windsor"
}
1 change: 0 additions & 1 deletion users/lucasmartins.json-old

This file was deleted.

14 changes: 7 additions & 7 deletions users/richie-bendall.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"copyright": "Richie Bendall, https://www.richie-bendall.ml",
"url": "https://www.richie-bendall.ml",
"email": "richiebendall@gmail.com",
"format": "html",
"gravatar": true,
"theme": "material-indigo"
}
"copyright": "Richie Bendall, https://www.richie-bendall.ml",
"url": "https://www.richie-bendall.ml",
"email": "richiebendall@gmail.com",
"format": "html",
"gravatar": true,
"theme": "material-indigo"
}
14 changes: 7 additions & 7 deletions users/richiebendall.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"copyright": "Richie Bendall, https://www.richie-bendall.ml",
"url": "https://www.richie-bendall.ml",
"email": "richiebendall@gmail.com",
"format": "html",
"gravatar": true,
"theme": "material-indigo"
}
"copyright": "Richie Bendall, https://www.richie-bendall.ml",
"url": "https://www.richie-bendall.ml",
"email": "richiebendall@gmail.com",
"format": "html",
"gravatar": true,
"theme": "material-indigo"
}
12 changes: 6 additions & 6 deletions users/richienb.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"copyright": "Richie Bendall, https://www.richie-bendall.ml",
"url": "https://www.richie-bendall.ml",
"email": "richiebendall@gmail.com",
"format": "html",
"gravatar": true,
"theme": "material-indigo"
"copyright": "Richie Bendall, https://www.richie-bendall.ml",
"url": "https://www.richie-bendall.ml",
"email": "richiebendall@gmail.com",
"format": "html",
"gravatar": true,
"theme": "material-indigo"
}
Loading

0 comments on commit 5a3f90a

Please sign in to comment.