-
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 #7 from cofide/ping-pong-mesh
Add ping-pong-mesh demo and build/lint CI workflow
- Loading branch information
Showing
7 changed files
with
203 additions
and
4 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,41 @@ | ||
name: ci | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
lint: | ||
name: lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: Setup Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version-file: go.mod | ||
- name: Install golangci-lint | ||
uses: golangci/golangci-lint-action@v6 | ||
with: | ||
args: --timeout=5m | ||
|
||
build-test: | ||
name: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: Install just | ||
uses: taiki-e/install-action@just | ||
- name: Setup Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version-file: go.mod | ||
- name: Setup Ko | ||
uses: ko-build/[email protected] | ||
env: | ||
KO_DOCKER_REPO: ko.local | ||
- name: Build all demos | ||
run: just build-demos |
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
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,87 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log" | ||
"log/slog" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func main() { | ||
if err := run(context.Background(), getEnv()); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
type Env struct { | ||
ServerAddress string | ||
ServerPort int | ||
} | ||
|
||
func getEnvOrPanic(variable string) string { | ||
v, ok := os.LookupEnv(variable) | ||
if !ok { | ||
panic(fmt.Sprintf("expected environment variable %s not set", variable)) | ||
} | ||
return v | ||
} | ||
|
||
func getEnvIntWithDefault(variable string, defaultValue int) int { | ||
v, ok := os.LookupEnv(variable) | ||
if !ok { | ||
return defaultValue | ||
} | ||
|
||
intValue, err := strconv.Atoi(v) | ||
if err != nil { | ||
return defaultValue | ||
} | ||
|
||
return intValue | ||
} | ||
|
||
func getEnv() *Env { | ||
return &Env{ | ||
ServerAddress: getEnvOrPanic("PING_PONG_SERVICE_HOST"), | ||
ServerPort: getEnvIntWithDefault("PING_PONG_SERVICE_PORT", 80), | ||
} | ||
} | ||
|
||
func run(ctx context.Context, env *Env) error { | ||
client := &http.Client{ | ||
Transport: &http.Transport{}, | ||
} | ||
|
||
for { | ||
slog.Info("ping...") | ||
if err := ping(client, env.ServerAddress, env.ServerPort); err != nil { | ||
slog.Error("problem reaching server", "error", err) | ||
} | ||
time.Sleep(5 * time.Second) | ||
} | ||
} | ||
|
||
func ping(client *http.Client, serverAddr string, serverPort int) error { | ||
r, err := client.Get((&url.URL{ | ||
Scheme: "http", | ||
Host: fmt.Sprintf("%s:%d", serverAddr, serverPort), | ||
}).String()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
defer r.Body.Close() | ||
|
||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
return err | ||
} | ||
slog.Info(string(body)) | ||
return nil | ||
} |
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,61 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
func main() { | ||
if err := run(context.Background(), getEnv()); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
type Env struct { | ||
Port string | ||
} | ||
|
||
func getEnvWithDefault(variable string, defaultValue string) string { | ||
v, ok := os.LookupEnv(variable) | ||
if !ok { | ||
return defaultValue | ||
} | ||
return v | ||
} | ||
|
||
func getEnv() *Env { | ||
return &Env{ | ||
Port: getEnvWithDefault("PORT", ":9090"), | ||
} | ||
} | ||
|
||
func run(ctx context.Context, env *Env) error { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", handler) | ||
|
||
server := &http.Server{ | ||
Addr: env.Port, | ||
Handler: mux, | ||
ReadHeaderTimeout: time.Second * 10, | ||
} | ||
|
||
if err := server.ListenAndServe(); err != nil { | ||
return fmt.Errorf("failed to serve: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func handler(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "text/plain") | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write([]byte("...pong")) | ||
if err != nil { | ||
log.Printf("Error writing response: %v", err) | ||
return | ||
} | ||
} |
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