commit 8fb8415e0a3a4d55511dac063be3515f3e15676b Author: Martijn de Boer Date: Sat Jan 5 12:02:50 2019 +0100 First commit diff --git a/app.js b/app.js new file mode 100644 index 0000000..ef403f3 --- /dev/null +++ b/app.js @@ -0,0 +1,122 @@ +var net = require( 'net' ); +var fs = require( 'fs' ); +var path = require( 'path' ); +var os = require( 'os' ); + +var config = require( './configuration.json' ); + +var TAB = "\t"; +var EOF = ".\r\n"; +var NL = "\r\n"; + +if ( config.jsonPath[0] === "~" ) { + config.jsonPath = path.join( os.homedir(), config.jsonPath.slice( 1 ) ); +} + +var hugoJson = null; + +loadJson(); + +function loadJson() { + fs.readFile( config.jsonPath, 'utf8', function( err, data ) { + if ( err ) { + console.log( err ); + } else { + hugoJson = JSON.parse( data ); + } + } ); +} + +function processQuery( query, sock ) { + if ( query.length == 0 ) { + writeInfo( sock, '------------------------------------------------------------------------' ); + writeInfo( sock, config.siteName ); + + config.siteDescription.split( NL ).forEach( function( line ) { + writeInfo( sock, line ); + } ); + + writeInfo( sock, '------------------------------------------------------------------------' ); + writeInfo( sock, '' ); + + hugoJson.forEach( function( blog ) { + writeFile( sock, '0', blog[ config.titleAttr ].charAt( 0 ).toUpperCase() + blog[ config.titleAttr ].slice( 1 ), blog[ config.urlAttr ] ); + } ); + + sock.end(); + } else { + + sock.write( query + NL ); + + var hasBlog = false; + + hugoJson.forEach( function( blog ) { + if ( blog[ config.urlAttr ].trim().toLowerCase() == query.trim().toLowerCase() ) { + blog[ config.bodyAttr ].split( ".\n" ).forEach( function( line ) { + sock.write( line + "." + NL + NL ); + } ); + + sock.end(); + hasBlog = true; + } + } ); + + if ( hasBlog === false ) { + sock.write( "No such file" ); + sock.end(); + } + } +} + +function writeFile( sock, type, title, path ) { + sock.write( type + title + TAB + path + TAB + config.gopherDomain + TAB + config.gopherPort + NL ); +} + +function writeInfo( sock, text ) { + sock.write( 'i' + text + TAB + TAB + TAB + NL ); +} + +function writeError( sock, text ) { + sock.end( '3' + text + EOF ); +} + +var server = net.createServer( function( sock ) { + var query = ""; + + console.log( 'Connection from', sock.remoteAddress, sock.remotePort ); + + sock.on( 'data', function( buf ) { + var ready = false; + + for ( var i = 0; i < buf.length; i++ ) { + var bChar = buf.readUInt8( i ); + switch (bChar) { + case 0x0: + ready = false; + break; + case 0xD: + ready = true; + break; + case 0xA: + if ( ready === true) { + processQuery( query, sock ); + } + break; + default: + ready = false; + query += String.fromCharCode( bChar ); + break; + } + } + } ); +} ); + +server.listen( { + "host": config.gopherInterface, + "port": config.gopherPort, + "exclusive": true +}, function() { + console.log( 'hugo-gopher ready' ); + console.log( config.gopherDomain + ' at gopher://' + config.gopherInterface + ":" + config.gopherPort ); + console.log( 'JSON: ' + config.jsonPath ); +}); \ No newline at end of file diff --git a/configuration.json b/configuration.json new file mode 100644 index 0000000..1d0af90 --- /dev/null +++ b/configuration.json @@ -0,0 +1,13 @@ +{ + "jsonPath": "~/source/ginnegappen/ginnegappen/public/index.json", + "siteName": "Ginnegappen.nl", + "siteDescription": "\r\nAls je het niet te serieus neemt, dan valt er misschien nog wat te leren op dit blog.\r\nWe schrijven over van alles en nog wat, zo lang er maar wat over valt te ginnegappen.\r\nVan opinie tot wetenschap, van online privacy tot koffie.", + + "gopherDomain": "ginnegappen.nl", + "gopherPort": "1070", + "gopherInterface": "0.0.0.0", + + "titleAttr": "title", + "urlAttr": "id", + "bodyAttr": "body" +} \ No newline at end of file