Skip to content

Commit

Permalink
Replaced Microsoft's multiple + overly bundled NuGet WebPages + Razor…
Browse files Browse the repository at this point in the history
… packages with a single ServiceStack.Razor package which has all the requried deps.

Copied Razor view into Self-Host + WinService projects
  • Loading branch information
mythz committed Sep 15, 2012
1 parent 9464b44 commit bb093e7
Show file tree
Hide file tree
Showing 174 changed files with 6,795 additions and 246 deletions.
127 changes: 37 additions & 90 deletions src/RazorRockstars.SelfHost/AppHost.cs
@@ -1,131 +1,78 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Net;
using Funq;
using ServiceStack.Common;
using ServiceStack.DataAnnotations;
using ServiceStack.Logging;
using ServiceStack.Logging.Support.Logging;
using ServiceStack.OrmLite;
using ServiceStack.Razor;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;

//The entire C# code for the stand-alone RazorRockstars demo.
namespace RazorRockstars
namespace RazorRockstars.SelfHost
{
public class AppHost : AppHostHttpListenerBase
public class AppHost : AppHostHttpListenerBase
{
public static Rockstar[] SeedData = new[] {
new Rockstar(1, "Jimi", "Hendrix", 27, false),
new Rockstar(2, "Janis", "Joplin", 27, false),
new Rockstar(4, "Kurt", "Cobain", 27, false),
new Rockstar(5, "Elvis", "Presley", 42, false),
new Rockstar(6, "Michael", "Jackson", 50, false),
new Rockstar(7, "Eddie", "Vedder", 47, true),
new Rockstar(8, "Dave", "Grohl", 43, true),
new Rockstar(9, "Courtney", "Love", 48, true),
new Rockstar(10, "Bruce", "Springsteen", 62, true),
};

public AppHost() : base("Test Razor", typeof(AppHost).Assembly) { }

public override void Configure(Container container)
{
LogManager.LogFactory = new ConsoleLogFactory();

Plugins.Add(new RazorFormat());

container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(":memory:", false, SqliteDialect.Provider));

using (var db = container.Resolve<IDbConnectionFactory>().OpenDbConnection())
{
db.CreateTable<Rockstar>(overwrite: false); //Create table if not exists
db.Insert(Rockstar.SeedData); //Populate with seed data
db.CreateTableIfNotExists<Rockstar>();
db.InsertAll<Rockstar>(SeedData);
}
}

private static void Main(string[] args)
{
var appHost = new AppHost();
appHost.Init();
appHost.Start("http://*:1337/");
System.Console.WriteLine("Listening on http://localhost:1337/ ...");
System.Console.ReadLine();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
SetConfig(new EndpointHostConfig {
CustomHttpHandlers = {
{ HttpStatusCode.NotFound, new RazorHandler("/notfound") }
}
});
}
}

//Poco Data Model for OrmLite + SeedData
public class Rockstar
{
public static Rockstar[] SeedData = new[] {
new Rockstar(1, "Jimi", "Hendrix", 27),
new Rockstar(2, "Janis", "Joplin", 27),
new Rockstar(3, "Jim", "Morrisson", 27),
new Rockstar(4, "Kurt", "Cobain", 27),
new Rockstar(5, "Elvis", "Presley", 42),
new Rockstar(6, "Michael", "Jackson", 50),
};

[AutoIncrement]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
public bool Alive { get; set; }

public string Url
{
get { return "/stars/{0}/{1}".Fmt(Alive ? "alive" : "dead", LastName.ToLower()); }
}

public Rockstar() { }
public Rockstar(int id, string firstName, string lastName, int age)
public Rockstar(int id, string firstName, string lastName, int age, bool alive)
{
Id = id;
FirstName = firstName;
LastName = lastName;
Age = age;
}
}

[RestService("/rockstars")]
[RestService("/rockstars/aged/{Age}")]
[RestService("/rockstars/delete/{Delete}")]
[RestService("/rockstars/{Id}")]
public class Rockstars
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? Age { get; set; }
public string Delete { get; set; }
}

[DataContract] //Attrs for CSV Format to recognize it's a DTO and serialize the Enumerable property
public class RockstarsResponse
{
[DataMember] public int Total { get; set; }
[DataMember] public int? Aged { get; set; }
[DataMember] public List<Rockstar> Results { get; set; }
}

