Skip to content

Commit

Permalink
Merge branch 'master' into feat/support-not-linux
Browse files Browse the repository at this point in the history
  • Loading branch information
whywaita committed Mar 10, 2022
2 parents 085102a + 9c56ef8 commit bc36cda
Show file tree
Hide file tree
Showing 25 changed files with 714 additions and 169 deletions.
38 changes: 38 additions & 0 deletions api/myshoes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# myshoes-sdk-go

The Go SDK for myshoes

## Usage

```go
package main

import (
"context"
"fmt"
"io"
"log"
"net/http"

"github.com/whywaita/myshoes/api/myshoes"
)

func main() {
// Set customized HTTP Client
customHTTPClient := http.DefaultClient
// Set customized logger
customLogger := log.New(io.Discard, "", log.LstdFlags)

client, err := myshoes.NewClient("https://example.com", customHTTPClient, customLogger)
if err != nil {
// ...
}

targets, err := client.ListTarget(context.Background())
if err != nil {
// ...
}

fmt.Println(targets)
}
```
77 changes: 77 additions & 0 deletions api/myshoes/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package myshoes

import (
"context"
"fmt"
"io"
"log"
"net/http"
"net/url"
"path"
"strings"
)

// Client is a client for myshoes
type Client struct {
HTTPClient http.Client
URL *url.URL

UserAgent string
Logger *log.Logger
}

const (
defaultUserAgent = "myshoes-sdk-go"
)

// NewClient create a Client
func NewClient(endpoint string, client *http.Client, logger *log.Logger) (*Client, error) {
u, err := url.Parse(endpoint)
if err != nil {
return nil, fmt.Errorf("failed to parse endpoint: %w", err)
}

httpClient := client
if httpClient == nil {
httpClient = http.DefaultClient
}
l := logger
if l == nil {
// Default is discard logger
l = log.New(io.Discard, "", log.LstdFlags)
}

return &Client{
HTTPClient: *httpClient,
URL: u,

Logger: logger,
}, nil
}

func (c *Client) newRequest(ctx context.Context, method, spath string, body io.Reader) (*http.Request, error) {
u := *c.URL
u.Path = path.Join(c.URL.Path, spath)

req, err := http.NewRequestWithContext(ctx, method, u.String(), body)
if err != nil {
return nil, fmt.Errorf("failed to create a new HTTP request: %w", err)
}

ua := c.UserAgent
if strings.EqualFold(ua, "") {
ua = defaultUserAgent
}
req.Header.Set("User-Agent", ua)

req.Header.Set("Content-Type", "application/json")

return req, nil
}

// Error values
var (
errCreateRequest = "failed to create request: %w"
errRequest = "failed to request: %w"
errDecodeBody = "failed to decodeBody: %w"
)
46 changes: 46 additions & 0 deletions api/myshoes/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package myshoes

import (
"encoding/json"
"fmt"
"net/http"

"github.com/whywaita/myshoes/pkg/web"
)

func decodeBody(resp *http.Response, out interface{}) error {
defer resp.Body.Close()

decoder := json.NewDecoder(resp.Body)
return decoder.Decode(out)
}

func decodeErrorBody(resp *http.Response) error {
var e web.ErrorResponse

if err := decodeBody(resp, &e); err != nil {
return fmt.Errorf(errDecodeBody, err)
}

return fmt.Errorf("%s", e.Error)
}

func (c *Client) request(req *http.Request, out interface{}) error {
c.Logger.Printf("Do request: %+v", req)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return fmt.Errorf("failed to do HTTP request: %w", err)
}

switch {
case resp.StatusCode == http.StatusNoContent:
return nil
case resp.StatusCode >= 400:
return decodeErrorBody(resp)
}

if err := decodeBody(resp, out); err != nil {
return fmt.Errorf(errDecodeBody, err)
}
return nil
}
106 changes: 106 additions & 0 deletions api/myshoes/target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package myshoes

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/whywaita/myshoes/pkg/web"
)

// CreateTarget create a target
func (c *Client) CreateTarget(ctx context.Context, param web.TargetCreateParam) (*web.UserTarget, error) {
spath := "/target"

jb, err := json.Marshal(param)
if err != nil {
return nil, fmt.Errorf("failed to json.Marshal: %w", err)
}

req, err := c.newRequest(ctx, http.MethodPost, spath, bytes.NewBuffer(jb))
if err != nil {
return nil, fmt.Errorf(errCreateRequest, err)
}

var target web.UserTarget
if err := c.request(req, &target); err != nil {
return nil, fmt.Errorf(errRequest, err)
}

return &target, nil
}

// GetTarget get a target
func (c *Client) GetTarget(ctx context.Context, targetID string) (*web.UserTarget, error) {
spath := fmt.Sprintf("/target/%s", targetID)

req, err := c.newRequest(ctx, http.MethodGet, spath, nil)
if err != nil {
return nil, fmt.Errorf(errCreateRequest, err)
}

var target web.UserTarget
if err := c.request(req, &target); err != nil {
return nil, fmt.Errorf(errRequest, err)
}

return &target, nil
}

