func main() { var err error flags.Parse(os.Args[1:]) conf, err := server.NewConfigFromFile(*confFile, os.Getenv("CONFIG")) if err != nil { log.Fatal(err) } srv := server.New(conf) graceful.AddSignal(syscall.SIGINT, syscall.SIGTERM) graceful.PreHook(func() { srv.Close() }) if err := srv.Configure(); err != nil { log.Fatal(err) } lg.Infof("** Imgry Server v%s at %s **\n", imgry.VERSION, srv.Config.Server.Addr) lg.Infof("** Engine: %s", imagick.Engine{}.Version()) err = graceful.ListenAndServe(srv.Config.Server.Addr, srv.NewRouter()) if err != nil { lg.Fatal(err.Error()) } graceful.Wait() }
func RequestLogger(next http.Handler) http.Handler { reqCounter := metrics.GetOrRegisterCounter("route.TotalNumRequests", nil) h := func(w http.ResponseWriter, r *http.Request) { reqCounter.Inc(1) u, err := url.QueryUnescape(r.URL.RequestURI()) if err != nil { lg.Error(err.Error()) } start := time.Now() lg.Infof("Started %s %s", r.Method, u) lw := &loggedResponseWriter{w, -1} next.ServeHTTP(lw, r) lg.Infof("Completed (%s): %v %s in %v", u, lw.Status(), http.StatusText(lw.Status()), time.Since(start), ) } return http.HandlerFunc(h) }
func main() { var err error flags.Parse(os.Args[1:]) conf, err := server.NewConfigFromFile(*confFile, os.Getenv("CONFIG")) if err != nil { log.Fatal(err) } srv := server.New(conf) if err := srv.Configure(); err != nil { log.Fatal(err) } lg.Infof("** Imgry Server v%s at %s **", imgry.VERSION, srv.Config.Bind) lg.Infof("** Engine: %s", srv.ImageEngine.Version()) graceful.AddSignal(syscall.SIGINT, syscall.SIGTERM) graceful.Timeout(30 * time.Second) graceful.PreHook(srv.Close) graceful.PostHook(srv.Shutdown) err = graceful.ListenAndServe(srv.Config.Bind, srv.NewRouter()) if err != nil { lg.Fatal(err.Error()) } graceful.Wait() }
func (hf HttpFetcher) GetAll(urls []string) ([]*HttpFetcherResponse, error) { m := metrics.GetOrRegisterTimer("fn.FetchRemoteData", nil) // TODO: update metric name defer m.UpdateSince(time.Now()) resps := make([]*HttpFetcherResponse, len(urls)) var wg sync.WaitGroup wg.Add(len(urls)) // TODO: add thruput here.. for i, urlStr := range urls { resps[i] = &HttpFetcherResponse{} go func(resp *HttpFetcherResponse) { defer wg.Done() url, err := urlx.Parse(urlStr) if err != nil { resp.Err = err return } resp.URL = url lg.Infof("Fetching %s", url.String()) fetch, err := hf.client().Get(url.String()) if err != nil { lg.Warnf("Error fetching %s because %s", url.String(), err) resp.Err = err return } defer fetch.Body.Close() resp.Status = fetch.StatusCode body, err := ioutil.ReadAll(fetch.Body) if err != nil { resp.Err = err return } resp.Data = body resp.Err = nil }(resps[i]) } wg.Wait() return resps, nil }
func (f Fetcher) GetAll(ctx context.Context, urls []string) ([]*FetcherResponse, error) { defer metrics.MeasureSince([]string{"fn.FetchRemoteData"}, time.Now()) fetches := make([]*FetcherResponse, len(urls)) var wg sync.WaitGroup wg.Add(len(urls)) // TODO: add thruput here.. for i, urlStr := range urls { fetches[i] = &FetcherResponse{} go func(fetch *FetcherResponse) { defer wg.Done() url, err := urlx.Parse(urlStr) if err != nil { fetch.Err = err return } fetch.URL = url lg.Infof("Fetching %s", url.String()) resp, err := ctxhttp.Get(ctx, f.client(), url.String()) if err != nil { lg.Warnf("Error fetching %s because %s", url.String(), err) fetch.Err = err return } defer resp.Body.Close() fetch.Status = resp.StatusCode body, err := ioutil.ReadAll(resp.Body) if err != nil { fetch.Err = err return } fetch.Data = body fetch.Err = nil }(fetches[i]) } wg.Wait() return fetches, nil }