Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

Node.js

Node.js

<!-- use require to include a module -->
var mysql = require('mysql');

var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var baseDirectory = __dirname;   // or whatever base directory you want

var port = 8000;

var con = mysql.createConnection({
  host: "localhost",
  user: "inventory",
  password: "A3yGDqkxYvd64fDe",
  database: "inventory",
  insecureAuth: true
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

http.createServer(function (request, response) {
    try {
        var requestUrl = url.parse(request.url);

        // need to use path.normalize so people can't access directories underneath baseDirectory
        var fsPath = baseDirectory+path.normalize(requestUrl.pathname);

        var fileStream = fs.createReadStream(fsPath);
        fileStream.pipe(response);
        fileStream.on('open', function() {
            response.writeHead(200);
        });
        fileStream.on('error',function(e) {
            response.writeHead(404) ;    // assume the file doesn't exist
            response.end();
        });
    } catch(e) {
        response.writeHead(500);
        response.end() ;    // end the response so browsers don't hang
        console.log(e.stack);
    }
}).listen(port);

console.log("listening on port "+port);

/*
CREATE TABLE `inventory`.`items` (
  `id` INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `title` VARCHAR(128) NOT NULL ,
  `overview` TEXT NOT NULL ,
  `category` VARCHAR(18) NOT NULL ,
  `serial` VARCHAR(32) NOT NULL ,
  `warranty_months` INT(2) NOT NULL ,
  `danger` TEXT NOT NULL ,
  `repairs` TEXT NOT NULL ,
  `notes` TEXT NOT NULL ,
  `related_items` JSON NOT NULL ,
  `vendors` JSON NOT NULL ,
  `maintenance` JSON NOT NULL ,
  `purchase_cost` DECIMAL(5,2) NOT NULL ,
  `purchase_date` DATE NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

CREATE TABLE `inventory`.`vendors` (
  `id` INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `service` VARCHAR(128) NOT NULL ,
  `contact_name` VARCHAR(32) NOT NULL ,
  `contact_phone` VARCHAR(13) NOT NULL ,
  `contact_email` VARCHAR(64) NOT NULL ,
  `alt_phone` VARCHAR(13) NOT NULL ,
  `website` VARCHAR(128) NOT NULL ,
  `hourly_rate` DECIMAL(5,2) NOT NULL ,
  `notes` TEXT NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

CREATE TABLE `inventory`.`activities` (
  `id` INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `item_id` INT(5) NOT NULL ,
  `when` DATE NOT NULL ,
  `title` VARCHAR(255) NOT NULL ,
  `followup` DATE NOT NULL ,
  `description` TEXT NOT NULL ,
  `vendor_id` INT(5) NOT NULL ,
  `assigned_to` INT(5) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

CREATE TABLE `inventory`.`maintenance` (
  `id` INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `item_id` INT(5) NOT NULL ,
  `due_date` DATE NOT NULL ,
  `completed` DATE NOT NULL ,
  `assigned_to` INT(5) NOT NULL ,
  `title` VARCHAR(255) NOT NULL ,
  `description` TEXT NOT NULL ,
  `followup` DATE NOT NULL ,
  `warning` TEXT NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

CREATE TABLE `inventory`.`links` (
  `id` INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `item_id` INT(5) NOT NULL ,
  `parents` JSON NOT NULL ,
  `children` JSON NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

CREATE TABLE `inventory`.`logins` (
  `id` INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `internal_addr` VARCHAR(18) NOT NULL ,
  `external_url` VARCHAR(255) NOT NULL ,
  `user` VARCHAR(128) NOT NULL ,
  `pass` VARCHAR(32) NOT NULL ,
  `email` VARCHAR(128) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

CREATE TABLE `inventory`.`workers` (
  `id` INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `first_name` INT(5) NOT NULL ,
  `last_name` DATE NOT NULL ,
  `email` DATE NOT NULL ,
  `phone` INT(5) NOT NULL ,
  `role` VARCHAR(32) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
*/