public class RockstarsService : RestServiceBase<Rockstars>
{
public IDbConnectionFactory DbFactory { get; set; }

public override object OnGet(Rockstars request)
{
using (var db = DbFactory.OpenDbConnection())
{
if (request.Delete == "reset")
{
db.DeleteAll<Rockstar>();
db.Insert(Rockstar.SeedData);
}
else if (request.Delete.IsInt())
{
db.DeleteById<Rockstar>(request.Delete.ToInt());
}

return new RockstarsResponse {
Aged = request.Age,
Total = db.GetScalar<int>("select count(*) from Rockstar"),
Results = request.Id != default(int) ?
db.Select<Rockstar>(q => q.Id == request.Id)
: request.Age.HasValue ?
db.Select<Rockstar>(q => q.Age == request.Age.Value)
: db.Select<Rockstar>()
};
}
}

public override object OnPost(Rockstars request)
{
using (var db = DbFactory.OpenDbConnection())
{
db.Insert(request.TranslateTo<Rockstar>());
return OnGet(new Rockstars());
}
Alive = alive;
}
}
}
29 changes: 29 additions & 0 deletions src/RazorRockstars.SelfHost/NoModelNoController.cshtml
@@ -0,0 +1,29 @@
@inherits ViewPage

@{
Layout = "SimpleLayout";
ViewBag.Title = "Page with no model and no C# controller - just this page :)";
int age = 0;
var hasAge = Request.QueryString["Age"] != null && int.TryParse(Model.Age, out age);
var rockstars = hasAge
? Db.Select<Rockstar>(q => q.Age == age)
: Db.Select<Rockstar>();
var title = hasAge ? "{0} year old rockstars".Fmt(age) : "All Rockstars";
}
<div id="content-page">

@Html.Partial("OtherPages")

<div>@title</div>
<ul>
@foreach (var rockstar in rockstars) {
<li><a href="@rockstar.Url">@rockstar.LastName, @rockstar.FirstName</a> (<a href="?Age=@rockstar.Age">@rockstar.Age</a>)</li>
}
</ul>

<p><a href="?">Show all Rockstars</a></p>

<h2>Razor View</h2>
<script src="https://gist.github.com/3162494.js"></script>

</div>
79 changes: 79 additions & 0 deletions src/RazorRockstars.SelfHost/NotFound.cshtml
@@ -0,0 +1,79 @@
@{
Layout = "Empty";
var search = (@Request.PathInfo ?? "").SplitOnLast("/").Last().Trim();
}
<!DOCTYPE HTML>
<html>
<head>
<title>Page Not Found</title>
<style>
BODY {
background-color:white;
color:#000000;
font-family:Verdana;
margin: 0;
}
H1 {
background-color: #c30;
color: #FFF;
font-family: Tahoma;
font-size: 26px;
font-weight: normal;
margin: 0;
padding: 10px 0 3px 15px;
}
#content {
padding: 15px;
}
input {
padding: 5px 8px;
}
#preview {
background: #fff;
box-shadow: 0 3px 8px rgba(100, 100, 100, 0.3);
font-size: 74%;
padding: 10px 40px 20px 40px;
margin: auto;
max-width: 960px;
}
#preview {
margin: 40px auto;
}
#preview h3 a {
color: green;
}
#preview h3 b
{
display: inline-block;
width: 100px;
}
</style>
</head>
<body>
<h1>Page Not Found</h1>

<div id="content">
<p>
We're sorry <b>@Request.PathInfo</b> is no longer here.
</p>
@if (!string.IsNullOrEmpty(search))
{
<p>
Maybe Google has it?
</p>
<form action="https://www.google.com/search" method="GET">
<input type="search" name="q" value="@search"/>
<input type="submit" value="Search"/>
</form>
}
</div>

<div id="preview">
<h2>Implemenation</h2>
<h3><b>View</b> <a href="https://github.com/ServiceStack/RazorRockstars/blob/master/src/RazorRockstars.WebHost/NotFound.cshtml">/NotFound.cshtml</a></h3>
<script src="https://gist.github.com/3618038.js"></script>
</div>

</body>
</html>
14 changes: 11 additions & 3 deletions src/RazorRockstars.SelfHost/Program.cs
@@ -1,14 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Logging;
using ServiceStack.Logging.Support.Logging;
using ServiceStack.Text;

namespace RazorRockstars.SelfHost
{
class Program
{
static void Main(string[] args)
{
LogManager.LogFactory = new ConsoleLogFactory();

var appHost = new AppHost();
appHost.Init();
appHost.Start("http://*:2001/");

"\n\nType any key to quit..".Print();
Console.ReadKey();
}
}
}

0 comments on commit bb093e7

Please sign in to comment.