An API to retrieve licenses for your project.
Simple express API with a postgres DB backend to store the license information. Might eventually convert it to a sqlite DB and run it all in a single container or maybe set it up as a pair of docker containers or something.
The idea is that there could be simple command line apps to make a fetch a license for an existing project, or project generators could call the license api and include a license as part of project generation.
-
Requires:
- NodeJS > version 8.
- a PostgreSQL instance.
-
Run the three scripts in the SQL folder against your postgres instance.
- Make sure the password you set in the createNodeUser script matches the one in config/dbConfig.json.
psql -a -U postgres -W -f 001createNodeUser.sql
- create the node user.psql -a -U postgres -W-f 002createDB.sql
- create the database.psql -d licensedata -U postgres -W -a -f 003createTable.sql
- create the table and schema in the new database.
-
you can either run a web scraper or use the json data file to set up the DB data.
- run the scraper to pull the license data from https://choosealicense.com/.
node tools/scraper.js
- scrapes the data and puts it into the postgres table. - The preferred way would be to run the json_to_db tool.
s
node tools/json_to_db.js
- uses the json file indata/full_license_info.json
to populate the database.
- run the scraper to pull the license data from https://choosealicense.com/.
-
start a development server and test.
npm run dev
- start the development server on port 3000.- use curl, powershell, insomnia, postman or any other tool to check it is running and sending data.
- Get http://localhost:3000/license to get an index of licenses and a url for each.
- Get http://localhost:3000/license/:license_name: to get the individual license text and details.
- Get parameters like http://localhost:3000/license/:license_name:?name=:your_name:&email=:your_email: can be used to populate the email and fullName fields in the license text.
-
Bash and Python:
curl -s 'http://localhost:3000/license/mit' | \ python3 -c "import sys, json; print(json.load(sys.stdin)['license_text'])" > LICENSE.md
-
Powershell:
invoke-restmethod -uri 'http://localhost:3000/license/apache-2.0' | select-object -expandproperty license_text | out-file LICENSE.md
-
Node/JS:
const https = require('https') const fs = require('fs') let licenseUrl = 'https://os-license-api.herokuapp.com/license/mit' https.get(licenseUrl, res => { let apiData = [] res.on('data', (dataChunk) => { apiData.push(dataChunk) }) res.on('end', () => { let data = JSON.parse(apiData.join('')) try { fs.writeFileSync('./license.md', data.licenseText) } catch (err) { console.error(err) } }) })
-
Integrated with tooling. For example,
NPM init
could pull down the relevent license during the init process.