func header(r *http.Request, key string) (string, bool) { if r.Header == nil { return "", false } if candidate := r.Header[key]; !misc.ZeroOrNil(candidate) { return candidate[0], true } return "", false }
func (config *Config) complete() bool { cfgs := reflect.ValueOf(config).Elem() for i := 0; i < cfgs.NumField(); i++ { if misc.ZeroOrNil(cfgs.Field(i).Interface()) { return false } } return true }
func (config *Config) merge(arg Config) *Config { mine := reflect.ValueOf(config).Elem() theirs := reflect.ValueOf(&arg).Elem() for i := 0; i < mine.NumField(); i++ { myField := mine.Field(i) if misc.ZeroOrNil(myField.Interface()) { myField.Set(reflect.ValueOf(theirs.Field(i).Interface())) } } return config }
func (c auth) Get(session *models.OAuthAuthorizedToken, url string, queries url.Values, body io.Reader) (util.APIStatus, interface{}) { user := &struct { Name string `json:"name"` Image string `json:"img, omitempty"` TokenKey string `json:"token, omitempty"` TokenSecret string `json:"secret, omitempty"` }{} if !misc.ZeroOrNil(session) { user.Name = session.ScreenName user.Image = session.ScreenImage user.TokenKey = session.AccessTokenKey user.TokenSecret = session.AccessTokenSecret } return util.Success(http.StatusOK), user }
func custom(log, cors, validate bool, f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { addr := r.RemoteAddr if ip, found := header(r, "X-Forwarded-For"); found { addr = ip } // compress settings ioWriter := w.(io.Writer) for _, val := range misc.ParseCsvLine(r.Header.Get("Accept-Encoding")) { if val == "gzip" { w.Header().Set("Content-Encoding", "gzip") g := gzip.NewWriter(w) defer g.Close() ioWriter = g break } if val == "deflate" { w.Header().Set("Content-Encoding", "deflate") z := zlib.NewWriter(w) defer z.Close() ioWriter = z break } } writer := &customResponseWriter{Writer: ioWriter, ResponseWriter: w, status: http.StatusOK} // CORS headers if cors && !misc.ZeroOrNil(cfg.CorsMethods) { w.Header().Set("Access-Control-Allow-Headers", "*") w.Header().Set("Access-Control-Allow-Methods", cfg.CorsMethods) w.Header().Set("Access-Control-Allow-Origin", cfg.CorsOrigin) w.Header().Set("Access-Control-Allow-Credentials", "true") w.Header().Set("Access-Control-Max-Age", "300") } // route to the controllers f(writer, r) // access log if log && cfg.AccessLog { logs.Info.Printf("%s %s %s %s", addr, strconv.Itoa(writer.status), r.Method, r.URL) } } }
func toStringArray(values string) []string { if misc.ZeroOrNil(values) { return []string{} } return strings.Split(values, ",") }