Created
June 27, 2013 18:14
-
-
Save robertdean/5878902 to your computer and use it in GitHub Desktop.
service stack ravendb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Web.Mvc; | |
using Raven.Client; | |
using Raven.Client.Embedded; | |
using ServiceStack.Api.Swagger; | |
using ServiceStack.Authentication.OpenId; | |
using ServiceStack.Authentication.RavenDb; | |
using ServiceStack.CacheAccess; | |
using ServiceStack.CacheAccess.Providers; | |
using ServiceStack.Common.Web; | |
using ServiceStack.Configuration; | |
using ServiceStack.FluentValidation; | |
using ServiceStack.Mvc; | |
using ServiceStack.ServiceInterface; | |
using ServiceStack.ServiceInterface.Admin; | |
using ServiceStack.ServiceInterface.Auth; | |
using ServiceStack.ServiceInterface.Cors; | |
using ServiceStack.WebHost.Endpoints; | |
using WebUI.Logic; | |
using WebUI.Models; | |
using WebUI.ServiceInterface; | |
namespace WebUI | |
{ | |
public class AppHost : AppHostBase | |
{ | |
public AppHost(IDocumentStore documentStore) //Tell ServiceStack the name and where to find your web services | |
: base("StarterTemplate ASP.NET Host", typeof(HelloService).Assembly) | |
{ | |
base.Container.Register<IDocumentStore>(documentStore); | |
} | |
public static AppConfig AppConfig; | |
public static IDocumentStore Store; | |
public override void Configure(Funq.Container container) | |
{ | |
//Set JSON web services to return idiomatic JSON camelCase properties | |
ServiceStack.Text.JsConfig.EmitCamelCaseNames = true; | |
//Register Typed Config some services might need to access | |
var appSettings = new AppSettings(); | |
AppConfig = new AppConfig(appSettings); | |
container.Register(AppConfig); | |
//Register all your dependencies: | |
//Register a external dependency-free | |
container.Register<ICacheClient>(new MemoryCacheClient()); | |
//Configure an alt. distributed persistent cache that survives AppDomain restarts. e.g Redis | |
//container.Register<IRedisClientsManager>(c => new PooledRedisClientManager("localhost:6379")); | |
//Enable Authentication an Registration | |
ConfigureAuth(container); | |
//Create your own custom User table | |
//Register application services | |
container.Register(new TodoRepository()); | |
container.Register<ITwitterGateway>(new TwitterGateway()); | |
//Configure Custom User Defined REST Paths for your services | |
ConfigureServiceRoutes(); | |
//Change the default ServiceStack configuration | |
//const Feature disableFeatures = Feature.Jsv | Feature.Soap; | |
SetConfig(new EndpointHostConfig { | |
//EnableFeatures = Feature.All.Remove(disableFeatures), | |
AppendUtf8CharsetOnContentTypes = new HashSet<string> { ContentType.Html }, | |
}); | |
Plugins.Add(new SwaggerFeature()); | |
Plugins.Add(new CorsFeature()); | |
//Set MVC to use the same Funq IOC as ServiceStack | |
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container)); | |
//ServiceStackController.CatchAllController = reqCtx => container.TryResolve<HomeController>(); | |
} | |
private void ConfigureServiceRoutes() | |
{ | |
Routes | |
//Hello World RPC example | |
.Add<Hello>("/hello") | |
.Add<Hello>("/hello/{Name*}") | |
//Simple REST TODO example | |
.Add<Todo>("/todos") | |
.Add<Todo>("/todos/{Id}") | |
//Custom services for this application | |
.Add<Users>("/users/{UserIds}") | |
.Add<UserProfile>("/profile") | |
//Twitter related services | |
.Add<TwitterTimelines>("/twitter/{ScreenName}/timelines") | |
.Add<TwitterTweets>("/twitter/{ScreenName}/tweets") | |
.Add<TwitterFriends>("/twitter/id/{UserId}/friends") | |
.Add<TwitterFriends>("/twitter/{ScreenName}/friends") | |
.Add<TwitterFollowers>("/twitter/id/{UserId}/followers") | |
.Add<TwitterFollowers>("/twitter/{ScreenName}/followers") | |
.Add<TwitterDirectMessages>("/twitter/directmessages") | |
.Add<TwitterUsers>("/twitter/ids/{UserIds}") //userIds serparated by ',' | |
.Add<TwitterUsers>("/twitter/{ScreenNames}") //screenNames serparated by ',' | |
; | |
} | |
private void ConfigureAuth(Funq.Container container) | |
{ | |
//Enable and register existing services you want this host to make use of. | |
//Look in Web.config for examples on how to configure your oauth providers, e.g. oauth.facebook.AppId, etc. | |
var appSettings = new AppSettings(); | |
//Register all Authentication methods you want to enable for this web app. | |
Plugins.Add(new AuthFeature( | |
() => new CustomUserSession(), //Use your own typed Custom UserSession type | |
new IAuthProvider[] { | |
new CredentialsAuthProvider(), //HTML Form post of UserName/Password credentials | |
//new TwitterAuthProvider(appSettings), //Sign-in with Twitter | |
//new FacebookAuthProvider(appSettings), //Sign-in with Facebook | |
//new DigestAuthProvider(appSettings), //Sign-in with Digest Auth | |
//new BasicAuthProvider(), //Sign-in with Basic Auth | |
new GoogleOpenIdOAuthProvider(appSettings), //Sign-in with Google OpenId | |
new YahooOpenIdOAuthProvider(appSettings), //Sign-in with Yahoo OpenId | |
new OpenIdOAuthProvider(appSettings), //Sign-in with Custom OpenId | |
})); | |
//Provide service for new users to register so they can login with supplied credentials. | |
Plugins.Add(new RegistrationFeature()); | |
//override the default registration validation with your own custom implementation | |
container.RegisterAs<CustomRegistrationValidator, IValidator<Registration>>(); | |
//Store User Data into the referenced SqlServer database | |
container.Register<IUserAuthRepository>(c => new RavenUserAuthRepository(c.Resolve<IDocumentStore>())); | |
Plugins.Add(new RequestLogsFeature()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MvcApplication : System.Web.HttpApplication | |
{ | |
public static void RegisterGlobalFilters(GlobalFilterCollection filters) | |
{ | |
filters.Add(new HandleErrorAttribute()); | |
} | |
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
routes.IgnoreRoute("Content/{*pathInfo}"); | |
routes.IgnoreRoute("api/{*pathInfo}"); | |
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); | |
} | |
protected void Application_Start() | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
RegisterGlobalFilters(GlobalFilters.Filters); | |
RegisterRoutes(RouteTable.Routes); | |
var documentStore = new EmbeddableDocumentStore | |
{ | |
RunInMemory = true | |
}.Initialize(); | |
new AppHost(documentStore).Init(); | |
} | |
protected void Application_BeginRequest(object sender, EventArgs e) | |
{ | |
Console.WriteLine("Application_BeginRequest"); | |
if (Request.IsLocal) | |
Profiler.Start(); | |
} | |
protected void Application_EndRequest(object sender, EventArgs e) | |
{ | |
Console.WriteLine("Application_EndRequest"); | |
Profiler.Stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment