当前位置: 首页>>代码示例>>Golang>>正文


Golang keys.MustGetLog函数代码示例

本文整理汇总了Golang中github.com/clawio/clawiod/keys.MustGetLog函数的典型用法代码示例。如果您正苦于以下问题:Golang MustGetLog函数的具体用法?Golang MustGetLog怎么用?Golang MustGetLog使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了MustGetLog函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Status

// Status implements the  ownCloud status call
func (s *svc) Status(w http.ResponseWriter, r *http.Request) {
	log := keys.MustGetLog(r)

	major := "8"
	minor := "2"
	micro := "1"
	edition := ""

	version := fmt.Sprintf("%s.%s.%s.4", major, minor, micro)
	versionString := fmt.Sprintf("%s.%s.%s", major, minor, micro)

	status := &struct {
		Installed     bool   `json:"installed"`
		Maintenace    bool   `json:"maintenance"`
		Version       string `json:"version"`
		VersionString string `json:"versionstring"`
		Edition       string `json:"edition"`
	}{
		true,
		false,
		version,
		versionString,
		edition,
	}

	statusJSON, err := json.MarshalIndent(status, "", "    ")
	if err != nil {
		log.WithError(err).Error("cannot encode status struct")
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}
	w.Header().Add("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	w.Write(statusJSON)
}
开发者ID:clawio,项目名称:clawiod,代码行数:36,代码来源:status.go

示例2: handleFinderRequest

func (s *svc) handleFinderRequest(w http.ResponseWriter, r *http.Request) error {
	log := keys.MustGetLog(r)

	/*
	   Many webservers will not cooperate well with Finder PUT requests,
	   because it uses 'Chunked' transfer encoding for the request body.
	   The symptom of this problem is that Finder sends files to the
	   server, but they arrive as 0-length files in PHP.
	   If we don't do anything, the user might think they are uploading
	   files successfully, but they end up empty on the server. Instead,
	   we throw back an error if we detect this.
	   The reason Finder uses Chunked, is because it thinks the files
	   might change as it's being uploaded, and therefore the
	   Content-Length can vary.
	   Instead it sends the X-Expected-Entity-Length header with the size
	   of the file at the very start of the request. If this header is set,
	   but we don't get a request body we will fail the request to
	   protect the end-user.
	*/
	log.Warnf("intercepting Finder problem (Content-Length:%s X-Expected-Entity-Length:%s)", r.Header.Get("Content-Length"), r.Header.Get("X-Expected-Entity-Length"))

	// The best mitigation to this problem is to tell users to not use crappy Finder.
	// Another possible mitigation is to change the use the value of X-Expected-Entity-Length header in the Content-Length header.
	expected := r.Header.Get("X-Expected-Entity-Length")
	expectedInt, err := strconv.ParseInt(expected, 10, 64)
	if err != nil {
		log.WithError(err).Error("X-Expected-Entity-Length is not a number")
		w.WriteHeader(http.StatusBadRequest)
		return err
	}
	r.ContentLength = expectedInt
	return nil
}
开发者ID:clawio,项目名称:clawiod,代码行数:33,代码来源:put.go

示例3: Get

// Get implements the WebDAV GET method to download a file.
func (s *svc) Get(w http.ResponseWriter, r *http.Request) {
	log := keys.MustGetLog(r)
	user := keys.MustGetUser(r)

	path := mux.Vars(r)["path"]
	info, err := s.metaDataController.ExamineObject(user, path)
	if err != nil {
		s.handleGetError(err, w, r)
		return
	}
	if info.Type != entities.ObjectTypeBLOB {
		log.Warn("object is not a blob")
		w.WriteHeader(http.StatusNotImplemented)
		return
	}

	reader, err := s.dataController.DownloadBLOB(user, path)
	if err != nil {
		s.handleGetError(err, w, r)
		return
	}

	w.Header().Add("Content-Type", info.MimeType)

	if _, err := io.Copy(w, reader); err != nil {
		log.WithError(err).Error("cannot write response body")
	}
}
开发者ID:clawio,项目名称:clawiod,代码行数:29,代码来源:get.go

示例4: Token

// Authenticate authenticates an user using an username and a password.
func (s *svc) Token(w http.ResponseWriter, r *http.Request) {
	log := keys.MustGetLog(r)

	if r.Body == nil {
		log.WithError(errors.New("body is nil")).Info("cannot read body")
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	authReq := &TokenRequest{}
	if err := json.NewDecoder(r.Body).Decode(authReq); err != nil {
		e := codes.NewErr(codes.BadInputData, "")
		log.WithError(e).Error(codes.BadInputData.String())
		w.WriteHeader(http.StatusBadRequest)
		if err := json.NewEncoder(w).Encode(e); err != nil {
			log.WithError(err).Error("cannot encode")
		}
		return
	}

	token, err := s.authenticationController.Authenticate(authReq.Username, authReq.Password)
	if err != nil {
		s.handleTokenError(err, w, r)
		return
	}
	log.WithField("user", authReq.Username).Info("token generated")

	res := &TokenResponse{AccessToken: token}

	w.WriteHeader(http.StatusCreated)
	if err := json.NewEncoder(w).Encode(res); err != nil {
		log.WithError(err).Error("cannot encode")
	}
}
开发者ID:clawio,项目名称:clawiod,代码行数:35,代码来源:token.go

示例5: basicAuthHandlerFunc

// basicAuthHandlerFunc is a middleware function to authenticate HTTP requests.
func (s *svc) basicAuthHandlerFunc(handler http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		log := keys.MustGetLog(r)

		// try to get token from cookie
		authCookie, err := r.Cookie("ClawIO_Token")
		if err == nil {
			user, err := s.authenticator.CreateUserFromToken(authCookie.Value)
			if err == nil {
				r = keys.SetUser(r, user)
				log.WithField("user", user.Username).Info("authenticated request")
				handler(w, r)
				return
			}
			log.WithError(err).Warn("token is not valid anymore")
		} else {
			log.WithError(err).Warn("cookie is not valid")
		}

		// try to get credentials using basic auth
		username, password, ok := r.BasicAuth()
		if !ok {
			log.Warn("basic auth not provided")
			w.Header().Set("WWW-Authenticate", "Basic Realm='ClawIO credentials'")
			w.WriteHeader(http.StatusUnauthorized)
			return
		}

		// try to authenticate user with username and password
		token, err := s.authenticationController.Authenticate(username, password)
		if err != nil {
			log.WithError(err).Warn("unauthorized")
			w.Header().Set("WWW-Authenticate", "Basic Realm='ClawIO credentials'")
			w.WriteHeader(http.StatusUnauthorized)
			return
		}

		// save token into cookie for further requests
		cookie := &http.Cookie{}
		cookie.Name = "ClawIO_Token"
		cookie.Value = token
		http.SetCookie(w, cookie)

		user, err := s.authenticator.CreateUserFromToken(token)
		if err == nil {
			keys.SetUser(r, user)
			log.WithField("user", user.Username).Info("authenticated request")
			handler(w, r)
			return
		}

		log.WithError(err).Error("token is not valid after being generated in the same request")
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
}
开发者ID:clawio,项目名称:clawiod,代码行数:57,代码来源:webdav.go

示例6: handleTokenError

func (s *svc) handleTokenError(err error, w http.ResponseWriter, r *http.Request) {
	log := keys.MustGetLog(r)
	e := codes.NewErr(codes.BadInputData, "user or password do not match")
	log.WithError(e).Error(codes.BadInputData.String())
	w.WriteHeader(http.StatusBadRequest)
	if err := json.NewEncoder(w).Encode(e); err != nil {
		log.WithError(err).Error("cannot encode")
	}
	return
}
开发者ID:clawio,项目名称:clawiod,代码行数:10,代码来源:token.go

示例7: handleGetError

func (s *svc) handleGetError(err error, w http.ResponseWriter, r *http.Request) {
	log := keys.MustGetLog(r)
	if codeErr, ok := err.(*codes.Err); ok {
		if codeErr.Code == codes.NotFound {
			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
			return
		}
	}
	log.WithError(err).Error("cannot get blob")
	w.WriteHeader(http.StatusInternalServerError)
	return
}
开发者ID:clawio,项目名称:clawiod,代码行数:12,代码来源:get.go

示例8: handleOptionsError

func (s *svc) handleOptionsError(err error, w http.ResponseWriter, r *http.Request) {
	log := keys.MustGetLog(r)
	if codeErr, ok := err.(*codes.Err); ok {
		if codeErr.Code == codes.NotFound {
			w.WriteHeader(http.StatusNotFound)
			return
		}
	}
	log.WithError(err).Error("cannot examine object")
	w.WriteHeader(http.StatusInternalServerError)
	return
}
开发者ID:clawio,项目名称:clawiod,代码行数:12,代码来源:options.go

示例9: handleIfMatchHeader

func (s *svc) handleIfMatchHeader(clientETag, serverETag string, w http.ResponseWriter, r *http.Request) error {
	log := keys.MustGetLog(r)

	// ownCloud adds double quotes around ETag value
	serverETag = fmt.Sprintf(`"%s"`, serverETag)
	if clientETag != serverETag {
		err := fmt.Errorf("etags do not match")
		log.WithError(err).WithField("clientetag", clientETag).WithField("severetag", serverETag).Warn("cannot accept conditional request")
		w.WriteHeader(http.StatusPreconditionFailed)
		return err
	}

	return nil
}
开发者ID:clawio,项目名称:clawiod,代码行数:14,代码来源:put.go

示例10: ExamineObject

// ExamineObject retrieves the information about an object.
func (s *svc) ExamineObject(w http.ResponseWriter, r *http.Request) {
	log := keys.MustGetLog(r)
	user := keys.MustGetUser(r)

	path := mux.Vars(r)["path"]
	oinfo, err := s.metaDataController.ExamineObject(user, path)
	if err != nil {
		s.handleExamineObjectError(err, w, r)
		return
	}
	if err := json.NewEncoder(w).Encode(oinfo); err != nil {
		log.WithError(err).Error("cannot encode")
	}
}
开发者ID:clawio,项目名称:clawiod,代码行数:15,代码来源:examineobject.go

示例11: Put

// Put uploads a blob to user tree.
func (s *svc) Put(w http.ResponseWriter, r *http.Request) {
	if r.Body == nil {
		http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
		return
	}

	user := keys.MustGetUser(r)
	log := keys.MustGetLog(r)

	if s.requestHasContentRange(r) {
		log.Warning("Content-Range header is not accepted on PUT")
		http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
		return
	}

	if s.requestSuffersFinderProblem(r) {
		s.handlerFinderRequest(w, r)
	}

	path := mux.Vars(r)["path"]
	info, err := s.metaDataController.ExamineObject(user, path)
	// if err is not found it is okay to continue
	if err != nil {
		if !s.isNotFoundError(err) {
			s.handlePutError(err, w, r)
			return
		}
	}

	if info != nil && info.Type != entities.ObjectTypeBLOB {
		log.Warn("object is not a blob")
		w.WriteHeader(http.StatusConflict)
		return
	}

	readCloser := http.MaxBytesReader(w, r.Body, int64(s.conf.GetDirectives().WebDAV.UploadMaxFileSize))
	if err := s.dataController.UploadBLOB(user, path, readCloser, ""); err != nil {
		s.handlePutError(err, w, r)
		return
	}

	// if object did not exist, http code is 201, else 204.
	if info == nil {
		w.WriteHeader(http.StatusCreated)
		return
	}
	w.WriteHeader(http.StatusNoContent)
	return
}
开发者ID:clawio,项目名称:clawiod,代码行数:50,代码来源:put.go

示例12: JWTHandlerFunc

// JWTHandlerFunc is a middleware function to authenticate HTTP requests.
func (a *Authenticator) JWTHandlerFunc(handler http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		log := keys.MustGetLog(r)
		token := a.getTokenFromRequest(r)
		user, err := a.CreateUserFromToken(token)
		if err != nil {
			log.Warn("unauthorized")
			http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
			return
		}
		r = keys.SetUser(r, user)
		log.WithField("user", user.Username).Info("authenticated request")
		handler(w, r)
	}
}
开发者ID:clawio,项目名称:clawiod,代码行数:16,代码来源:lib.go

示例13: Download

// Download streams a file to the client.
func (s *svc) Download(w http.ResponseWriter, r *http.Request) {
	log := keys.MustGetLog(r)
	user := keys.MustGetUser(r)

	path := mux.Vars(r)["path"]
	reader, err := s.dataController.DownloadBLOB(user, path)
	if err != nil {
		s.handleDownloadError(err, w, r)
		return
	}
	// add security headers
	w.Header().Add("X-Content-Type-Options", "nosniff")
	w.Header().Add("Content-Type", entities.ObjectTypeBLOBMimeType)
	w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename='%s'", filepath.Base(path)))
	if _, err := io.Copy(w, reader); err != nil {
		log.WithError(err).Error("cannot write response body")
	}
}
开发者ID:clawio,项目名称:clawiod,代码行数:19,代码来源:download.go

示例14: handleMoveObjectError

func (s *svc) handleMoveObjectError(err error, w http.ResponseWriter, r *http.Request) {
	log := keys.MustGetLog(r)
	if codeErr, ok := err.(*codes.Err); ok {
		if codeErr.Code == codes.NotFound {
			log.WithError(err).Error("object not found")
			w.WriteHeader(http.StatusNotFound)
			return
		} else if codeErr.Code == codes.BadInputData {
			log.WithError(err).Error("object cannot be moved")
			w.WriteHeader(http.StatusBadRequest)
			if err := json.NewEncoder(w).Encode(err); err != nil {
				log.WithError(err).Error("cannot encode")
			}
			return
		}
	}
	log.WithError(err).Error("cannot move object")
	w.WriteHeader(http.StatusInternalServerError)
	return
}
开发者ID:clawio,项目名称:clawiod,代码行数:20,代码来源:move.go

示例15: handleUploadError

func (s *svc) handleUploadError(err error, w http.ResponseWriter, r *http.Request) {
	log := keys.MustGetLog(r)

	if err.Error() == "http: request body too large" {
		log.WithError(err).Error("request body max size exceed")
		w.WriteHeader(http.StatusRequestEntityTooLarge)
		return
	}
	if codeErr, ok := err.(*codes.Err); ok {
		if codeErr.Code == codes.NotFound {
			w.WriteHeader(http.StatusNotFound)
			return
		}
		if codeErr.Code == codes.BadChecksum {
			log.WithError(err).Warn("blob corruption")
			w.WriteHeader(http.StatusPreconditionFailed)
			return
		}
	}
	log.WithError(err).Error("cannot save blob")
	w.WriteHeader(http.StatusInternalServerError)
	return
}
开发者ID:clawio,项目名称:clawiod,代码行数:23,代码来源:upload.go


注:本文中的github.com/clawio/clawiod/keys.MustGetLog函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。