-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23ef7b4
commit 47aad71
Showing
13 changed files
with
643 additions
and
6 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,26 @@ | ||
name: ci | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
ci: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Setup go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: 'go.mod' | ||
- name: Build | ||
run: go build -v ./... | ||
- name: Lint | ||
uses: golangci/golangci-lint-action@v3 | ||
- name: Test | ||
run: go test -v ./... |
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,29 @@ | ||
name: goreleaser | ||
|
||
on: | ||
push: | ||
tags: | ||
- '*' | ||
|
||
permissions: | ||
contents: write | ||
|
||
jobs: | ||
goreleaser: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
- run: git fetch --force --tags | ||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version-file: 'go.mod' | ||
cache: true | ||
- uses: goreleaser/goreleaser-action@v4 | ||
with: | ||
distribution: goreleaser | ||
version: latest | ||
args: release --clean | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
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
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,14 @@ | ||
builds: | ||
- id: vidai | ||
binary: vidai | ||
main: ./cmd/vidai | ||
goarch: | ||
- amd64 | ||
- arm64 | ||
- arm | ||
archives: | ||
- id: vidai | ||
builds: | ||
- vidai | ||
format: zip | ||
name_template: 'vidai_{{ .Version }}_{{- if eq .Os "darwin" }}macos{{- else }}{{ .Os }}{{ end }}_{{ .Arch }}' |
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,15 @@ | ||
# builder image | ||
FROM golang:alpine as builder | ||
ARG TARGETPLATFORM | ||
COPY . /src | ||
WORKDIR /src | ||
RUN apk add --no-cache make bash git | ||
RUN make app-build PLATFORMS=$TARGETPLATFORM | ||
|
||
# running image | ||
FROM alpine | ||
WORKDIR /home | ||
COPY --from=builder /src/bin/vidai-* /bin/vidai | ||
|
||
# executable | ||
ENTRYPOINT [ "/bin/vidai" ] |
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,74 @@ | ||
#!/bin/bash | ||
|
||
SHELL = /bin/bash | ||
PLATFORMS ?= linux/amd64 darwin/amd64 windows/amd64 | ||
IMAGE_PREFIX ?= igolaizola | ||
REPO_NAME ?= vidai | ||
COMMIT_SHORT ?= $(shell git rev-parse --verify --short HEAD) | ||
VERSION ?= $(COMMIT_SHORT) | ||
VERSION_NOPREFIX ?= $(shell echo $(VERSION) | sed -e 's/^[[v]]*//') | ||
|
||
# Build the binaries for the current platform | ||
.PHONY: build | ||
build: | ||
os=$$(go env GOOS); \ | ||
arch=$$(go env GOARCH); \ | ||
PLATFORMS="$$os/$$arch" make app-build | ||
|
||
# Build the binaries | ||
# Example: PLATFORMS=linux/amd64 make app-build | ||
.PHONY: app-build | ||
app-build: | ||
@for platform in $(PLATFORMS) ; do \ | ||
os=$$(echo $$platform | cut -f1 -d/); \ | ||
arch=$$(echo $$platform | cut -f2 -d/); \ | ||
arm=$$(echo $$platform | cut -f3 -d/); \ | ||
arm=$${arm#v}; \ | ||
ext=""; \ | ||
if [ "$$os" == "windows" ]; then \ | ||
ext=".exe"; \ | ||
fi; \ | ||
file=./bin/$(REPO_NAME)-$(VERSION_NOPREFIX)-$$(echo $$platform | tr / -)$$ext; \ | ||
GOOS=$$os GOARCH=$$arch GOARM=$$arm CGO_ENABLED=0 \ | ||
go build \ | ||
-a -x -tags netgo,timetzdata -installsuffix cgo -installsuffix netgo \ | ||
-ldflags " \ | ||
-X main.Version=$(VERSION_NOPREFIX) \ | ||
-X main.GitRev=$(COMMIT_SHORT) \ | ||
" \ | ||
-o $$file \ | ||
./cmd/$(REPO_NAME); \ | ||
if [ $$? -ne 0 ]; then \ | ||
exit 1; \ | ||
fi; \ | ||
chmod +x $$file; \ | ||
done | ||
|
||
# Build the docker image | ||
# Example: PLATFORMS=linux/amd64 make docker-build | ||
.PHONY: docker-build | ||
docker-build: | ||
rm -rf bin; \ | ||
@platforms=($(PLATFORMS)); \ | ||
platform=$${platforms[0]}; \ | ||
if [[ $${#platforms[@]} -ne 1 ]]; then \ | ||
echo "Multi-arch build not supported"; \ | ||
exit 1; \ | ||
fi; \ | ||
docker build --platform $$platform -t $(IMAGE_PREFIX)/$(REPO_NAME):$(VERSION) .; \ | ||
if [ $$? -ne 0 ]; then \ | ||
exit 1; \ | ||
fi | ||
|
||
# Build the docker images using buildx | ||
# Example: PLATFORMS="linux/amd64 darwin/amd64 windows/amd64" make docker-buildx | ||
.PHONY: docker-buildx | ||
docker-buildx: | ||
@platforms=($(PLATFORMS)); \ | ||
platform=$$(IFS=, ; echo "$${platforms[*]}"); \ | ||
docker buildx build --platform $$platform -t $(IMAGE_PREFIX)/$(REPO_NAME):$(VERSION) . | ||
|
||
# Clean binaries | ||
.PHONY: clean | ||
clean: | ||
rm -rf bin |
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 |
---|---|---|
@@ -1,2 +1,11 @@ | ||
# vidai | ||
Video generation using AI | ||
|
||
Skeleton for golang projects | ||
|
||
## Usage | ||
|
||
```bash | ||
git clone https://github.com/igolaizola/vidai project | ||
cd project | ||
./rename.sh owner/project | ||
``` |
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,109 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"runtime/debug" | ||
"strings" | ||
"time" | ||
|
||
"github.com/igolaizola/vidai" | ||
"github.com/peterbourgon/ff/v3" | ||
"github.com/peterbourgon/ff/v3/ffcli" | ||
) | ||
|
||
// Build flags | ||
var version = "" | ||
var commit = "" | ||
var date = "" | ||
|
||
func main() { | ||
// Create signal based context | ||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) | ||
defer cancel() | ||
|
||
// Launch command | ||
cmd := newCommand() | ||
if err := cmd.ParseAndRun(ctx, os.Args[1:]); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func newCommand() *ffcli.Command { | ||
fs := flag.NewFlagSet("vidai", flag.ExitOnError) | ||
|
||
return &ffcli.Command{ | ||
ShortUsage: "vidai [flags] <subcommand>", | ||
FlagSet: fs, | ||
Exec: func(context.Context, []string) error { | ||
return flag.ErrHelp | ||
}, | ||
Subcommands: []*ffcli.Command{ | ||
newVersionCommand(), | ||
newRunCommand(), | ||
}, | ||
} | ||
} | ||
|
||
func newVersionCommand() *ffcli.Command { | ||
return &ffcli.Command{ | ||
Name: "version", | ||
ShortUsage: "vidai version", | ||
ShortHelp: "print version", | ||
Exec: func(ctx context.Context, args []string) error { | ||
v := version | ||
if v == "" { | ||
if buildInfo, ok := debug.ReadBuildInfo(); ok { | ||
v = buildInfo.Main.Version | ||
} | ||
} | ||
if v == "" { | ||
v = "dev" | ||
} | ||
versionFields := []string{v} | ||
if commit != "" { | ||
versionFields = append(versionFields, commit) | ||
} | ||
if date != "" { | ||
versionFields = append(versionFields, date) | ||
} | ||
fmt.Println(strings.Join(versionFields, " ")) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func newRunCommand() *ffcli.Command { | ||
cmd := "run" | ||
fs := flag.NewFlagSet(cmd, flag.ExitOnError) | ||
_ = fs.String("config", "", "config file (optional)") | ||
debug := fs.Bool("debug", false, "debug mode") | ||
wait := fs.Duration("wait", 2*time.Second, "wait time between requests") | ||
token := fs.String("token", "", "runway token") | ||
image := fs.String("image", "", "source image") | ||
|
||
return &ffcli.Command{ | ||
Name: cmd, | ||
ShortUsage: fmt.Sprintf("vidai %s [flags] <key> <value data...>", cmd), | ||
Options: []ff.Option{ | ||
ff.WithConfigFileFlag("config"), | ||
ff.WithConfigFileParser(ff.PlainParser), | ||
ff.WithEnvVarPrefix("VIDAI"), | ||
}, | ||
ShortHelp: fmt.Sprintf("vidai %s command", cmd), | ||
FlagSet: fs, | ||
Exec: func(ctx context.Context, args []string) error { | ||
if *token == "" { | ||
return fmt.Errorf("token is required") | ||
} | ||
if *image == "" { | ||
return fmt.Errorf("image is required") | ||
} | ||
return vidai.Run(ctx, *debug, *wait, *token, *image) | ||
}, | ||
} | ||
} |
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,5 @@ | ||
module github.com/igolaizola/vidai | ||
|
||
go 1.20 | ||
|
||
require github.com/peterbourgon/ff/v3 v3.3.0 |
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,2 @@ | ||
github.com/peterbourgon/ff/v3 v3.3.0 h1:PaKe7GW8orVFh8Unb5jNHS+JZBwWUMa2se0HM6/BI24= | ||
github.com/peterbourgon/ff/v3 v3.3.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ= |
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,47 @@ | ||
package ratelimit | ||
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Lock interface { | ||
Lock(ctx context.Context) func() | ||
LockWithDuration(ctx context.Context, d time.Duration) func() | ||
} | ||
|
||
type lock struct { | ||
lck *sync.Mutex | ||
duration time.Duration | ||
} | ||
|
||
// New creates a new rate limit lock. | ||
func New(d time.Duration) Lock { | ||
return &lock{ | ||
lck: &sync.Mutex{}, | ||
duration: d, | ||
} | ||
} | ||
|
||
// Lock locks the rate limit for the given duration and returns a function that | ||
// unlocks the rate limit with a delay time based on the given duration. | ||
func (l *lock) LockWithDuration(ctx context.Context, d time.Duration) func() { | ||
l.lck.Lock() | ||
// Apply a factor between 0.85 and 1.15 to the duration | ||
d = time.Duration(float64(d) * (0.85 + rand.Float64()*0.3)) | ||
return func() { | ||
defer l.lck.Unlock() | ||
select { | ||
case <-ctx.Done(): | ||
case <-time.After(d): | ||
} | ||
} | ||
} | ||
|
||
// Lock locks the rate limit for the default duration and returns a function that | ||
// unlocks the rate limit with a delay time based on the default duration. | ||
func (l *lock) Lock(ctx context.Context) func() { | ||
return l.LockWithDuration(ctx, l.duration) | ||
} |
Oops, something went wrong.