JudgeServer/client/go/options.go
taoyu 0bc50ecae2 add SetOptions method
有了这个方法就可以随时改变配置
2018-12-10 19:39:13 +08:00

46 lines
830 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package judge
import (
"time"
"crypto/sha256"
"encoding/hex"
)
type options struct {
// scheme://host:port .
EndpointURL string
Token string
sha256Token string
// 请求超时时间
// 如果 Timeout 为 0那么意味着不会超时
Timeout time.Duration
}
var DefaultOptions = &options{
EndpointURL: "http://127.0.0.1:12358",
Token: "YOUR_TOKEN_HERE",
Timeout: 10 * time.Second,
}
type Option func(*options)
func WithEndpointURL(u string) Option {
return func(o *options) {
o.EndpointURL = u
}
}
func WithToken(token string) Option {
return func(o *options) {
o.Token = token
sha256Token := sha256.Sum256([]byte(token))
o.sha256Token = hex.EncodeToString(sha256Token[:])
}
}
func WithTimeout(timeout time.Duration) Option {
return func(o *options) {
o.Timeout = timeout
}
}