-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAppHost.cs
136 lines (123 loc) · 5.32 KB
/
AppHost.cs
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
using System.Collections.Generic;
using Amazon;
using Amazon.S3;
using Funq;
using ServiceStack;
using ServiceStack.Auth;
using ServiceStack.Authentication.OAuth2;
using ServiceStack.Authentication.OpenId;
using ServiceStack.Aws.DynamoDb;
using ServiceStack.Aws.S3;
using ServiceStack.Aws.Sqs;
using ServiceStack.Caching;
using ServiceStack.Configuration;
using ServiceStack.IO;
using ServiceStack.Logging;
using ServiceStack.Messaging;
using ServiceStack.Razor;
using ServiceStack.Support;
using ServiceStack.Text;
using ServiceStack.Validation;
namespace AwsApps
{
public class AppHost : AppHostBase
{
public AppHost() : base("AWS Examples", typeof(AppHost).Assembly)
{
#if DEBUG
LogManager.LogFactory = new StringBuilderLogFactory(); //View logs at /logs
#else
//Deployed RELEASE build uses Config settings in DynamoDb
AppSettings = new MultiAppSettings(
new DynamoDbAppSettings(new PocoDynamo(AwsConfig.CreateAmazonDynamoDb()), initSchema:true),
new AppSettings());
#endif
}
public override void Configure(Container container)
{
JsConfig.EmitCamelCaseNames = true;
Plugins.Add(new RazorFormat());
//Comment out 2 lines below to change to use local FileSystem instead of S3
var s3Client = new AmazonS3Client(AwsConfig.AwsAccessKey, AwsConfig.AwsSecretKey, RegionEndpoint.USEast1);
VirtualFiles = new S3VirtualPathProvider(s3Client, AwsConfig.S3BucketName, this);
container.Register<IPocoDynamo>(c => new PocoDynamo(AwsConfig.CreateAmazonDynamoDb()));
var db = container.Resolve<IPocoDynamo>();
db.RegisterTable<Todos.Todo>();
db.RegisterTable<EmailContacts.Email>();
db.RegisterTable<EmailContacts.Contact>();
db.InitSchema();
//AWS Auth
container.Register<ICacheClient>(new DynamoDbCacheClient(db, initSchema:true));
container.Register<IAuthRepository>(new DynamoDbAuthRepository(db, initSchema:true));
Plugins.Add(CreateAuthFeature());
//EmailContacts
ConfigureSqsMqServer(container);
ConfigureEmailer(container);
Plugins.Add(new ValidationFeature());
container.RegisterValidators(typeof(EmailContacts.CreateContact).Assembly);
}
public override List<IVirtualPathProvider> GetVirtualFileSources()
{
var fileSources = base.GetVirtualFileSources();
fileSources.Add(VirtualFiles);
return fileSources;
}
public AuthFeature CreateAuthFeature()
{
return new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[]
{
new CredentialsAuthProvider(), //HTML Form post of UserName/Password credentials
new BasicAuthProvider(), //Sign-in with HTTP Basic Auth
new DigestAuthProvider(AppSettings), //Sign-in with HTTP Digest Auth
new TwitterAuthProvider(AppSettings), //Sign-in with Twitter
new FacebookAuthProvider(AppSettings), //Sign-in with Facebook
new YahooOpenIdOAuthProvider(AppSettings), //Sign-in with Yahoo OpenId
new OpenIdOAuthProvider(AppSettings), //Sign-in with Custom OpenId
new GoogleOAuth2Provider(AppSettings), //Sign-in with Google OAuth2 Provider
new LinkedInOAuth2Provider(AppSettings), //Sign-in with LinkedIn OAuth2 Provider
new GithubAuthProvider(AppSettings), //Sign-in with GitHub OAuth Provider
})
{
HtmlRedirect = "/awsauth/",
IncludeRegistrationService = true,
};
}
private void ConfigureSqsMqServer(Container container)
{
container.Register<IMessageService>(c => new SqsMqServer(
AwsConfig.AwsAccessKey, AwsConfig.AwsSecretKey, RegionEndpoint.USEast1) {
DisableBuffering = true,
});
var mqServer = container.Resolve<IMessageService>();
mqServer.RegisterHandler<EmailContacts.EmailContact>(ExecuteMessage);
mqServer.Start();
}
private void ConfigureEmailer(Container container)
{
//If SmtpConfig exists, use real SMTP Emailer otherwise use simulated DbEmailer
var smtpConfig = AppSettings.Get<EmailContacts.SmtpConfig>("SmtpConfig");
if (smtpConfig != null)
{
container.Register(smtpConfig);
container.RegisterAs<EmailContacts.SmtpEmailer, EmailContacts.IEmailer>().ReusedWithin(ReuseScope.Request);
}
else
{
container.RegisterAs<EmailContacts.DbEmailer, EmailContacts.IEmailer>().ReusedWithin(ReuseScope.Request);
}
}
}
#if DEBUG
[Route("/logs")]
public class GetLogs { } //Useful service to dump Debug logs at /logs
public class LogService : Service
{
[AddHeader(ContentType = MimeTypes.PlainText)]
public object Any(GetLogs request)
{
return ((StringBuilderLogFactory)LogManager.LogFactory).GetLogs();
}
}
#endif
}