Programming and Exciting Things

Go lang create map from database results without using predefined struct

Published on 07.12.2015

I created some function which get "random" data from MySQL database and return it as JSON. Here is some example code for achieve this, I skipped boring part with imports and creating net/http server and error handling is skipped.


db, _ := sql.Open(
        "mysql",
        "user=demo dbname=demo password=test")
		
rows, _ := db.Query("select * from sites")

columns, _ := rows.Columns()
count := len(columns)
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)

final_result := map[int]map[string]string{}
result_id := 0
for rows.Next() {
	for i, _ := range columns {
		valuePtrs[i] = &values[i]
	}
	rows.Scan(valuePtrs...)

	tmp_struct := map[string]string{}

	for i, col := range columns {
		var v interface{}
		val := values[i]
		b, ok := val.([]byte)
		if (ok) {
			v = string(b)
		} else {
			v = val
		}
		tmp_struct[col] = fmt.Sprintf("%s",v)
	}
	
	final_result[ result_id ] = tmp_struct
	result_id++
}

fmt.Println(final_result)

Fake rsyslog server with Go Lang

Published on 18.11.2015

Few months ago I created script which act as fake rsyslog server with python, so because I fall in love with Go Lang, here is updated version of this script with added logging into MongoDB

package main

import (
	"encoding/json"
	"fmt"
	"github.com/manucorporat/try"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"net"
	"os"
	"time"
)

const (
	MongoDBHosts      = "127.0.0.1:27017"
	MongoAuthUserName = "root"
	MongoAuthPassword = ""
)

type ParsedMsg struct {
	Msg     string
	ApiKey  string `json:"api_key"`
	AppName string `json:"app-name"`
}

type MongoDoc struct {
	ID        bson.ObjectId `bson:"_id,omitempty"`
	Message   string
	Program   string
	ApiKey    string
	Timestamp time.Time
}


func getSession() *mgo.Session {

	mongoDBDialInfo := &mgo.DialInfo{
		Addrs:   []string{MongoDBHosts},
		Timeout: 60 * time.Second,
	}

	// Connect to our local mongo
	s, err := mgo.DialWithInfo(mongoDBDialInfo)

	// Check if connection error, is mongo running?
	if err != nil {
		panic(err)
	}
	s.SetMode(mgo.Monotonic, true)

	return s
}

func ParseMsg(str string, mongoSession *mgo.Session) {

	var j map[string]interface{}
	err := json.Unmarshal([]byte(str), &j)
	if err != nil {
		panic(err)
	}

	res := ParsedMsg{}
	json.Unmarshal([]byte(str), &res)

	c := mongoSession.DB("logs").C(res.AppName)

	new_doc := &MongoDoc{ID: bson.NewObjectId(), Message: res.Msg, Program: res.AppName, ApiKey: res.ApiKey, Timestamp: time.Now()}

	c.Insert(new_doc)
	if err != nil {
		fmt.Println(err)
	}

	// inserted mongo id
	// string(new_doc.ID.Hex())

}

/* A Simple function to verify error */
func CheckError(err error) {
	if err != nil {
		fmt.Println("Error: ", err)
		os.Exit(0)
	}
}

func main() {
	/* Lets prepare a address at any address at port 10001*/
	ServerAddr, err := net.ResolveUDPAddr("udp", ":514")
	CheckError(err)

	/* Now listen at selected port */
	ServerConn, err := net.ListenUDP("udp", ServerAddr)
	CheckError(err)
	defer ServerConn.Close()

	mongoSession := getSession()

	buf := make([]byte, 1024)

	for {
		n, _, err := ServerConn.ReadFromUDP(buf)

		try.This(func() {
			ParseMsg(string(buf[0:n]), mongoSession)
		})

		if err != nil {
			fmt.Println("Error: ", err)
		}
	}
}

PHP Singlethon implementation

Published on 12.11.2015

Example of quick singlethon implementation in PHP

<?php 
abstract class Singleton {
    
    // protected static $INSTANCE;

   
    /**
     * Protected constructor to prevent creating a new instance of the
     * @return void
     */
    final private function __construct() {
    }

    /**
     * Private clone method to prevent cloning of the instance
     * @return void
     */
    final private function __clone() {
    }
    
    /**
     * Private unserialize method to prevent unserializing 
     * @return void
     */
    final private function __wakeup()
    {
    }

    /**
     * The reference to *Singleton* instance of this class
     * @return Singleton
     */
    final public static function getInstance() {
        return isset(static::$INSTANCE) ? static::$INSTANCE : static::$INSTANCE = new static;
    }

}

////

class AB extends Singleton {
    protected static $INSTANCE;
}

class BA extends Singleton {
    protected static $INSTANCE;
}

$obj = AB::getInstance();

$obj2 = BA::getInstance();


var_dump($obj === AB::getInstance()); // bool true
var_dump($obj === $obj2); // bool false

Availible also in https://gist.github.com/yuksbg/9d981ea6baf0bd8f2d92

Mysql to MongoDB exporter

Published on 02.11.2015

I just finished with simple python class for exporting MySQL tables to MongoDB
It can be use as part of project or as saparate command line application.

More information can be found at https://yuksbg.github.io/mysql2mongo/

Quick clear all docker logs

Published on 26.10.2015

Docker keep all stdout in -json.log files and there is quick way to clear them if they are not added in

logrotate
already.
truncate -s 0 /var/lib/docker/containers/*/*-json.log