Browse Source

En cours de développement

master
losyme 7 months ago
parent
commit
0c8966b4ae
  1. 51
      client/client.go
  2. 69
      client/endpoint.go
  3. 2
      workers/workers.go

51
client/client.go

@ -6,10 +6,55 @@
package client
type Client struct{}
import (
"crypto/tls"
func New(c *Config) *Client {
return &Client{}
"forge.chapril.org/losyme/errors"
)
type Client struct {
endpoints []*endpoint
}
func buildTLSCertificate(cfg *Config) (*tls.Certificate, error) {
if cfg.Cert == "" || cfg.Key == "" {
return nil, nil
}
cert, err := tls.LoadX509KeyPair(cfg.Cert, cfg.Key)
if err != nil {
return nil, err
}
return &cert, nil
}
func New(cfg *Config) (*Client, error) {
if err := cfg.validate(); err != nil {
return nil, errors.WithMessage(err, "sdk.client") //////////////////////////////////////////////////////////////
}
endpoints := make([]*endpoint, 0, len(cfg.Endpoints))
tlsCert, err := buildTLSCertificate(cfg)
if err != nil {
return nil, err
}
for _, ce := range cfg.Endpoints {
ep, err := newEndpoint(cfg, ce, tlsCert)
if err != nil {
return nil, err
}
endpoints = append(endpoints, ep)
}
c := &Client{
endpoints: endpoints,
}
return c, nil
}
/*

69
client/endpoint.go

@ -0,0 +1,69 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### sdk ####### Copyright (c) 2021 losyme ###################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package client
import (
"crypto/tls"
"crypto/x509"
"net/http"
"os"
"time"
)
type endpoint struct {
endpoint *Endpoint
client *http.Client
}
func buildTransport(ce *Endpoint, tlsCert *tls.Certificate) (http.RoundTripper, error) {
if ce.CA == "" {
return nil, nil
}
buf, err := os.ReadFile(ce.CA)
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(buf)
t := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: certPool,
},
}
if tlsCert != nil {
t.TLSClientConfig.Certificates = []tls.Certificate{*tlsCert}
}
return t, nil
}
func newEndpoint(cfg *Config, ce *Endpoint, tlsCert *tls.Certificate) (*endpoint, error) {
t, err := buildTransport(ce, tlsCert)
if err != nil {
return nil, err
}
c := &http.Client{
Transport: t,
Timeout: 10 * time.Second, // AFINIR
}
ep := &endpoint{
endpoint: ce,
client: c,
}
return ep, nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

2
workers/workers.go

@ -29,7 +29,7 @@ type Workers struct {
func New(cfg *Config) (*Workers, error) {
if err := cfg.validate(); err != nil {
return nil, errors.WithMessage(err, "workers") /////////////////////////////////////////////////////////////////
return nil, errors.WithMessage(err, "sdk.workers") /////////////////////////////////////////////////////////////
}
dashboard := newDashboard()

Loading…
Cancel
Save