// UpdateTarget update a target
func (c *Client) UpdateTarget(ctx context.Context, targetID string, param web.TargetCreateParam) (*web.UserTarget, error) {
spath := fmt.Sprintf("/target/%s", targetID)

jb, err := json.Marshal(param)
if err != nil {
return nil, fmt.Errorf("failed to json.Marshal: %w", err)
}

req, err := c.newRequest(ctx, http.MethodPost, spath, bytes.NewBuffer(jb))
if err != nil {
return nil, fmt.Errorf(errCreateRequest, err)
}

var target web.UserTarget
if err := c.request(req, &target); err != nil {
return nil, fmt.Errorf(errRequest, err)
}

return &target, nil
}

// DeleteTarget delete a target
func (c *Client) DeleteTarget(ctx context.Context, targetID string) error {
spath := fmt.Sprintf("/target/%s", targetID)

req, err := c.newRequest(ctx, http.MethodDelete, spath, nil)
if err != nil {
return fmt.Errorf(errCreateRequest, err)
}

var i interface{} // this endpoint return N/A
if err := c.request(req, &i); err != nil {
return fmt.Errorf(errRequest, err)
}

return nil
}

// ListTarget get a list of target
func (c *Client) ListTarget(ctx context.Context) ([]web.UserTarget, error) {
spath := "/target"

req, err := c.newRequest(ctx, http.MethodGet, spath, nil)
if err != nil {
return nil, fmt.Errorf(errCreateRequest, err)
}

var targets []web.UserTarget
if err := c.request(req, targets); err != nil {
return nil, fmt.Errorf(errRequest, err)
}

return targets, nil
}
7 changes: 5 additions & 2 deletions cmd/server/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"strings"
"time"

"github.com/whywaita/myshoes/pkg/logger"

"github.com/whywaita/myshoes/internal/config"
"github.com/whywaita/myshoes/pkg/datastore"
"github.com/whywaita/myshoes/pkg/datastore/mysql"
"github.com/whywaita/myshoes/pkg/gh"
"github.com/whywaita/myshoes/pkg/logger"
"github.com/whywaita/myshoes/pkg/runner"
"github.com/whywaita/myshoes/pkg/starter"
"github.com/whywaita/myshoes/pkg/starter/safety/unlimited"
Expand All @@ -22,6 +22,9 @@ import (

func init() {
config.Load()
if err := gh.InitializeCache(config.Config.GitHub.AppID, config.Config.GitHub.PEMByte); err != nil {
log.Panicf("failed to create a cache: %+v", err)
}
}

func main() {
Expand Down
3 changes: 3 additions & 0 deletions docs/01_01_for_admin_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,8 @@ $ ./myshoes
- `MAX_CONNECTIONS_TO_BACKEND`
- default: 50
- The number of max connections to shoes-provider
- `MAX_CONCURRENCY_DELETING`
- default: 1
- The number of max concurrency of deleting

and more some env values from [shoes provider](https://github.com/whywaita/myshoes-providers).
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ require (
github.com/go-sql-driver/mysql v1.5.0
github.com/google/go-cmp v0.5.6
github.com/google/go-github/v35 v35.2.0
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
github.com/hashicorp/go-plugin v1.4.0
github.com/hashicorp/go-version v1.3.0
github.com/jmoiron/sqlx v1.2.0
github.com/m4ns0ur/httpcache v0.0.0-20200426190423-1040e2e8823f
github.com/ory/dockertest/v3 v3.8.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.11.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA=
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/hashicorp/go-hclog v0.14.1 h1:nQcJDQwIAGnmoUWp8ubocEX40cCml/17YkF6csQLReU=
github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-plugin v1.4.0 h1:b0O7rs5uiJ99Iu9HugEzsM67afboErkHUWddUSpUO3A=
Expand Down Expand Up @@ -143,6 +141,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/m4ns0ur/httpcache v0.0.0-20200426190423-1040e2e8823f h1:MBcrTbmCf7CZa9yAwcB7ArveQb9TPVy4zFnQGz/LiUU=
github.com/m4ns0ur/httpcache v0.0.0-20200426190423-1040e2e8823f/go.mod h1:UawoqorwkpZ58qWiL+nVJM0Po7FrzAdCxYVh9GgTTaA=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type conf struct {
Debug bool
Strict bool // check to registered runner before delete job
MaxConnectionsToBackend int64
MaxConcurrencyDeleting int64
}

// Config Environment keys
Expand All @@ -34,4 +35,5 @@ const (
EnvDebug = "DEBUG"
EnvStrict = "STRICT"
EnvMaxConnectionsToBackend = "MAX_CONNECTIONS_TO_BACKEND"
EnvMaxConcurrencyDeleting = "MAX_CONCURRENCY_DELETING"
)
8 changes: 8 additions & 0 deletions internal/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ func Load() {
}
Config.MaxConnectionsToBackend = numberPB
}
Config.MaxConcurrencyDeleting = 1
if os.Getenv(EnvMaxConcurrencyDeleting) != "" {
numberCD, err := strconv.ParseInt(os.Getenv(EnvMaxConcurrencyDeleting), 10, 64)
if err != nil {
log.Panicf("failed to convert int64 %s: %+v", EnvMaxConcurrencyDeleting, err)
}
Config.MaxConcurrencyDeleting = numberCD
}
}

func checkBinary(p string) (string, error) {
Expand Down
Loading

0 comments on commit bc36cda

Please sign in to comment.