Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the Online Boutique application into its own repository. #1

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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.

name: Build and Test

on:
push:
branches:
- 'main'
pull_request:

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.20'
cache: true

- name: build
run: go build ./...
- name: vet
run: go vet ./...
- name: test
run: go test ./...
- name: testrace
run: go test -race ./...

49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Online Boutique

This directory contains the port of the Google Cloud's [`Online
Boutique`][boutique] demo application to Service Weaver.

```mermaid
%%{init: {"flowchart": {"defaultRenderer": "elk"}} }%%
graph TD
%% Nodes.
github.com/ServiceWeaver/weaver/Main(weaver.Main)
github.com/ServiceWeaver/weaver/examples/onlineboutique/adservice/T(adservice.T)
github.com/ServiceWeaver/weaver/examples/onlineboutique/cartservice/T(cartservice.T)
github.com/ServiceWeaver/weaver/examples/onlineboutique/cartservice/cartCache(cartservice.cartCache)
github.com/ServiceWeaver/weaver/examples/onlineboutique/checkoutservice/T(checkoutservice.T)
github.com/ServiceWeaver/weaver/examples/onlineboutique/currencyservice/T(currencyservice.T)
github.com/ServiceWeaver/weaver/examples/onlineboutique/emailservice/T(emailservice.T)
github.com/ServiceWeaver/weaver/examples/onlineboutique/paymentservice/T(paymentservice.T)
github.com/ServiceWeaver/weaver/examples/onlineboutique/productcatalogservice/T(productcatalogservice.T)
github.com/ServiceWeaver/weaver/examples/onlineboutique/recommendationservice/T(recommendationservice.T)
github.com/ServiceWeaver/weaver/examples/onlineboutique/shippingservice/T(shippingservice.T)

%% Edges.
github.com/ServiceWeaver/weaver/Main --> github.com/ServiceWeaver/weaver/examples/onlineboutique/adservice/T
github.com/ServiceWeaver/weaver/Main --> github.com/ServiceWeaver/weaver/examples/onlineboutique/cartservice/T
github.com/ServiceWeaver/weaver/Main --> github.com/ServiceWeaver/weaver/examples/onlineboutique/checkoutservice/T
github.com/ServiceWeaver/weaver/Main --> github.com/ServiceWeaver/weaver/examples/onlineboutique/currencyservice/T
github.com/ServiceWeaver/weaver/Main --> github.com/ServiceWeaver/weaver/examples/onlineboutique/productcatalogservice/T
github.com/ServiceWeaver/weaver/Main --> github.com/ServiceWeaver/weaver/examples/onlineboutique/recommendationservice/T
github.com/ServiceWeaver/weaver/Main --> github.com/ServiceWeaver/weaver/examples/onlineboutique/shippingservice/T
github.com/ServiceWeaver/weaver/examples/onlineboutique/cartservice/T --> github.com/ServiceWeaver/weaver/examples/onlineboutique/cartservice/cartCache
github.com/ServiceWeaver/weaver/examples/onlineboutique/checkoutservice/T --> github.com/ServiceWeaver/weaver/examples/onlineboutique/cartservice/T
github.com/ServiceWeaver/weaver/examples/onlineboutique/checkoutservice/T --> github.com/ServiceWeaver/weaver/examples/onlineboutique/currencyservice/T
github.com/ServiceWeaver/weaver/examples/onlineboutique/checkoutservice/T --> github.com/ServiceWeaver/weaver/examples/onlineboutique/emailservice/T
github.com/ServiceWeaver/weaver/examples/onlineboutique/checkoutservice/T --> github.com/ServiceWeaver/weaver/examples/onlineboutique/paymentservice/T
github.com/ServiceWeaver/weaver/examples/onlineboutique/checkoutservice/T --> github.com/ServiceWeaver/weaver/examples/onlineboutique/productcatalogservice/T
github.com/ServiceWeaver/weaver/examples/onlineboutique/checkoutservice/T --> github.com/ServiceWeaver/weaver/examples/onlineboutique/shippingservice/T
github.com/ServiceWeaver/weaver/examples/onlineboutique/recommendationservice/T --> github.com/ServiceWeaver/weaver/examples/onlineboutique/productcatalogservice/T
```

Here are the changes made to the original application:

* All of the services that weren't written in `Go` have been ported to `Go`.
* All of the networking calls have been replaced with the corresponding
Service Weaver calls.
* All of the logging/tracing/monitoring calls have been replaced with the
corresponding Service Weaver calls.
* The code is organized as a single Go module.

[boutique]: https://github.com/GoogleCloudPlatform/microservices-demo
120 changes: 120 additions & 0 deletions adservice/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2022 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 adservice

import (
"context"
"math/rand"
"strings"

"golang.org/x/exp/maps"

"github.com/ServiceWeaver/weaver"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

const (
maxAdsToServe = 2
)

// Ad represents an advertisement.
type Ad struct {
weaver.AutoMarshal
RedirectURL string // URL to redirect to when an ad is clicked.
Text string // Short advertisement text to display.
}

type T interface {
GetAds(ctx context.Context, keywords []string) ([]Ad, error)
}

type impl struct {
weaver.Implements[T]
ads map[string]Ad
}

func (s *impl) Init(context.Context) error {
s.Logger().Info("Ad Service started")
s.ads = createAdsMap()
return nil
}

// GetAds returns a list of ads that best match the given context keywords.
func (s *impl) GetAds(ctx context.Context, keywords []string) ([]Ad, error) {
s.Logger().Info("received ad request", "keywords", keywords)
span := trace.SpanFromContext(ctx)
var allAds []Ad
if len(keywords) > 0 {
span.AddEvent("Constructing Ads using context", trace.WithAttributes(
attribute.String("Context Keys", strings.Join(keywords, ",")),
attribute.Int("Context Keys length", len(keywords)),
))
for _, kw := range keywords {
allAds = append(allAds, s.getAdsByCategory(kw)...)
}
if allAds == nil {
// Serve random ads.
span.AddEvent("No Ads found based on context. Constructing random Ads.")
allAds = s.getRandomAds()
}
} else {
span.AddEvent("No Context provided. Constructing random Ads.")
allAds = s.getRandomAds()
}
return allAds, nil
}

func (s *impl) getAdsByCategory(category string) []Ad {
return []Ad{s.ads[category]}
}

func (s *impl) getRandomAds() []Ad {
ads := make([]Ad, maxAdsToServe)
vals := maps.Values(s.ads)
for i := 0; i < maxAdsToServe; i++ {
ads[i] = vals[rand.Intn(len(vals))]
}
return ads
}

func createAdsMap() map[string]Ad {
return map[string]Ad{
"hair": {
RedirectURL: "/product/2ZYFJ3GM2N",
Text: "Hairdryer for sale. 50% off.",
},
"clothing": {
RedirectURL: "/product/66VCHSJNUP",
Text: "Tank top for sale. 20% off.",
},
"accessories": {
RedirectURL: "/product/1YMWWN1N4O",
Text: "Watch for sale. Buy one, get second kit for free",
},
"footwear": {
RedirectURL: "/product/L9ECAV7KIM",
Text: "Loafers for sale. Buy one, get second one for free",
},
"decor": {
RedirectURL: "/product/0PUK6V6EV0",
Text: "Candle holder for sale. 30% off.",
},
"kitchen": {
RedirectURL: "/product/9SIQT8TOJO",
Text: "Bamboo glass jar for sale. 10% off.",
},
}
}
Loading