forked from ServiceStackApps/Todos
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStartup.cs
More file actions
140 lines (119 loc) · 4.13 KB
/
Startup.cs
File metadata and controls
140 lines (119 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Linq;
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Funq;
using ServiceStack;
using ServiceStack.Configuration;
using ServiceStack.Host.Handlers;
using ServiceStack.Redis;
using ServiceStack.VirtualPath;
using ServiceStack.Api.OpenApi;
using System.Text;
using ServiceStack.Auth;
namespace Todos
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
public class Startup
{
IConfiguration Configuration { get; set; }
public Startup(IConfiguration configuration) => Configuration = configuration;
// 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.UseServiceStack(new AppHost {
AppSettings = new NetCoreAppSettings(Configuration)
});
}
}
// Create your ServiceStack Web Service with a singleton AppHost
public class AppHost : AppHostBase
{
// Initializes your AppHost Instance, with the Service Name and assembly containing the Services
public AppHost() : base("Backbone.js TODO", typeof(TodoService).Assembly) {}
// Configure your AppHost with the necessary configuration and dependencies your App needs
public override void Configure(Container container)
{
//Register Redis Client Manager singleton in ServiceStack's built-in Func IOC
container.Register<IRedisClientsManager>(c =>
new RedisManagerPool(AppSettings.Get("REDIS_HOST", defaultValue:"localhost")));
Plugins.Add(new OpenApiFeature());
SetConfig(new HostConfig
{
AdminAuthSecret = "secret",
DebugMode = true,
});
Plugins.Add(new AuthFeature(() => new AuthUserSession(), new [] {
new CredentialsAuthProvider()
}));
Plugins.AddIfDebug(new RequestLogsFeature {
EnableResponseTracking = true,
});
Plugins.AddIfDebug(new ProfilingFeature {
IncludeStackTrace = true,
});
}
}
// Define your ServiceStack web service request (i.e. Request DTO).
[Route("/todos")]
[Route("/todos/{Id}")]
public class Todo
{
public long Id { get; set; }
public string Content { get; set; }
public int Order { get; set; }
public bool Done { get; set; }
}
// Create your ServiceStack rest-ful web service implementation.
public class TodoService : Service
{
public object Get(Todo todo)
{
//Return a single Todo if the id is provided.
if (todo.Id != default(long))
return Redis.As<Todo>().GetById(todo.Id);
//Return all Todos items.
return Redis.As<Todo>().GetAll();
}
// Handles creating and updating the Todo items.
public Todo Post(Todo todo)
{
var redis = Redis.As<Todo>();
//Get next id for new todo
if (todo.Id == default(long))
todo.Id = redis.GetNextSequence();
redis.Store(todo);
return todo;
}
// Handles creating and updating the Todo items.
public Todo Put(Todo todo)
{
return Post(todo);
}
// Handles Deleting the Todo item
public void Delete(Todo todo)
{
Redis.As<Todo>().DeleteById(todo.Id);
}
}
}