commit bd0f673a8f7e91964ba77e45c2be6724843de870 Author: Matt Baer Date: Sun Sep 4 14:49:51 2016 -0400 Add log and auth modules diff --git a/modules/auth/auth.go b/modules/auth/auth.go new file mode 100644 index 0000000..4f17546 --- /dev/null +++ b/modules/auth/auth.go @@ -0,0 +1,25 @@ +package auth + +import ( + uuid "github.com/nu7hatch/gouuid" + "strings" + + "code.as/writeas/web/modules/log" +) + +// GetToken parses out the user token from an Authorization header. +func GetToken(header string) []byte { + var accessToken []byte + if len(header) > 0 { + f := strings.Fields(header) + if len(f) == 2 && f[0] == "Token" { + t, err := uuid.ParseHex(f[1]) + if err != nil { + log.Error("Couldn't parseHex on '%s': %v", accessToken, err) + } else { + accessToken = t[:] + } + } + } + return accessToken +} diff --git a/modules/log/log.go b/modules/log/log.go new file mode 100644 index 0000000..67fa597 --- /dev/null +++ b/modules/log/log.go @@ -0,0 +1,28 @@ +// log holds loggers for the application +package log + +import ( + "log" + "os" +) + +var ( + i *log.Logger + e *log.Logger +) + +// Init creates the local loggers used in the app. +func Init() { + i = log.New(os.Stdout, "", log.Ldate|log.Ltime) + e = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) +} + +// Info logs an informational message to Stdout. +func Info(s string, v ...interface{}) { + i.Printf(s, v) +} + +// Error logs an error to Stderr. +func Error(s string, v ...interface{}) { + e.Printf(s, v) +}