Skip to content

Instantly share code, notes, and snippets.

View nanmu42's full-sized avatar

Zhennan LI nanmu42

View GitHub Profile
@nanmu42
nanmu42 / fileserver.go
Created March 6, 2021 03:48
Golang Quick Static File Server
package main
import (
"flag"
"fmt"
"net"
"net/http"
)
var (
@nanmu42
nanmu42 / vue.config.js
Created January 13, 2021 04:20
A vue.config.js example for starting new project
const CompressionPlugin = require("compression-webpack-plugin");
const fileToCompressRegex = /\.(js|css|htm|html|svg|png|txt|json|wasm)$/
const productionMode = process.env.NODE_ENV === 'production'
let plugins = []
if (productionMode) {
plugins.push(new CompressionPlugin({
filename: '[path][base].br[query]',
algorithm: 'brotliCompress',
test: fileToCompressRegex,
@nanmu42
nanmu42 / remove_slice_duplicates.go
Created November 18, 2020 06:47
Golang Remove Slice Duplicates
// source: https://www.reddit.com/r/golang/comments/5ia523/idiomatic_way_to_remove_duplicates_in_a_slice/db6qa2e/
func SliceUniqMap(s []int) []int {
seen := make(map[int]struct{}, len(s))
j := 0
for _, v := range s {
if _, ok := seen[v]; ok {
continue
}
seen[v] = struct{}{}
s[j] = v
@nanmu42
nanmu42 / detect-touchscreen.js
Last active March 23, 2020 03:13
Javascript: Does the device have a touchscreen?
function isTouchDevice() {
return 'ontouchstart' in window
}
@nanmu42
nanmu42 / awk.sh
Created March 11, 2020 04:38
awk examples
# unique line
awk '{!seen[$0]++};END{for(i in seen) if(seen[i]==1)print i}' file
# second column of a csv, with white spaces and quotes stripped
awk -F',' '{gsub(/[ "]/, "", $0);print $2}' some.csv > dest.txt
@nanmu42
nanmu42 / delete-k3s.md
Last active February 1, 2020 04:57
K3S Jumpstart

Run /usr/local/bin/k3s-uninstall.sh

@nanmu42
nanmu42 / eck.yml
Last active November 8, 2019 10:41
Elastic Cloud + FluentBit on k8s (minisized deployment)
apiVersion: elasticsearch.k8s.elastic.co/v1beta1
kind: Elasticsearch
metadata:
namespace: elastic-system
name: logging-eck
spec:
version: 7.4.2
nodeSets:
- name: default
count: 1
@nanmu42
nanmu42 / scanner_valuer.go
Created October 23, 2019 07:29
Golang SQL scanner and valuer example
// QueryParams every key is a query param
type QueryParams map[string]QueryParam
// Scan satisfies SQL scanner interface
func (q *QueryParams) Scan(src interface{}) error {
b, ok := src.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}
@nanmu42
nanmu42 / check_cert_valid_date.sh
Created August 30, 2019 05:58
Check valid date of certificate
echo | openssl s_client -servername www.github.com -connect www.github.com:443 2>/dev/null | openssl x509 -noout -dates
@nanmu42
nanmu42 / geoOffset.go
Created August 21, 2019 08:28
计算从 (lon0, lat0) 经纬度(单位 °)移动 dx, dy 米后的新的经纬度
// geoOffset 计算从 (lon0, lat0) 经纬度(单位 °)移动 dx, dy 米后的新的经纬度
//
// 这个算法在100km以内有良好精度。
//
// source: https://stackoverflow.com/questions/2839533/adding-distance-to-a-gps-coordinate
func geoOffset(lon0, lat0, dx, dy float64) (lon, lat float64) {
lon = lon0 + (180/math.Pi)*(dx/6378137)/math.Cos(lat0/180*math.Pi)
lat = lat0 + (180/math.Pi)*(dy/6378137)
return