Skip to content

Instantly share code, notes, and snippets.

// I created Delete method on Post struct
func (p *Post) Delete(db *DB) error {
_, err := db.Exec("DELETE FROM posts WHERE id = ?", p.ID)
if err != nil {
return err
}
return nil
}
@JSila
JSila / restart
Last active April 7, 2016 12:07
Bash script for building and starting application written in Go. Also if some file is changed, it rebuilds and restarts application.
#!/bin/bash
# Bash script for building and starting application in Go. Also if some file is changed, it rebuilds and restarts application.
# Before running the script, inotify-tools has to be installed.
trap ctrl_c INT
function ctrl_c {
kill $PID
log "stopped"
@JSila
JSila / main.go
Created January 13, 2016 22:22
Basic Auth middleware with example of use
package main
import (
"fmt"
"net/http"
)
func BasicAuth(username string, password string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@JSila
JSila / instruction.md
Last active October 3, 2015 14:55
Quick set-up for ES6 project with babel and browserify.

It's easy! There are only 2 steps:

  1. Install required packages with NPM: npm install browserify babelify

  2. Create package.json with the following content (here main.js file is source file and bundle.js is output file. Also note we are applying transformer to browserify command):

    {
      "scripts": {
        "build-js": "browserify -t babelify main.js > bundle.js"

}

@JSila
JSila / localStorage.js
Created June 26, 2015 22:33
this helps with saving and retriving values other than strings from localStorage.
Storage.prototype.getItem = function(key) {
try {
return JSON.parse(this[key]);
} catch (error) {}
return this[key];
}
Storage.prototype.setItem = function(key, value) {
this[key] = (typeof value == 'string') ? value : JSON.stringify(value);
}
@JSila
JSila / BladeExtensionsProvider.php
Last active August 29, 2015 14:16
Few additional tags for blade templating engine
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class BladeExtensionsProvider extends ServiceProvider {
// Don't forget to register this provider in config/app.php
//
// Or if you don't want to bother, add content of boot function
// to AppServiceProvider.php's boot function
@JSila
JSila / laravel-socialite.md
Last active August 29, 2015 14:16
How to: Laravel Socialite

In this case we want to authenticate with twitter. This gist assumes new twitter app is already generated (we need client id, client secret and redirect url).

First require the package with composer

composer require laravel/socialite:~2.0

Register service provider and facade for the package.

Then add credentials for authentication with twitter in config/services.php:

@JSila
JSila / reflection-api.php
Created February 14, 2015 13:48
php reflection api example
<?php
class User {
protected $name;
public function __construct($name)
{
$this->name = $name;
}
@JSila
JSila / AppServiceProvider.php
Created February 6, 2015 23:03
Google ReCaptcha Laravel 5 Integration (validation rule)
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->app->validator->resolver(function($translator, $data, $rules, $messages)
{
return new GoogleReCaptchaValidator($translator, $data, $rules, $messages);
@JSila
JSila / paginate.php
Created February 1, 2015 15:23
pagination function
/**
* Paginates an array of data in accordance with how many items are on page
* and page currently viewed.
*
* @param array $data Array of data to paginate.
* @param int $perPage Number of items to display per page.
* @param int $currentPage Page number currently viewed.
* @return array
*/
function paginate(array $data, $perPage, $currentPage = null)