Skip to content
Permalink
Browse files

Start implementing API

  • Loading branch information...
Richienb committed May 31, 2019
1 parent 3d9f71c commit 5a3f90adc302f25fa28983b701483ae70ff86221
Showing with 273 additions and 36 deletions.
  1. +2 −2 CONTRIBUTING.md
  2. +2 −0 package.json
  3. +34 −2 server.ts
  4. +5 −3 test.js
  5. +8 −0 users/localhost.json
  6. +3 −3 users/lucasmartins.json
  7. +0 −1 users/lucasmartins.json-old
  8. +7 −7 users/richie-bendall.json
  9. +7 −7 users/richiebendall.json
  10. +6 −6 users/richienb.json
  11. +199 −5 yarn.lock
@@ -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 💙
@@ -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",
@@ -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()
@@ -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
@@ -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!")
@@ -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"
}
@@ -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"
}

This file was deleted.

@@ -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"
}
@@ -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"
}
@@ -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"
}

0 comments on commit 5a3f90a

Please sign in to comment.
You can’t perform that action at this time.