-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from phr3nzy/feat/v2
feat: v2
- Loading branch information
Showing
6 changed files
with
454 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# golang .gitignore paths | ||
|
||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# HTML, JS, CSS, images | ||
*.html | ||
*.html~ | ||
*.css | ||
*.css~ | ||
*.js | ||
*.js~ | ||
*.gif | ||
*.png | ||
*.jpg | ||
*.jpeg | ||
|
||
# Compiled Object files, Static and Dynamic libs (Shared Objects) | ||
*.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/phr3nzy/tango" | ||
) | ||
|
||
func main() { | ||
type state struct { | ||
Attempts int | ||
PluginState string | ||
RequestBodyReader *bufio.Reader | ||
Content []byte | ||
} | ||
|
||
type services struct { | ||
http *http.Client | ||
} | ||
|
||
ctx := &tango.MachineContext[services, state]{ | ||
Services: services{ | ||
http: &http.Client{ | ||
Timeout: time.Second * 10, | ||
}, | ||
}, | ||
State: state{ | ||
Attempts: 0, | ||
PluginState: "plugin state", | ||
}, | ||
PreviousResult: nil, | ||
} | ||
|
||
config := &tango.MachineConfig[services, state]{ | ||
Log: true, | ||
LogLevel: "info", | ||
Plugins: []tango.Plugin[services, state]{}, | ||
} | ||
|
||
steps := []tango.Step[services, state]{ | ||
{ | ||
Name: "visit website", | ||
Execute: func(ctx *tango.MachineContext[services, state]) (*tango.Response[services, state], error) { | ||
ctx.State.Attempts++ | ||
resp, err := ctx.Services.http.Get("https://google.com/") | ||
if err != nil { | ||
return ctx.Machine.Error(err), nil | ||
} | ||
defer resp.Body.Close() | ||
|
||
ctx.State.RequestBodyReader = bufio.NewReader(resp.Body) | ||
ctx.State.Content, err = io.ReadAll(ctx.State.RequestBodyReader) | ||
if err != nil { | ||
return ctx.Machine.Error(err.Error()), nil | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
return ctx.Machine.Error(fmt.Sprintf( | ||
"status code: %d, body: %s", | ||
resp.StatusCode, | ||
string(ctx.State.Content), | ||
)), nil | ||
} | ||
|
||
return ctx.Machine.Next("page visited"), nil | ||
}, | ||
Compensate: func(ctx *tango.MachineContext[services, state]) (*tango.Response[services, state], error) { | ||
return ctx.Machine.Done("Compensate"), nil | ||
}, | ||
}, | ||
{ | ||
Name: "write to disk", | ||
Execute: func(ctx *tango.MachineContext[services, state]) (*tango.Response[services, state], error) { | ||
// if the file exists, return an error | ||
_, err := os.Stat("examples/output.html") | ||
if err == nil { | ||
return ctx.Machine.Error("file already exists"), nil | ||
} | ||
|
||
f, err := os.Create("examples/output.html") | ||
if err != nil { | ||
return ctx.Machine.Error(err.Error()), nil | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.Write(ctx.State.Content) | ||
if err != nil { | ||
return ctx.Machine.Error(err.Error()), nil | ||
} | ||
|
||
return ctx.Machine.Done("wrote the file to disk"), nil | ||
}, | ||
Compensate: func(ctx *tango.MachineContext[services, state]) (*tango.Response[services, state], error) { | ||
_, err := os.Stat("examples/output.html") | ||
if err == nil { | ||
fmt.Println("removing file") | ||
err = os.Remove("examples/output.html") | ||
if err != nil { | ||
return ctx.Machine.Error(err.Error()), nil | ||
} | ||
} | ||
|
||
return ctx.Machine.Next("compensated"), nil | ||
}, | ||
}, | ||
} | ||
|
||
machine := tango.NewMachine[services, state]( | ||
"scraper", | ||
steps, | ||
ctx, | ||
config, | ||
&tango.ConcurrentStrategy[services, state]{ | ||
Concurrency: 1, | ||
}, | ||
) | ||
ctx.Machine = machine | ||
|
||
response, err := machine.Run() | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
fmt.Println(response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.