Skip to content

Instantly share code, notes, and snippets.

@debnath
debnath / Echo Logger HandlerFunc
Created September 6, 2018 14:52
Echo HandlerFunc for logging all HTTP responses
package middleware
import (
"log"
"net/http"
"github.com/labstack/echo"
)
/*
@debnath
debnath / gist:e11de2e10ec36055eda9e446b536874e
Last active March 25, 2024 13:13
Example usage of sync.Map: Store(), Load() and Range()
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
var m sync.Map
@debnath
debnath / gzip
Created January 3, 2018 03:14
gzip / gunzip functions
//Gzip will take an uncompressed bytestream and gzip it
func Gzip(unzip []byte) []byte {
var zip bytes.Buffer
gz := gzip.NewWriter(&zip)
if _, err := gz.Write(unzip); err != nil {
panic(err)
}
if err := gz.Flush(); err != nil {
panic(err)
}
@debnath
debnath / gist:05e16ecc0177543f17c7a6b9e6b12703
Last active December 6, 2017 03:27
Failed experiment: strings.join vs buffer.WriteString
start := time.Now()
/*
var buffer bytes.Buffer
for _, x := range v {
buffer.WriteString(x)
buffer.WriteString("::")
}
test := strings.ToLower(buffer.String()) */
@debnath
debnath / gist:a9879ffd5492113e7db2a33f016ced8c
Created December 4, 2017 03:20
Redis script to delete all keys starting with a prefix
redis-cli KEYS "PREFIX*" | xargs -L 100 redis-cli DEL
@debnath
debnath / gist:ff084e83d77dae8aafd737c6ca510958
Created December 1, 2017 03:43
Tracing Golang app execution
f, err := os.Create("cpu.trace")
if err != nil {
panic(err)
}
trace.Start(f)
defer trace.Stop()
@debnath
debnath / gitu
Created November 10, 2017 03:36
function gitu {
BRANCH=${1:-master}
echo "Checking out $BRANCH"
git checkout "$BRANCH" && git fetch origin && git merge --ff-only origin/"$BRANCH"
}
@debnath
debnath / paperclips-savegame
Last active October 22, 2022 10:55
How to save and load the state of the paperclips game (www.decisionproblem.com/paperclips/index2.html)
The paperclips game over at www.decisionproblem.com/paperclips/index2.html is great, but if you want to play the game indefinitely you can lose all progress if you clear browser cache or change laptop.
I wanted a way to create a save file and load it later, so I did. Use the below script to generate the output to load the current game state.
var saveGame=localStorage.getItem("saveGame"),savePrestige=localStorage.getItem("savePrestige"),saveProjectsActive=localStorage.getItem("saveProjectsActive"),saveProjectsFlags=localStorage.getItem("saveProjectsFlags"),saveProjectsUses=localStorage.getItem("saveProjectsUses"),saveStratsActive=localStorage.getItem("saveStratsActive"),restoreString="/************* COPY ALL TEXT BELOW THIS LINE AND SAVE IN A TEXTFILE SOMEWHERE. RUN IT IN ANY CHROME OR FIREFOX CONSOLE TO LOAD IT. *************/\nlocalStorage.setItem('saveGame', '"+saveGame+"') \nlocalStorage.setItem('savePrestige', '"+savePrestige+"') \nlocalStorage.setItem('saveProjectsActive', '"+saveProjectsActive+"')
Website only has kilojoule/calorie info so that's all we'll render I guess. Maybe I can estimate the protein...
Mock data:
POST /api/v1/nutrition/salad
{
"size": "small",
"data": [
{
"id": "chicken_pasta",
There are 2 possibilities I can think of to organise the data cleanly.
Option 1)
* Have 2 JSON objects.
* One that is just a map between the salad name and the id. We can then pass ids from the form to the calculator functions.
* The other json object contains all the actual data.
E.g.:
{