Skip to content

Instantly share code, notes, and snippets.

@sayden
Created August 17, 2018 22:03
Show Gist options
  • Save sayden/5840e37b032dbc2a7900580e7e86d1e8 to your computer and use it in GitHub Desktop.
Save sayden/5840e37b032dbc2a7900580e7e86d1e8 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"github.com/thehivecorporation/log"
"net/http"
"time"
)
type res struct {
Success bool
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
potentiallySlowOperation(5)
byt, err := json.Marshal(res{Success: true})
if err != nil {
w.WriteHeader(500)
return
}
w.Write(byt)
})
http.ListenAndServe(":8080", nil)
}
func potentiallySlowOperation(s time.Duration) {
log.Infof("'Processing' for %d seconds", s)
now := time.Now()
defer func(now time.Time) {
log.WithField("elapsed", time.Since(now)).Info("Returning")
}(now)
time.Sleep(time.Second * s)
log.Info("Processing done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment