Documentation ¶
Index ¶
- Variables
- func Atoi(value string) (int, error)
- func DumpRequest(r *http.Request) string
- func LogRequest(r *http.Request)
- func NormalizeConsumable(c structures.Consumable) structures.Consumable
- func NormalizeName(name string) string
- func NormalizeQuery(q structures.Query) structures.Query
- func ParseJSON(r *http.Request, v interface{}) error
- func SplitString(s, sep string, lenRes int) []string
- func TokenToUser(d data.Data, tokenStr string) (structures.User, error)
- func UserToToken(u structures.User) (string, error)
- func ValidateToken(d data.Data, token string) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // TokensKey is the secret key for all tokens. // warning: On server reboot, all token are invalidated TokensKey = []byte(hash.GenerateKey(666)) // ExpToken number of days before the token expire ExpToken = time.Duration(14) // Logger used to log output Logger = log.New(os.Stdout, "Misc: ", log.Ldate|log.Ltime|log.Lshortfile) )
Functions ¶
func Atoi ¶
Atoi is a wrapper around strconv.Atoi
Example ¶
package main import ( "fmt" "github.com/eiko-team/eiko/misc/misc" ) func main() { fmt.Println(misc.Atoi("42")) }
Output: 42 <nil>
Example (Error) ¶
package main import ( "fmt" "github.com/eiko-team/eiko/misc/misc" ) func main() { fmt.Println(misc.Atoi("test")) }
Output: 0 strconv.ParseInt: parsing "test": invalid syntax
func DumpRequest ¶
DumpRequest return the request in a string format
Example ¶
package main import ( "fmt" "net/http" "github.com/eiko-team/eiko/misc/misc" ) func main() { r, _ := http.NewRequest("GET", "/index.html", nil) fmt.Println(misc.DumpRequest(r)) }
Output: GET /index.html HTTP/1.1
func LogRequest ¶
LogRequest logs a *http.Request using the Logger
Example ¶
package main import ( "net/http" "github.com/eiko-team/eiko/misc/misc" ) func main() { r, _ := http.NewRequest("GET", "/index.html", nil) misc.LogRequest(r) // Misc: 2000/01/01 00:00:00 misc.go:56: GET /index.html HTTP/1.1 }
Output:
func NormalizeConsumable ¶
func NormalizeConsumable(c structures.Consumable) structures.Consumable
NormalizeConsumable is used to normalize a consumable
func NormalizeName ¶
NormalizeName is used to normalize a name
func NormalizeQuery ¶
func NormalizeQuery(q structures.Query) structures.Query
NormalizeQuery is used to normalize a query
func ParseJSON ¶
ParseJSON generic function to parse request body, extract it's content and fill the struct
Example ¶
package main import ( "fmt" "net/http" "strings" "github.com/eiko-team/eiko/misc/misc" ) func main() { type Example struct { Name string `json:"name"` } req, _ := http.NewRequest("POST", "/test", strings.NewReader(`{"name":"test"}`)) var example Example misc.ParseJSON(req, &example) fmt.Printf("%+v", example) }
Output: {Name:test}
Example (Error) ¶
package main import ( "fmt" "net/http" "strings" "github.com/eiko-team/eiko/misc/misc" ) func main() { type Example struct { Name string `json:"name"` } req, _ := http.NewRequest("POST", "/test", strings.NewReader(`bad Json`)) var example Example err := misc.ParseJSON(req, &example) fmt.Printf("%+v\n", example) fmt.Printf("%s", err.Error()) }
Output: {Name:} invalid character 'b' looking for beginning of value
func SplitString ¶
SplitString return the string s splited with the separator sep and the size of result is at least lenRes.
func TokenToUser ¶
TokenToUser convert the Token to user's information
func UserToToken ¶
func UserToToken(u structures.User) (string, error)
UserToToken convert the user information to a valid token
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.