func (this *service) ServeHTTP(w http.ResponseWriter, r *http.Request) { urlPath := r.URL.Path hash := urlPath[strings.LastIndex(urlPath, "/")+1:] size := this.mustInt(r, 290, "s", "size") // default size = 290*290 avatar := New(hash, this.cacheDir) avatar.AlterImage = this.altImage if avatar.Expired() { if err := avatar.UpdateTimeout(time.Millisecond * 1000); err != nil { log.Trace("avatar update error: %v", err) return } } if modtime, err := avatar.Modtime(); err == nil { etag := fmt.Sprintf("size(%d)", size) if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) && etag == r.Header.Get("If-None-Match") { h := w.Header() delete(h, "Content-Type") delete(h, "Content-Length") w.WriteHeader(http.StatusNotModified) return } w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat)) w.Header().Set("ETag", etag) } w.Header().Set("Content-Type", "image/jpeg") if err := avatar.Encode(w, size); err != nil { log.Warn("avatar encode error: %v", err) w.WriteHeader(500) } }
func NewContext() { var ( e error ) Cfg, e = ini.Load("conf/app.ini") if e != nil { log.Fatal(4, "Fail to load conf %v", e) } if com.IsFile(CustomConf) { if e = Cfg.Append(CustomConf); e != nil { log.Fatal(4, "Fail to load custom conf '%s': %v", CustomConf, e) } } else { log.Warn("Custom config (%s) not found, ignore this if you're running first time", CustomConf) } Cfg.NameMapper = ini.AllCapsUnderscore }