diff --git a/README.md b/README.md index cbc7c2d..6397ac2 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,10 @@ When utilizing the `nitric new` command to initiate a new project, the available | [url-shortener](./v1/url-shortener/) | URL Shortener with Pub/Sub | APIs, Topics, Key Value Stores | | [websocket-app](./v1/websocket-app/) | A basic websockets example | APIs, WebSockets, Key Value Stores | +### C# + +| [dotnet-starter](./v1/dotnet-starter/) | .NET REST API Starter | APIs | + ### Websites | Name | Description | Features | diff --git a/cli-templates.yaml b/cli-templates.yaml index 09d82f5..e1aa24a 100644 --- a/cli-templates.yaml +++ b/cli-templates.yaml @@ -29,3 +29,7 @@ templates: label: Go - Starter desc: A simple REST API path: ./v1/go-starter + - name: csharp-starter + label: C# - Starter + desc: A simple REST API + path: ./v1/dotnet-starter diff --git a/v1/dotnet-starter/.gitignore b/v1/dotnet-starter/.gitignore new file mode 100644 index 0000000..bad94e2 --- /dev/null +++ b/v1/dotnet-starter/.gitignore @@ -0,0 +1,6 @@ +.nitric/ +bin +obj +Nuget.config +.vs +nitric-spec.json \ No newline at end of file diff --git a/v1/dotnet-starter/README.md b/v1/dotnet-starter/README.md new file mode 100644 index 0000000..3153cf2 --- /dev/null +++ b/v1/dotnet-starter/README.md @@ -0,0 +1,36 @@ +

