Skip to content

Instantly share code, notes, and snippets.

View ulexxander's full-sized avatar
💪
Workin hard

Alexander Ustyugov ulexxander

💪
Workin hard
  • Limitlex d.o.o.
  • Slovenia
View GitHub Profile
@ulexxander
ulexxander / subjects.go
Created May 31, 2022 06:56
Good way to organize Pub/Sub subjects for both publishers and consumers. Can be applied to other OOP languages as well.
package api
import "fmt"
func publish(subject string) {
fmt.Println("Publishing on", subject)
}
func subscribe(subject string) {
fmt.Println("Subscribing on", subject)
@ulexxander
ulexxander / google-auth.ts
Created May 15, 2022 09:55
Google Auth getAccessToken using keyFile
const googleAuth = new GoogleAuth({
keyFile: "./google-service-account.json",
scopes: "https://www.googleapis.com/auth/homegraph",
});
const accessToken = await googleAuth.getAccessToken();
console.log(accessToken);
package main
import (
"strconv"
"time"
)
func main() {
intsChan := make(chan int)
stringsChan := make(chan string)
@ulexxander
ulexxander / client-libraries.md
Created March 9, 2022 13:43
Simple and obvious approach for estabilishing end-to-end typing between communicating services with better maintainability and productivity.

image

Made using https://app.diagrams.net

<mxfile host="app.diagrams.net" modified="2022-03-09T13:38:35.242Z" agent="5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36" etag="AHwkt2RR6Z7wHRqwMwQm" version="16.6.5" type="device"><diagram id="H_dx995EEjTLIg52hnT0" name="Page-1">5Vpbc5s4FP41nkkf6uHi2z7Gdpp0Jtub03b3aUcGGbQBxAoRx/31e44QYAKkTmqbOJ2JJ+jogvSdT5+OhHr2LLy/FCT2/+QuDXqW4d737HnPsixjZME/tGwyi2mOBpnFE8zVttKwYD+oNhramjKXJpWCkvNAsrhqdHgUUUdWbEQIvq4WW/Gg+taYeLRmWDgkqFu/M1f6+TAMo8y4oszz81eP85yQ5KW1IfGJy9dbJvuiZ88E5zJ7Cu9nNED4cmCyeu9acoueCRrJXSq8N4fix8er2yQxPs4W39a3i+/Lt8OslTsSpHrEs4Bhg5ZxzZaCiI3uvNzkkCRrFgYkgtRUV6ZC0vvWXpnFWIEmlIdUYqOGrjDI8dIMGQx0el3CbU20zd9CujAS7WKvaLsEAR40Dk/AxBzUQJGbmIJlRmQNjrXPJF3ExMH0GqYB4OLLEN44N+GRJHHGyxW7p+6umLX7qhVI6wGOdRhHDSiODgbipA3EOfdOBURz1DGKg0ENKuqCQOkkF9LnHo9IcFFap4KnkYswzQ1IlWWuOY81oP9SKTdabUkqeRXuFY/kOxKyAGG4os
@ulexxander
ulexxander / twofactorunmarshal_test.go
Created February 21, 2022 14:38
Example of using Go json.RawMessage to unmarshal JSON field in struct based on another field determining its expected type.
package twofactorunmarshal
import (
"encoding/json"
"fmt"
"testing"
"github.com/pkg/errors"
)
@ulexxander
ulexxander / json_time_duration.go
Created January 19, 2022 08:43
Make Go time.Duration JSON marshalable and unmarshalable as strings.
// Duration embeds time.Duration and makes it more JSON-friendly.
// Instead of marshaling and unmarshaling as int64 it uses strings, like "5m" or "0.5s".
type Duration time.Duration
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
func (d *Duration) UnmarshalJSON(data []byte) error {
var str string
@ulexxander
ulexxander / token_hex.go
Created November 27, 2021 09:46
Generate random hex token
package random
import (
"crypto/rand"
"encoding/hex"
"fmt"
)
func TokenHex(length int) (string, error) {
// Base 16 is 2x longer than Base 256
@ulexxander
ulexxander / prometheus-docker-sd.yml
Created November 15, 2021 09:00
Prometheus scrape config for automatic docker service discovery
# NOTE: containers must be on the same network as Prometheus
# And port 80 with /metrics endpoint must be exposed
scrape_configs:
- job_name: "docker_sd"
docker_sd_configs:
# Do not forget to mount docker socket into container
- host: "unix:///var/run/docker.sock"
relabel_configs:
- source_labels:
- "__meta_docker_container_id"
/*
basename prop has '/' by deafult
it works as a prefix for all of routes, pushes, links and so on
*/
export const App: React.FC = () => {
return (
<Router basename="/admin">
{/* ... */}
</Router>
);
func splitEscaped(s string, sep, esc rune) []string {
var pos int
a := strings.FieldsFunc(s, func(r rune) bool {
escaped := pos > 0 && rune(s[pos-1]) == esc
pos++
return r == sep && !escaped
})
for i, s := range a {
a[i] = strings.ReplaceAll(s, string(esc)+string(sep), string(sep))
}