1
0
mirror of https://github.com/Luzifer/cloudkeys-go.git synced 2024-09-18 23:22:59 +00:00

Replace stdout logging with proper log package

fixes #16

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-12-24 20:10:43 +01:00
parent 884dbd7b1b
commit a0e5005835
Signed by: luzifer
GPG Key ID: DC2729FDD34BE99E
6 changed files with 28 additions and 19 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/flosch/pongo2"
"github.com/gorilla/sessions"
log "github.com/sirupsen/logrus"
)
type ajaxResponse struct {
@ -33,7 +34,7 @@ func ajaxGetHandler(res http.ResponseWriter, r *http.Request, session *sessions.
userFileRaw, err := storage.Read(user.UserFile)
if err != nil {
fmt.Printf("ERR: Unable to read user file: %s\n", err)
log.WithError(err).Error("Could not read user file from storage")
res.Write(ajaxResponse{Error: true}.Bytes())
return nil, nil
}
@ -60,7 +61,7 @@ func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions
userFileRaw, err := storage.Read(user.UserFile)
if err != nil {
fmt.Printf("ERR: Unable to read user file: %s\n", err)
log.WithError(err).Error("Could not read user file from storage")
res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes())
return nil, nil
}
@ -84,7 +85,7 @@ func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions
}
if err := storage.Backup(user.UserFile); err != nil {
fmt.Printf("ERR: Unable to backup user file: %s\n", err)
log.WithError(err).Error("Could not create backup of user file")
res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes())
return nil, nil
}
@ -95,7 +96,7 @@ func ajaxPostHandler(res http.ResponseWriter, r *http.Request, session *sessions
d, _ := userFile.GetData()
if err := storage.Write(user.UserFile, d); err != nil {
fmt.Printf("ERR: Unable to write user file: %s\n", err)
log.WithError(err).Error("Could not write user file to storage")
res.Write(ajaxResponse{Error: true, Type: "storage_error"}.Bytes())
return nil, nil
}

View File

@ -1,11 +1,11 @@
package main
import (
"fmt"
"net/http"
"github.com/flosch/pongo2"
"github.com/gorilla/sessions"
log "github.com/sirupsen/logrus"
)
type httpHelperFunc func(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error)
@ -22,7 +22,7 @@ func httpHelper(f httpHelperFunc) http.HandlerFunc {
template, err := f(res, r, sess, &ctx)
if err != nil {
http.Error(res, "An error ocurred.", http.StatusInternalServerError)
fmt.Printf("ERR: %s\n", err)
log.WithError(err).Error("Unable to execute template")
return
}
@ -31,13 +31,17 @@ func httpHelper(f httpHelperFunc) http.HandlerFunc {
ts.SetBaseDirectory("templates")
tpl, err := ts.FromFile(*template)
if err != nil {
fmt.Printf("ERR: Could not parse template '%s': %s\n", *template, err)
log.WithError(err).WithFields(log.Fields{
"template": *template,
}).Error("Could not parse template")
http.Error(res, "An error ocurred.", http.StatusInternalServerError)
return
}
out, err := tpl.Execute(ctx)
if err != nil {
fmt.Printf("ERR: Unable to execute template '%s': %s\n", *template, err)
log.WithError(err).WithFields(log.Fields{
"template": *template,
}).Error("Could not execute template")
http.Error(res, "An error ocurred.", http.StatusInternalServerError)
return
}

View File

@ -12,6 +12,7 @@ import (
"github.com/flosch/pongo2"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
log "github.com/sirupsen/logrus"
)
func loginHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
@ -28,7 +29,7 @@ func loginHandler(res http.ResponseWriter, r *http.Request, session *sessions.Se
userFileRaw, err := storage.Read(createUserFilename(username))
if err != nil {
fmt.Printf("ERR: Unable to read user file: %s\n", err)
log.WithError(err).Error("Unable to read user file")
(*ctx)["error"] = true
return stringPointer("login.html"), nil
}

10
main.go
View File

@ -13,6 +13,7 @@ import (
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
)
var (
@ -30,18 +31,18 @@ func init() {
}
if _, err := cfg.ParsedStorage(); err != nil {
fmt.Printf("ERR: Please provide a valid storage URI\n")
log.WithError(err).Error("Unable to parse storage URI")
os.Exit(1)
}
if cfg.CookieSigningKey == "" {
cfg.CookieSigningKey = uuid.NewV4().String()[:32]
fmt.Printf("WRN: cookie-authkey was set randomly, this will break your sessions!\n")
log.Warn("cookie-authkey was set randomly, this will break your sessions!")
}
if cfg.CookieEncryptKey == "" {
cfg.CookieEncryptKey = uuid.NewV4().String()[:32]
fmt.Printf("WRN: cookie-encryptkey was set randomly, this will break your sessions!\n")
log.Warn("cookie-encryptkey was set randomly, this will break your sessions!")
}
cookieStore = sessions.NewCookieStore(
@ -53,8 +54,7 @@ func init() {
func main() {
s, err := getStorageAdapter(cfg)
if err != nil {
fmt.Printf("ERR: Could not instanciate storage: %s\n", err)
os.Exit(1)
log.WithError(err).Fatal("Could not instanciate storage")
}
storage = s

View File

@ -1,7 +1,6 @@
package main
import (
"fmt"
"net/http"
"strings"
@ -9,6 +8,7 @@ import (
"github.com/flosch/pongo2"
"github.com/gorilla/sessions"
log "github.com/sirupsen/logrus"
)
func registerHandler(res http.ResponseWriter, r *http.Request, session *sessions.Session, ctx *pongo2.Context) (*string, error) {
@ -29,7 +29,7 @@ func registerHandler(res http.ResponseWriter, r *http.Request, session *sessions
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
fmt.Printf("ERR: Unable to hash users password: %s\n", err)
log.WithError(err).Error("Could not hash user password")
(*ctx)["error"] = true
return stringPointer("register.html"), nil
}
@ -39,7 +39,7 @@ func registerHandler(res http.ResponseWriter, r *http.Request, session *sessions
data, _ := d.GetData()
if err := storage.Write(createUserFilename(username), data); err != nil {
fmt.Printf("ERR: Unable to write user file: %s\n", err)
log.WithError(err).Error("Could not write user file to storage")
(*ctx)["error"] = true
return stringPointer("register.html"), nil
}

View File

@ -2,7 +2,6 @@ package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/url"
@ -10,6 +9,7 @@ import (
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/xuyu/goredis"
)
@ -57,7 +57,10 @@ func (r *RedisStorage) Read(identifier string) (io.Reader, error) {
func (r *RedisStorage) IsPresent(identifier string) bool {
e, err := r.conn.Exists(r.prefix + identifier)
if err != nil {
fmt.Printf("ERR: %s\n", err)
log.WithError(err).WithFields(log.Fields{
"storagedriver": "redis",
"identifier": identifier,
}).Error("Unable to check key existence")
}
return e && err == nil
}