Spartan
Spartan is the complete API and Database Layer that the Tilas Publishing Platform is built on. Lantern (the CMS platform) and Titan (The Website Platform) both speak to it via REST webservices to provide all the features of the end to end publishing and blogging frontends.
Settings up Spartan on your Server:
Prerequisites:
- DB: you need a new and empty MongoDB database setup and accessible to Spartan via a url like "mongodb://db-server-name/new-db-name". This can be on your same server, a Docker container or similar so long as it's accessible via the mongodb:// protocal to Spartan.
- AWS S3: All assets are stored in a AWS S3 bucket so you need a new S3 bucket set up. You then need to set up a AWS API access Key and Secret. Note does your bucket name, access key and secret for use below.
Spartan Software Installation (Source Setup)
For below example, let assume our new app is called "funnycats"
- Create a new folder and name it funnycats
- Inside the funnycats folder, Clone / Copy the latest Spartan project source
- To install all dependencies, run:
npm install
Environment Variables:
Environment Variables - Development: Open "scripts/development.sh" and enter your details from the prerequisites section to the NodeJS environment variables. Specifically MONGO_URL, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and S3_BUCKET. You can then fill up the remaining variables as needed.
Environment Variables - Staging/Production: You can edit the "scripts/staging.sh" or "scripts/production.sh" with your staging/production details or if you use Heroku or Docker/Dokku you will need to export these vars into your server as per their required way.
Export your environmental variable into your current session. run:
. scripts/development.sh
DB Setup:
- Next we need to execute the DB Setup script, which will configure your empty MongoDB database with the Spartan schema. Run this command:
npm run dbsetup
If everything worked you should see a success message like so: "Success: DB system has been set up."
Run the Tests:
- Run your tests to see if all your DB to API bindings work, run this:
npm test
All the tests should pass.
Updating server ports (optional step; if not done it will default to port 4000):
- Open your config/default.json, config/development.json, config/staging.json, config/production.json files and update any specific ports if needed.
- Or export a PORT environmental variable to overwrite all of these.
Start your app:
Launch your app like so:
npm start
Open your browser and navigate to app url:port, e.g. http://localhost:4000
Read the documentation:
- Go to http://localhost:3000/docs and familarise yourself with the API docs.
SPARTAN IS NOW UP AND READY TO SERVE BOTH TITAN AND LANTERN. YOUR NEXT STEP WOULD BE TO SETUP TITAN.
Development Tips
Debugging: To effectively debug your nodeJS app, I prefer to use node-inspector which pretty much lets your perform JavaScript debugging like you are used to in Chrome Dev Tools. To debug this app using node-inspector do this:
$ node --debug app.js // start your app in debug mode
$ node --debug-brk app.js // or to to pause script on first line do this
Now:
$ node-inspector & //launch the inspector
Read more here : https://greenido.wordpress.com/2013/08/27/debug-nodejs-like-a-pro/
Logging This app uses Bunyan for logging. This is a very advanced logging system that stores machine readable logs if needed. When the app runs, user friendly logs are spit out on the console to log our all trace (info and above), but it can be setup to only log to a file instead of the console or log on warns and above. There are more advanced use cases as well.
To prob apps in production, more here : https://www.joyent.com/blog/node-js-in-production-runtime-log-snooping
Read more here : https://github.com/trentm/node-bunyan
Collection, Schemas and Mappings:
Content Objects
Content Objects are basically the content data objects that populate the website, i.e. these objects end up being individual content web pages.
Collections
The DB has the following Collections:
settings
Here is where all the API runtime settings are stored.
authors
Here is where all your content author names are stored.
tags
Here is where all your content tag names are stored.
lists
Here is where we store the editorial lists like trending and popular.
assets
All images assets are stored here. Assets are used by Stories.
stories
Stories are the Content Objects, i.e the end up being the web pages you see in the website or they are used to create the content of the web pages. There are 2 types of stories: Articles and Tiles. Articles are your standard content articles/blog posts and Tiles are basically stand alone images that is read as a story. For e.g. Quote Images or Meme images are tiles.
Content Object Schema
The below describes the database Schema of Tiles, Articles and Assets.
Tile:
"_id" : ObjectId("")
"cat" : 1 // category
"oTy" : 1 // objType
"cOn" : 1435888483592 // createdOn
"uOn" : 0 // updatedOn
"sId" : "N1go943D" // shortId
"sTi" : "things..." // storyTitle
"txt" : "<p>You" // text
"atr" : 0 // author
"tgP" : 1 // tagPrimary
"tgs" : [11,18] // tags
"tgM" : [15,16] // tagsMeta
"iUl" : "https://PVEP.jpg" // imgUrl
"src" : "[text](link)" // source
"pUl" : "/a/N1go943D/asdas" // publicUrl
"aSh" : 0 // autoSharable
"fbS" : 0 // fbSharedOn
"pub" : 1 // published
Article
"_id" : ObjectId("")
"cat" : 1 // category
"oTy" : 1 // objType
"cOn" : 1435888483592 // createdOn
"uOn" : 0 // updatedOn
"sId" : "N1go943D" // shortId
"sTi" : "things..." // storyTitle
"blb" : "Le..." // blob
"txt" : "<p>Your html test" // text
"atr" : 0 // author
"tgP" : 1 // tagPrimary
"tgs" : [11,18] // tags
"tgM" : [15,16] // tagsMeta
"iUl" : "https://PVEP.jpg" // imgUrl
"iSc" : "[text](link)" // imgUrlSource
"src" : "[text](link)" // source
"pUl" : "/a/N1go943D/asdas" // publicUrl
"sTg" : "marley quotes,lovely" // SEO Tags
"aSh" : 0 // autoSharable
"fbS" : 0 // fbSharedOn
"pub" : 1 // published
Asset
"_id" : ObjectId("")
"oTy" : 1 // objType
"cOn" : 1435888483592 // createdOn
"tgs" : [11,18] // tags
"aUl" : "https://PVEP.jpg" // assetUrl
"src" : "[text](link)" // source
Object Type / Category Mappings
Objects that belong to the Stories collection have individual types that is then used to indentify them. The mapping is stored in the oTy property.
oTy in (Stories) Maps to:
- 0 = Tile
- 1 = Article
oTy in (Assets) Maps to:
- 0 = Image
By default front end is configured to support the following categories, you will see these mappings appear in the cat property of Articles and Tiles.
Category Mapping
- 0 = Quotes
- 1 = Discover
- 2 = Pics