Skip to content

Instantly share code, notes, and snippets.

@farhany
farhany / http-timeout.go
Last active April 18, 2019 20:00
Increase http timeout
// Source: https://www.reddit.com/r/golang/comments/45mzie/dont_use_gos_default_http_client/
// Go Source: https://golang.org/pkg/net/http/#Client
// Timeout specifies a time limit for requests made by this
// Client. The timeout includes connection time, any
// redirects, and reading the response body. The timer remains
// running after Get, Head, Post, or Do return and will
// interrupt reading of the Response.Body.
//
// A Timeout of zero means no timeout.
@farhany
farhany / cache-control.go
Created April 21, 2019 16:21
Gorilla Cache-Control for static files
// Source: https://stackoverflow.com/questions/48876324/gorilla-mux-golang-cache-static-files
package main
import (
"associations"
"html/template"
"net/http"
"log"
"io/ioutil"
@farhany
farhany / gorilla-min.go
Created April 21, 2019 16:30
Minimum Gorilla Web Snippet
package main
import (
"net/http"
"github.com/gorilla/mux"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello!"))
@farhany
farhany / gorilla-ws.go
Created April 21, 2019 18:06
Gorilla Web Sockets Example
var (
SyncOutput = make(chan string)
SyncClients = make(map[*websocket.Conn]bool)
SyncClientsLock = sync.RWMutex{}
FileSyncing = false
FileSyncLock = sync.RWMutex{}
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
@farhany
farhany / httpServer-timeout.go
Created April 21, 2019 20:31
httpServer-timeout
s := &http.Server{
Addr: ":8080",
Handler: myHandler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Fatal(s.ListenAndServe())
@farhany
farhany / dumpresponse.go
Last active April 24, 2019 03:51
httputil.DumpResponse use in Go
res, err := c.httpClient.Do(req)
dump, _ := httputil.DumpResponse(res, true)
fmt.Printf("DUMP: %q\n", dump)
@farhany
farhany / custom404.go
Created April 24, 2019 03:55
Custom Error Pages in Go
// Source: https://stackoverflow.com/questions/9996767/showing-custom-404-error-page-with-standard-http-package
package main
import (
"fmt"
"net/http"
)
func main() {
@farhany
farhany / TeeReader.go
Created April 24, 2019 03:58
Basic use of TeeReader to reduce memory consumption
// Source: https://stackoverflow.com/questions/41947307/how-to-return-hash-and-bytes-in-one-step-in-go
hasher := sha256.New()
f, err := os.Open(fname)
data := io.TeeReader(f, hasher)
// Now read from data as usual, which is still a stream.
@farhany
farhany / APICodes.txt
Created April 24, 2019 06:53
API Codes
HTTP Status Codes (General)
2xx = Success
3xx = Redirect
4xx = User error
5xx = Server error
Success codes:
200 - OK
201 - Created
202 - Accepted (Mostly used for DELETE)
@farhany
farhany / Setup macOS
Last active April 28, 2019 04:47
Setup macOS
# https://github.com/tonsky/FiraCode/wiki
# Fonts
brew tap caskroom/fonts
brew cask install font-fira-code
# ZSH
brew install zsh-syntax-highlighting zsh-autosuggestions
brew install fasd
brew install ripgrep