+ +> The Nitric C# SDK is currently experimental and subject to change. We don't recommend using it for production systems, feedback is welcome. + +## About Nitric + +This is a [Nitric](https://nitric.io) C# project, but Nitric is a framework for rapid development of cloud-native and serverless applications in many languages. + +Using Nitric you define your apps in terms of the resources they need, then write the code for serverless function based APIs, event subscribers and scheduled jobs. + +Apps built with Nitric can be deployed to AWS, Azure or Google Cloud all from the same code base so you can focus on your products, not your cloud provider. + +Nitric makes it easy to: + +- Create smart [serverless functions and APIs](https://nitric.io/docs/apis) +- Build reliable distributed apps that use [events](https://nitric.io/docs/messaging/topics) and/or [queues](https://nitric.io/docs/messaging/queues) +- Securely store, retrieve and rotate [secrets](https://nitric.io/docs/secrets) +- Read and write files from [buckets](https://nitric.io/docs/storage) + +## Learning Nitric + +Nitric provides detailed and intuitive [documentation](https://nitric.io/docs) and [guides](https://nitric.io/docs/getting-started) to help you get started quickly. + +If you'd rather chat with the maintainers or community, come and join our [Discord](https://discord.gg/Webemece5C) server, [GitHub Discussions](https://github.com/nitrictech/nitric/discussions) or find us on [Twitter](https://twitter.com/nitric_io). + +## Running this project + +To run this project you'll need the [Nitric CLI](https://nitric.io/docs/installation) installed, then you can use the CLI commands to run, build or deploy the project. + +Start the Nitric services. + +```bash +nitric start +``` + +You'll see your services connect in your `nitric start` terminal. diff --git a/v1/dotnet-starter/csharp-starter.sln b/v1/dotnet-starter/csharp-starter.sln new file mode 100644 index 0000000..788bfa1 --- /dev/null +++ b/v1/dotnet-starter/csharp-starter.sln @@ -0,0 +1,36 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 25.0.1704.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "services", "services", "{D4817AD1-C4EE-4EE2-A381-CCEDC4B2BEB7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hello", "services\Hello\Hello.csproj", "{524526B1-D05C-4E73-BB52-1EC675E67072}" +EndProject +Global + GlobalSection(NestedProjects) = preSolution + {524526B1-D05C-4E73-BB52-1EC675E67072} = {D4817AD1-C4EE-4EE2-A381-CCEDC4B2BEB7} + EndGlobalSection + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|Any CPU.Build.0 = Debug|Any CPU + {524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|x64.ActiveCfg = Debug|Any CPU + {524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|x64.Build.0 = Debug|Any CPU + {524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|x86.ActiveCfg = Debug|Any CPU + {524526B1-D05C-4E73-BB52-1EC675E67072}.Debug|x86.Build.0 = Debug|Any CPU + {524526B1-D05C-4E73-BB52-1EC675E67072}.Release|Any CPU.ActiveCfg = Release|Any CPU + {524526B1-D05C-4E73-BB52-1EC675E67072}.Release|Any CPU.Build.0 = Release|Any CPU + {524526B1-D05C-4E73-BB52-1EC675E67072}.Release|x64.ActiveCfg = Release|Any CPU + {524526B1-D05C-4E73-BB52-1EC675E67072}.Release|x64.Build.0 = Release|Any CPU + {524526B1-D05C-4E73-BB52-1EC675E67072}.Release|x86.ActiveCfg = Release|Any CPU + {524526B1-D05C-4E73-BB52-1EC675E67072}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/v1/dotnet-starter/dotnet.dockerfile b/v1/dotnet-starter/dotnet.dockerfile new file mode 100644 index 0000000..f89c578 --- /dev/null +++ b/v1/dotnet-starter/dotnet.dockerfile @@ -0,0 +1,35 @@ +ARG HANDLER + +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build + +# Set working directory +WORKDIR /app + +# Copy the entire project directory +COPY . ./ + +# Restore dependencies +RUN dotnet restore + +# Publish as a single file executable +RUN dotnet publish -c Release --self-contained -p:PublishSingleFile=true -p:EnableCompressionInSingleFile=true + +# Use a runtime image +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime + +ARG HANDLER + +# Extract project name from Handler in the form "./services/Hello/Hello.csproj" +ENV PROJECT_NAME=${HANDLER%/*} +ENV PROJECT_NAME=${PROJECT_NAME##*/} + +# Set working directory +WORKDIR /app + +# Copy the built executable from the builder stage +COPY --from=build /app/out/linux-x64/publish/ ./ + +RUN ls + +# Set entrypoint +ENTRYPOINT "./${PROJECT_NAME}" \ No newline at end of file diff --git a/v1/dotnet-starter/dotnet.dockerfile.dockerignore b/v1/dotnet-starter/dotnet.dockerfile.dockerignore new file mode 100644 index 0000000..cb26513 --- /dev/null +++ b/v1/dotnet-starter/dotnet.dockerfile.dockerignore @@ -0,0 +1,9 @@ +.nitric +.git +.idea +.vscode +.github +*.dockerfile +*.dockerignore +.vs +nitric-spec.json \ No newline at end of file diff --git a/v1/dotnet-starter/nitric.yaml b/v1/dotnet-starter/nitric.yaml new file mode 100644 index 0000000..0b6a274 --- /dev/null +++ b/v1/dotnet-starter/nitric.yaml @@ -0,0 +1,10 @@ +name: csharp-starter +services: + - match: services/**/*.csproj + start: dotnet watch --non-interactive --project $SERVICE_PATH + runtime: dotnet + +runtimes: + dotnet: + dockerfile: ./dotnet.dockerfile + args: {} diff --git a/v1/dotnet-starter/services/Hello/Hello.cs b/v1/dotnet-starter/services/Hello/Hello.cs new file mode 100644 index 0000000..1a04e82 --- /dev/null +++ b/v1/dotnet-starter/services/Hello/Hello.cs @@ -0,0 +1,14 @@ +using Application = Nitric.Sdk.Nitric; + +var api = Application.Api("main"); + +api.Get("/hello/:name", async context => +{ + var name = context.Req.PathParams["name"]; + + context.Res.Text($"Hello {name}!"); + + return context; +}); + +Application.Run(); \ No newline at end of file diff --git a/v1/dotnet-starter/services/Hello/Hello.csproj b/v1/dotnet-starter/services/Hello/Hello.csproj new file mode 100644 index 0000000..5ff86cb --- /dev/null +++ b/v1/dotnet-starter/services/Hello/Hello.csproj @@ -0,0 +1,18 @@ + + + + Exe + net8.0 + Dotnet Starter + + + + ../../out + false + false + + + + + + diff --git a/v1/dotnet-starter/services/Hello/Profile.cs b/v1/dotnet-starter/services/Hello/Profile.cs new file mode 100644 index 0000000..1b70e5e --- /dev/null +++ b/v1/dotnet-starter/services/Hello/Profile.cs @@ -0,0 +1,10 @@ +namespace Common; + +public class Profile +{ + public string Name { get; private set; } + + public Profile(string name) { + this.Name = name; + } +}