Created
August 17, 2018 22:03
-
-
Save sayden/5840e37b032dbc2a7900580e7e86d1e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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