Skip to content

Commit

Permalink
add function comments
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Penfound <[email protected]>
  • Loading branch information
kpenfound committed Nov 4, 2023
1 parent 5ad1957 commit 73f2591
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
9 changes: 6 additions & 3 deletions DEMO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## The Greetings API App

Familiarize yourself with the greetings-api project

### Backend

Lives at the repo root
Expand Down Expand Up @@ -75,12 +77,12 @@ Deploy the project
This deploys the backend to fly.io at https://dagger-demo.fly.dev/ and the frontend to
Netlify at https://dagger-demo.netlify.app/

### Demo
## Demo

- Lead with [Daggerverse](https://daggerverse.dev)
- browse to Hugo module
- run module against `./frontend`: `dagger download -m github.com/jedevc/daggerverse/hugo build --target ./ci/frontend`
- Now pull together multiple modules in `./ci`
- Now pull together multiple modules in `./ci`. This ci uses:
- hugo module in `ci/frontend/main.go`
- golang module in `ci/frontend/main.go` and `ci/backend/main.go`
- proxy module in `ci/main.go` <- written in python!
Expand All @@ -97,13 +99,14 @@ Netlify at https://dagger-demo.netlify.app/
- `dagger functions -m ./ci`
- `dagger serve ./ci serve --help`
- `dagger serve -m ./ci -p 8080,8081 serve --dir "."`
- `curl localhost:8080`
- navigate to [localhost:8081](http://localhost:8081/) in browser
- notice "Hello Kubecon!" greeting coming from backend API
- CI
- push a commit
- show `.github/workflows/test.yml`
- show `.circleci/config.yml`
- see PR in Github Actions (And CircleCI)
- see run in Github Actions (And CircleCI)
- Cloud
- look at all runs
- look at a run TODO: need a link
Expand Down
6 changes: 6 additions & 0 deletions ci/backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ import (

type Backend struct{}

// Return the compiled backend binary for a particular architecture
func (b *Backend) Binary(dir *Directory, arch Optional[string]) *File {
d := b.Build(dir, arch)
return d.File("greetings-api")
}

// Run the unit tests for the backend
func (b *Backend) UnitTest(ctx context.Context, dir *Directory) (string, error) {
return dag.
Golang().
WithProject(dir).
Test(ctx, []string{"./..."})
}

// Build the backend
func (b *Backend) Build(dir *Directory, arch Optional[string]) *Directory {
archStr := arch.GetOr(runtime.GOARCH)
return dag.
Expand All @@ -28,13 +31,15 @@ func (b *Backend) Build(dir *Directory, arch Optional[string]) *Directory {
Build([]string{}, GolangBuildOpts{ Arch: archStr })
}

// Lint the backend Go code
func (b *Backend) Lint(ctx context.Context, dir *Directory) (string, error) {
return dag.
Golang().
WithProject(dir).
GolangciLint(ctx)
}

// Get a container ready to run the backend
func (b *Backend) Container(dir *Directory, arch Optional[string]) *Container {
archStr := arch.GetOr(runtime.GOARCH)
bin := b.Binary(dir, arch)
Expand All @@ -46,6 +51,7 @@ func (b *Backend) Container(dir *Directory, arch Optional[string]) *Container {
WithExposedPort(8080)
}

// Get a Service to run the backend
func (b *Backend) Serve(dir *Directory) *Service {
return b.Container(dir, Opt(runtime.GOARCH)).AsService()
}
Expand Down
4 changes: 4 additions & 0 deletions ci/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@ import "context"

type Frontend struct{}

// Test the frontend
func (f *Frontend) UnitTest(ctx context.Context, dir *Directory) (string, error) {
return dag.
Golang().
WithProject(dir).
Test(ctx, []string{"./..."})
}

// Build the frontend hugo static site
func (f *Frontend) Build(dir *Directory, env Optional[string]) *Directory {
envStr := env.GetOr("dev")
return dag.
Hugo().
Build(dir, HugoBuildOpts{ HugoEnv: envStr })
}

// Lint the frontend Go code
func (f *Frontend) Lint(ctx context.Context, dir *Directory) (string, error) {
return dag.
Golang().
WithProject(dir).
GolangciLint(ctx)
}

// Get a service to run the frontend webservice
func (f *Frontend) Serve(dir *Directory) *Service {
build := f.Build(dir, Opt("dev"))

Expand Down
9 changes: 9 additions & 0 deletions ci/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (

type Greetings struct{}

// Run unit tests for the project
func (g *Greetings) UnitTest(ctx context.Context, dir *Directory) (string, error) {
backendResult, err := dag.Backend().UnitTest(ctx, dir)
if err != nil {
Expand All @@ -21,6 +22,7 @@ func (g *Greetings) UnitTest(ctx context.Context, dir *Directory) (string, error
return backendResult, nil
}

// Lint the Go code in the project
func (g *Greetings) Lint(ctx context.Context, dir *Directory) (string, error) {
backendResult, err := dag.Backend().Lint(ctx, dir)
if err != nil {
Expand All @@ -29,12 +31,14 @@ func (g *Greetings) Lint(ctx context.Context, dir *Directory) (string, error) {
return backendResult, nil
}

// Build the backend and frontend for a specified environment
func (g *Greetings) Build(dir *Directory, env string) *Directory {
return dag.Directory().
WithFile("/build/greetings-api", dag.Backend().Binary(dir)).
WithDirectory("build/website/", dag.Frontend().Build(dir.Directory("website"), FrontendBuildOpts{Env: env}))
}

// Serve the backend and frontend to 8080 and 8081 respectively
func (g *Greetings) Serve(dir *Directory) *Service {
backendService := dag.Backend().Serve(dir)
frontendService := dag.Frontend().Serve(dir.Directory("website"))
Expand All @@ -45,6 +49,7 @@ func (g *Greetings) Serve(dir *Directory) *Service {
Service()
}

// Create a GitHub release
func (g *Greetings) Release(ctx context.Context, dir *Directory, tag string, ghToken *Secret) (string, error) {
// Get build
build := g.Build(dir, "netlify")
Expand All @@ -61,6 +66,7 @@ func (g *Greetings) Release(ctx context.Context, dir *Directory, tag string, ghT
return dag.GithubRelease().Create(ctx, REPO, tag, title, ghToken, GithubReleaseCreateOpts{Assets: assets})
}

// Deploy the project to fly and netlify
func (g *Greetings) Deploy(ctx context.Context, dir *Directory, flyToken *Secret, netlifyToken *Secret, registryUser string, registryPass *Secret) (string, error) {
// Backend
backendAmd64 := dag.Backend().Container(dir, BackendContainerOpts{Arch: "amd64"})
Expand Down Expand Up @@ -93,6 +99,7 @@ func (g *Greetings) Deploy(ctx context.Context, dir *Directory, flyToken *Secret
return fmt.Sprintf("BACKEND\n\n%s\n\nFRONTEND\n\n%s", backendResult, frontendResult), nil
}

// Run the whole CI pipeline
func (g *Greetings) Ci(
ctx context.Context,
dir *Directory,
Expand Down Expand Up @@ -142,6 +149,7 @@ func (g *Greetings) Ci(
return out, nil
}

// Run the whole CI pipeline for a particular commit
func (g *Greetings) CiRemote(
ctx context.Context,
commit string,
Expand All @@ -161,6 +169,7 @@ func (g *Greetings) CiRemote(
infisicalToken,
)
}

func fly_deploy(ctx context.Context, imageRef string, token *Secret) (string, error) {
app := "dagger-demo"
out, err := dag.Fly().Deploy(ctx, app, imageRef, token)
Expand Down

0 comments on commit 73f2591

Please sign in to comment.