-
Notifications
You must be signed in to change notification settings - Fork 19
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
1c43a18
commit 43c0170
Showing
9 changed files
with
469 additions
and
165 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
This file was deleted.
Oops, something went wrong.
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,24 @@ | ||
# Echo | ||
|
||
This directory contains a Service Weaver web application that echos the request | ||
string back to the caller. | ||
|
||
This application exists primarily to test the correctness of the Kube deployer. | ||
|
||
## How to run? | ||
|
||
To run this application inside a Kubernetes cluster, run `weaver kube deploy`. | ||
|
||
```console | ||
$ weaver kube deploy weaver.toml # Generate YAML file | ||
$ kubectl apply -f <filename> # Deploy YAML file | ||
``` | ||
|
||
## How to interact with the application? | ||
|
||
Get the IP address of the listener service on your kubernetes cluster. Then run: | ||
|
||
```console | ||
$ curl http://<IPAddress>?s=foo | ||
foo | ||
``` |
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,45 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ServiceWeaver/weaver" | ||
"github.com/ServiceWeaver/weaver/metrics" | ||
) | ||
|
||
// stringLength is a histogram that tracks the length of strings passed to the | ||
// Echo method. | ||
var stringLength = metrics.NewHistogram( | ||
"echo_string_length", | ||
"The length of strings passed to the Echo method", | ||
metrics.NonNegativeBuckets, | ||
) | ||
|
||
// Echoer component. | ||
type Echoer interface { | ||
Echo(context.Context, string) (string, error) | ||
} | ||
|
||
// Implementation of the Echoer component. | ||
type echoer struct { | ||
weaver.Implements[Echoer] | ||
} | ||
|
||
func (e echoer) Echo(ctx context.Context, s string) (string, error) { | ||
stringLength.Put(float64(len(s))) // Update the stringLength metric. | ||
return s, 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,59 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/ServiceWeaver/weaver" | ||
) | ||
|
||
//go:generate weaver generate | ||
|
||
func main() { | ||
// Initialize the Service Weaver application. | ||
flag.Parse() | ||
if err := weaver.Run(context.Background(), serve); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
type server struct { | ||
weaver.Implements[weaver.Main] | ||
echoer weaver.Ref[Echoer] | ||
lis weaver.Listener `weaver:"echo"` | ||
} | ||
|
||
func serve(ctx context.Context, s *server) error { | ||
// Setup the HTTP handler. | ||
var mux http.ServeMux | ||
mux.Handle("/", weaver.InstrumentHandlerFunc("echo", | ||
func(w http.ResponseWriter, r *http.Request) { | ||
input := r.URL.Query().Get("s") | ||
output, err := s.echoer.Get().Echo(r.Context(), input) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.Write([]byte(output)) | ||
})) | ||
mux.HandleFunc(weaver.HealthzURL, weaver.HealthzHandler) | ||
|
||
fmt.Printf("echo listener available on %v\n", s.lis) | ||
return http.Serve(s.lis, &mux) | ||
} |
Oops, something went wrong.