diff --git a/src/Startup.cs b/src/Startup.cs new file mode 100644 index 0000000000000000000000000000000000000000..546c98f6d2611a75598d5c00a490f64ff5c63273 --- /dev/null +++ b/src/Startup.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using cartservice.cartstore; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using OpenTelemetry.Trace; +using OpenTelemetry.Resources; +using Grpc.Core; + +namespace cartservice +{ + public class Startup + { + public IConfiguration Configuration { get; } + + public Startup(IConfiguration configuration) + { + this.Configuration = configuration; + } + + + // This method gets called by the runtime. Use this method to add services to the container. + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 + public void ConfigureServices(IServiceCollection services) + { + // services.AddSingleton(); + services.AddGrpc(); + services.AddSingleton(); + + services.AddOpenTelemetryTracerProvider(builder => + { + builder.AddOtlpExporter((otlpOptions) => + { + otlpOptions.Endpoint = "http://" + this.Configuration.GetValue("JAEGER_HOST") + ":" + this.Configuration.GetValue("JAEGER_PORT") + "/api/traces"; + otlpOptions.Headers.Add("exporter", "jaeger"); + otlpOptions.Headers.Add("ip", this.Configuration.GetValue("POD_IP")); + otlpOptions.Headers.Add("podName", this.Configuration.GetValue("POD_NAME")); + otlpOptions.Headers.Add("nodeName", this.Configuration.GetValue("NODE_NAME")); + }); + builder.SetResource(Resources.CreateServiceResource(this.Configuration.GetValue("SERVICE_NAME"))); + builder.AddAspNetCoreInstrumentation(); + builder.AddGrpcClientInstrumentation(); + builder.AddHttpClientInstrumentation(); + }); + + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapGrpcService(); + endpoints.MapGrpcService(); + + // endpoints.MapGet("/", async context => + // { + // await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); + // }); + }); + } + } +} diff --git a/src/adservice/gradle-6.3/bin/gradle b/src/adservice/gradle-6.3/bin/gradle old mode 100755 new mode 100644 index 564bba482c1fc3c2b017b101a310757074454fef..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 --- a/src/adservice/gradle-6.3/bin/gradle +++ b/src/adservice/gradle-6.3/bin/gradle @@ -1,183 +0,0 @@ -#!/usr/bin/env sh - -# -# Copyright 2015 the original author or authors. -# -# 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 -# -# https://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. -# - -############################################################################## -## -## Gradle start up script for UN*X -## -############################################################################## - -# Attempt to set APP_HOME -# Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi -done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/.." >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null - -APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' - -# Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" - -warn () { - echo "$*" -} - -die () { - echo - echo "$*" - echo - exit 1 -} - -# OS specific support (must be 'true' or 'false'). -cygwin=false -msys=false -darwin=false -nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; -esac - -CLASSPATH=$APP_HOME/lib/gradle-launcher-6.3.jar - -# Determine the Java command to use to start the JVM. -if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" - else - JAVACMD="$JAVA_HOME/bin/java" - fi - if [ ! -x "$JAVACMD" ] ; then - die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." - fi -else - JAVACMD="java" - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." -fi - -# Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi -fi - -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi - -# For Cygwin or MSYS, switch paths to Windows format before running java -if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi - # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" - fi - i=`expr $i + 1` - done - case $i in - 0) set -- ;; - 1) set -- "$args0" ;; - 2) set -- "$args0" "$args1" ;; - 3) set -- "$args0" "$args1" "$args2" ;; - 4) set -- "$args0" "$args1" "$args2" "$args3" ;; - 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac -fi - -# Escape application args -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=`save "$@"` - -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.launcher.GradleMain "$APP_ARGS" - -exec "$JAVACMD" "$@" diff --git a/src/adservice/gradle-6.3/bin/gradle.bat b/src/adservice/gradle-6.3/bin/gradle.bat old mode 100755 new mode 100644 index 0be907fa9f0919d307e37d35325dbb54a06011ad..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 --- a/src/adservice/gradle-6.3/bin/gradle.bat +++ b/src/adservice/gradle-6.3/bin/gradle.bat @@ -1,103 +0,0 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME%.. - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\lib\gradle-launcher-6.3.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.launcher.GradleMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega diff --git a/src/adservice/sql.txt b/src/adservice/sql.txt new file mode 100644 index 0000000000000000000000000000000000000000..46234778697deb062411ff395b85062313534915 --- /dev/null +++ b/src/adservice/sql.txt @@ -0,0 +1,49 @@ +insert into aditems +(`item_name`, `redirect_url`, `text`) +values ( + 'lens', + '/product/66VCHSJNUP', + 'Lens for sale. 50% off.' +); +insert into aditems +(`item_name`, `redirect_url`, `text`) +values ( + 'camera', + '/product/2ZYFJ3GM2N', + 'Camera for sale. 20% off.' +); +insert into aditems +(`item_name`, `redirect_url`, `text`) +values ( + 'recordPlayer', + '/product/0PUK6V6EV0', + 'Record player for sale. 30% off.' +); +insert into aditems +(`item_name`, `redirect_url`, `text`) +values ( + 'bike', + '/product/9SIQT8TOJO', + 'City bike for sale. 10% off.' +); +insert into aditems +(`item_name`, `redirect_url`, `text`) +values ( + 'baristaKit', + '/product/1YMWWN1N4O', + 'Barista Kit for sale. Buy one, get second kit for free' +); +insert into aditems +(`item_name`, `redirect_url`, `text`) +values ( + 'airPlant', + '/product/6E92ZMYYFZ', + 'Air Plant for sale. Buy two, get third one for free' +); +insert into aditems +(`item_name`, `redirect_url`, `text`) +values ( + 'terrarium', + '/product/L9ECAV7KIM', + 'Terrarium for sale. Buy one, get second one for free' +); \ No newline at end of file diff --git a/src/cartservice.csproj b/src/cartservice.csproj new file mode 100644 index 0000000000000000000000000000000000000000..19afb94108fe0f5e9d3381566a60bdd22a1e64fc --- /dev/null +++ b/src/cartservice.csproj @@ -0,0 +1,39 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + + + + + + + Always + + + diff --git a/src/cartservice/Dockerfile copy b/src/cartservice/Dockerfile copy new file mode 100644 index 0000000000000000000000000000000000000000..06d8ca81f89442d87ff37b55717116a5b5f44ce7 --- /dev/null +++ b/src/cartservice/Dockerfile copy @@ -0,0 +1,36 @@ +FROM mcr.microsoft.com/dotnet/sdk:8.0.300 as builder +WORKDIR /app +COPY . . + +RUN dotnet nuget Disable source "nuget.org" && \ +dotnet nuget add source "http://nuget.cdn.azure.cn/v3/index.json" -n "azure" + +RUN dotnet restore && \ + dotnet build && \ + dotnet publish -c release -r linux-musl-x64 -o /cartservice + +# cartservice +FROM alpine:3.8 + +# RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \ +# wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64 && \ +# chmod +x /bin/grpc_health_probe +COPY grpc_health_probe-linux-amd64 /bin/grpc_health_probe + +RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \ + chmod +x /bin/grpc_health_probe + +# Dependencies for runtime +# busybox-extras => telnet +RUN apk add --no-cache \ + busybox-extras \ + libc6-compat \ + libunwind \ + libuuid \ + libgcc \ + libstdc++ \ + libintl \ + icu +WORKDIR /app +COPY --from=builder /cartservice . +ENTRYPOINT ["./cartservice", "start"] diff --git a/src/cartservice/Program.bak b/src/cartservice/Program.bak new file mode 100644 index 0000000000000000000000000000000000000000..2357af1b3919eecd2a02811ca501de85e2177249 --- /dev/null +++ b/src/cartservice/Program.bak @@ -0,0 +1,102 @@ +// Copyright 2018 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. + +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using cartservice.cartstore; +using cartservice.interfaces; +using CommandLine; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; +using Microsoft.AspNetCore.Server.Kestrel.Core; +using System.Net; +using System.Security.Authentication; +using Microsoft.AspNetCore.Connections.Features; +using Grpc.Core; +using Microsoft.Extensions.Configuration; +using Microsoft.Owin +using Microsoft.Owin.Host.SystemWeb + +namespace cartservice +{ + class Program + { + const string CART_SERVICE_ADDRESS = "LISTEN_ADDR"; + const string CART_SERVICE_PORT = "PORT"; + + static void Main(string[] args) + { + if (args.Length == 0) + { + Console.WriteLine("Invalid number of arguments supplied"); + Environment.Exit(-1); + } + + switch (args[0]) + { + case "start": + Console.WriteLine($"Started as process with id {System.Diagnostics.Process.GetCurrentProcess().Id}"); + + // Set hostname/ip address + Console.WriteLine($"Reading host address from {CART_SERVICE_ADDRESS} environment variable"); + string hostname = Environment.GetEnvironmentVariable(CART_SERVICE_ADDRESS); + if (string.IsNullOrEmpty(hostname)) + { + Console.WriteLine($"Environment variable {CART_SERVICE_ADDRESS} was not set. Setting the host to 0.0.0.0"); + hostname = "0.0.0.0"; + } + + // Set the port + Console.WriteLine($"Reading cart service port from {CART_SERVICE_PORT} environment variable"); + string portStr = Environment.GetEnvironmentVariable(CART_SERVICE_PORT); + int port; + if (string.IsNullOrEmpty(portStr)) + { + Console.WriteLine($"{CART_SERVICE_PORT} environment variable was not set. Setting the port to 8080"); + // TODO:DEBUG + // port = 8080; + port = 7070; + } + else + { + port = int.Parse(portStr); + } + Console.WriteLine($"Trying to start a grpc server at {hostname}:{port}"); + Console.WriteLine("Insecure mode!"); + string[] warpperArgs = new string[args.Length + 1]; + args.CopyTo(warpperArgs, 0); + Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + webBuilder.ConfigureKestrel(serverOptions => + { + serverOptions.Listen(IPAddress.Parse(hostname), port, listenOptions => + { + listenOptions.Protocols = HttpProtocols.Http2; + }); + }); + + // webBuilder.UseUrls($"http://{hostname}:{port}"); + }).Build().Run(); + break; + + default: + Console.WriteLine("Invalid command"); + break; + } + } + } +} diff --git a/src/cartservice/Startup.bak b/src/cartservice/Startup.bak new file mode 100644 index 0000000000000000000000000000000000000000..f3c103ef6715b084119fb8adb422b7eca9ba9c4d --- /dev/null +++ b/src/cartservice/Startup.bak @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using cartservice.cartstore; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using OpenTelemetry.Trace; +using OpenTelemetry.Resources; +using OpenTelemetry.Exporter; +using OpenTelemetry.Exporter.Console; +using OpenTelemetry.Instrumentation.AspNetCore; +using OpenTelemetry.Instrumentation.Http; +using Grpc.Core; + +namespace cartservice +{ + public class Startup + { + public IConfiguration Configuration { get; } + + public Startup(IConfiguration configuration) + { + this.Configuration = configuration; + Console.WriteLine("add configuration here"); + } + + + // This method gets called by the runtime. Use this method to add services to the container. + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 + public void ConfigureServices(IServiceCollection services) + { + // services.AddSingleton(); + Console.WriteLine("add builder here" + this.Configuration.GetValue("JAEGER_HOST")+ ":" + this.Configuration.GetValue("JAEGER_PORT")); + services.AddGrpc(); + services.AddSingleton(); + services.AddOpenTelemetryTracerProvider(builder => + { + builder.AddConsoleExporter(); + builder.AddOtlpExporter((otlpOptions) => + { + otlpOptions.Endpoint = "http://" + this.Configuration.GetValue("JAEGER_HOST") + ":" + this.Configuration.GetValue("JAEGER_PORT") + "/api/traces"; + otlpOptions.Headers.Add("exporter", "jaeger"); + otlpOptions.Headers.Add("ip", this.Configuration.GetValue("POD_IP")); + otlpOptions.Headers.Add("podName", this.Configuration.GetValue("POD_NAME")); + otlpOptions.Headers.Add("nodeName", this.Configuration.GetValue("NODE_NAME")); + }); + builder.SetResource(Resources.CreateServiceResource(this.Configuration.GetValue("SERVICE_NAME"))); + builder.AddAspNetCoreInstrumentation(); + builder.AddGrpcClientInstrumentation(); + builder.AddHttpClientInstrumentation(); + }); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + Console.WriteLine("add configure here"); + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapGrpcService(); + endpoints.MapGrpcService(); + }); + } + } +} diff --git a/src/cartservice/cartservice.bak b/src/cartservice/cartservice.bak new file mode 100644 index 0000000000000000000000000000000000000000..19afb94108fe0f5e9d3381566a60bdd22a1e64fc --- /dev/null +++ b/src/cartservice/cartservice.bak @@ -0,0 +1,39 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + + + + + + + Always + + +