misc

package
v0.0.0-...-3de62b2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2020 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

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

func Atoi(value string) (int, error)

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

func DumpRequest(r *http.Request) string

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

func LogRequest(r *http.Request)

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

func NormalizeName(name string) string

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

func ParseJSON(r *http.Request, v interface{}) error

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

func SplitString(s, sep string, lenRes int) []string

SplitString return the string s splited with the separator sep and the size of result is at least lenRes.

func TokenToUser

func TokenToUser(d data.Data, tokenStr string) (structures.User, error)

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

func ValidateToken

func ValidateToken(d data.Data, token string) bool

ValidateToken check if the token is valid

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL