Instantly share code, notes, and snippets.

View gen_cert_key.sh
#!/bin/sh
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
openssl rsa -in key.pem -out pri.key
View backup.sh
#!/bin/sh
# how to use:
# ./backup.sh mysql_prod root rootpw mydb /var/log/backup.sql
# crontab example:
# 0 0 * * * /backup.sh mysql_prod root rootpw mydb /var/log/backup.sql
# restore example:
# cat backup.sql | docker exec -i [container_name] /usr/bin/mysql -u [mysql_user] --password=[mysql_password] [database_name]
View go_installer_mac.sh
#!/bin/sh
# usage:
# ./go_installer_mac.sh [go version]
#
# example install go 1.10.2:
# ./go_installer_mac.sh 1.10.2
set -uex
View http_server.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func sayHello(w http.ResponseWriter, r *http.Request) {
View createOrOpenFile.go
func createOrOpenFile(dir, fn string) (*os.File, error) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, 0700)
if err != nil {
log.Infof("mkdir %v fail", dir)
return nil, err
}
}
fallPath := filepath.Join(dir, fn)
View Dockerfile
FROM ubuntu:16.04
MAINTAINER natataworld@love.com
ARG USER=docker_user
RUN apt-get update && apt-get -y install cron sudo
# create a normal user
RUN adduser --disabled-password --gecos '' $USER
RUN adduser $USER sudo
View paddingLabel.swift
// source: http://www.cnblogs.com/over140/p/4837652.html
class UILabelPadding : UILabel {
private var padding = UIEdgeInsets.zero
@IBInspectable
var paddingLeft: CGFloat {
get { return padding.left }
set { padding.left = newValue }
View Downloader.swift
//
// Downloader.swift
//
// Created by Natata on 2017/9/15.
// Copyright © 2017 Natata. All rights reserved.
//
import Foundation
import Alamofire
View recoverFactory.go
func recoverFactory(f func()) func() {
rf := func() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered in f", r, "ker")
}
}()
f()
}
View CMDeviceMotion.swift
extension CMDeviceMotion {
func gaze(atOrientation orientation: UIInterfaceOrientation) -> SCNVector4 {
let attitude = self.attitude.quaternion
let aq = GLKQuaternionMake(Float(attitude.x), Float(attitude.y), Float(attitude.z), Float(attitude.w))
let pi2 = Double.pi/2
let final: SCNVector4
switch UIApplication.shared.statusBarOrientation {