Add log and auth modules
This commit is contained in:
commit
bd0f673a8f
2 changed files with 53 additions and 0 deletions
25
modules/auth/auth.go
Normal file
25
modules/auth/auth.go
Normal file
|
@ -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
|
||||||
|
}
|
28
modules/log/log.go
Normal file
28
modules/log/log.go
Normal file
|
@ -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)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue