mirror of
https://github.com/QingdaoU/JudgeServer.git
synced 2025-09-26 22:31:25 +08:00
add SetOptions method
有了这个方法就可以随时改变配置
This commit is contained in:
parent
45125eb015
commit
0bc50ecae2
@ -4,8 +4,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"io"
|
"io"
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
"time"
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -105,9 +103,8 @@ func parseResp(body []byte) (*Resp, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
opts *options
|
opts *options
|
||||||
sha256Token string
|
httpClient *http.Client
|
||||||
httpClient *http.Client
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) request(method, path string, body io.Reader) (resp *Resp, err error) {
|
func (c *Client) request(method, path string, body io.Reader) (resp *Resp, err error) {
|
||||||
@ -115,7 +112,7 @@ func (c *Client) request(method, path string, body io.Reader) (resp *Resp, err e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
req.Header.Set("X-Judge-Server-Token", c.sha256Token)
|
req.Header.Set("X-Judge-Server-Token", c.opts.sha256Token)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
httpResp, err := c.httpClient.Do(req)
|
httpResp, err := c.httpClient.Do(req)
|
||||||
@ -162,15 +159,24 @@ func New(endpointURL, token string, timeout time.Duration) *Client {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) SetOptions(options ...Option) {
|
||||||
|
originTimeout := c.opts.Timeout
|
||||||
|
for _, o := range options {
|
||||||
|
o(c.opts)
|
||||||
|
}
|
||||||
|
if c.opts.Timeout != originTimeout {
|
||||||
|
c.httpClient.Timeout = c.opts.Timeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewClient(options ...Option) *Client {
|
func NewClient(options ...Option) *Client {
|
||||||
opts := DefaultOptions
|
opts := DefaultOptions
|
||||||
for _, o := range options {
|
for _, o := range options {
|
||||||
o(opts)
|
o(opts)
|
||||||
}
|
}
|
||||||
sha256Token := sha256.Sum256([]byte(opts.Token))
|
|
||||||
return &Client{
|
return &Client{
|
||||||
opts: opts,
|
opts: opts,
|
||||||
sha256Token: hex.EncodeToString(sha256Token[:]),
|
|
||||||
httpClient: &http.Client{
|
httpClient: &http.Client{
|
||||||
Timeout: opts.Timeout,
|
Timeout: opts.Timeout,
|
||||||
},
|
},
|
||||||
|
@ -58,12 +58,21 @@ print(int(s1[0]) + int(s1[1]))
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// 创建一个client。 这句代码等价于 judge.New("http://127.0.0.1:12358", "YOUR_TOKEN_HERE", 0)
|
// 创建一个client。 这句代码等价于
|
||||||
client := judge.NewClient(
|
|
||||||
judge.WithEndpointURL("http://127.0.0.1:12358"),
|
// 1.
|
||||||
judge.WithToken("YOUR_TOKEN_HERE"),
|
//client := judge.NewClient(
|
||||||
judge.WithTimeout(0),
|
// judge.WithEndpointURL("http://127.0.0.1:12358"),
|
||||||
)
|
// judge.WithToken("YOUR_TOKEN_HERE"),
|
||||||
|
// judge.WithTimeout(0),
|
||||||
|
//)
|
||||||
|
|
||||||
|
// 2.
|
||||||
|
// client := judge.New("http://127.0.0.1:12358", "YOUR_TOKEN_HERE", 0)
|
||||||
|
|
||||||
|
// 3.
|
||||||
|
client := judge.NewClient(judge.WithTimeout(0))
|
||||||
|
client.SetOptions(judge.WithEndpointURL("http://127.0.0.1:12358"), judge.WithToken("YOUR_TOKEN_HERE"))
|
||||||
|
|
||||||
fmt.Println("ping:")
|
fmt.Println("ping:")
|
||||||
resp, err := client.Ping()
|
resp, err := client.Ping()
|
||||||
|
@ -1,11 +1,16 @@
|
|||||||
package judge
|
package judge
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
)
|
||||||
|
|
||||||
type options struct {
|
type options struct {
|
||||||
// scheme://host:port .
|
// scheme://host:port .
|
||||||
EndpointURL string
|
EndpointURL string
|
||||||
Token string
|
Token string
|
||||||
|
sha256Token string
|
||||||
// 请求超时时间
|
// 请求超时时间
|
||||||
// 如果 Timeout 为 0,那么意味着不会超时
|
// 如果 Timeout 为 0,那么意味着不会超时
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
@ -28,6 +33,8 @@ func WithEndpointURL(u string) Option {
|
|||||||
func WithToken(token string) Option {
|
func WithToken(token string) Option {
|
||||||
return func(o *options) {
|
return func(o *options) {
|
||||||
o.Token = token
|
o.Token = token
|
||||||
|
sha256Token := sha256.Sum256([]byte(token))
|
||||||
|
o.sha256Token = hex.EncodeToString(sha256Token[:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user