├── .gitattributes ├── .gitignore ├── AkkaWebApi.sln ├── AkkaWebApi.sln.GhostDoc.xml ├── AkkaWebApi ├── Actors │ ├── BaseApi.cs │ └── ServerActor.cs ├── AkkaWebApi.csproj ├── Api │ └── HelloApi.cs ├── App.config ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── Responce.cs └── packages.config ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | [Dd]ebug/ 11 | [Dd]ebugPublic/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | bld/ 16 | [Bb]in/ 17 | [Oo]bj/ 18 | 19 | # Roslyn cache directories 20 | *.ide/ 21 | 22 | # MSTest test Results 23 | [Tt]est[Rr]esult*/ 24 | [Bb]uild[Ll]og.* 25 | 26 | #NUNIT 27 | *.VisualState.xml 28 | TestResult.xml 29 | 30 | # Build Results of an ATL Project 31 | [Dd]ebugPS/ 32 | [Rr]eleasePS/ 33 | dlldata.c 34 | 35 | *_i.c 36 | *_p.c 37 | *_i.h 38 | *.ilk 39 | *.meta 40 | *.obj 41 | *.pch 42 | *.pdb 43 | *.pgc 44 | *.pgd 45 | *.rsp 46 | *.sbr 47 | *.tlb 48 | *.tli 49 | *.tlh 50 | *.tmp 51 | *.tmp_proj 52 | *.log 53 | *.vspscc 54 | *.vssscc 55 | .builds 56 | *.pidb 57 | *.svclog 58 | *.scc 59 | 60 | # Chutzpah Test files 61 | _Chutzpah* 62 | 63 | # Visual C++ cache files 64 | ipch/ 65 | *.aps 66 | *.ncb 67 | *.opensdf 68 | *.sdf 69 | *.cachefile 70 | 71 | # Visual Studio profiler 72 | *.psess 73 | *.vsp 74 | *.vspx 75 | 76 | # TFS 2012 Local Workspace 77 | $tf/ 78 | 79 | # Guidance Automation Toolkit 80 | *.gpState 81 | 82 | # ReSharper is a .NET coding add-in 83 | _ReSharper*/ 84 | *.[Rr]e[Ss]harper 85 | *.DotSettings.user 86 | 87 | # JustCode is a .NET coding addin-in 88 | .JustCode 89 | 90 | # TeamCity is a build add-in 91 | _TeamCity* 92 | 93 | # DotCover is a Code Coverage Tool 94 | *.dotCover 95 | 96 | # NCrunch 97 | _NCrunch_* 98 | .*crunch*.local.xml 99 | 100 | # MightyMoose 101 | *.mm.* 102 | AutoTest.Net/ 103 | 104 | # Web workbench (sass) 105 | .sass-cache/ 106 | 107 | # Installshield output folder 108 | [Ee]xpress/ 109 | 110 | # DocProject is a documentation generator add-in 111 | DocProject/buildhelp/ 112 | DocProject/Help/*.HxT 113 | DocProject/Help/*.HxC 114 | DocProject/Help/*.hhc 115 | DocProject/Help/*.hhk 116 | DocProject/Help/*.hhp 117 | DocProject/Help/Html2 118 | DocProject/Help/html 119 | 120 | # Click-Once directory 121 | publish/ 122 | 123 | # Publish Web Output 124 | *.[Pp]ublish.xml 125 | *.azurePubxml 126 | ## TODO: Comment the next line if you want to checkin your 127 | ## web deploy settings but do note that will include unencrypted 128 | ## passwords 129 | #*.pubxml 130 | 131 | # NuGet Packages Directory 132 | packages/* 133 | ## TODO: If the tool you use requires repositories.config 134 | ## uncomment the next line 135 | #!packages/repositories.config 136 | 137 | # Enable "build/" folder in the NuGet Packages folder since 138 | # NuGet packages use it for MSBuild targets. 139 | # This line needs to be after the ignore of the build folder 140 | # (and the packages folder if the line above has been uncommented) 141 | !packages/build/ 142 | 143 | # Windows Azure Build Output 144 | csx/ 145 | *.build.csdef 146 | 147 | # Windows Store app package directory 148 | AppPackages/ 149 | 150 | # Others 151 | sql/ 152 | *.Cache 153 | ClientBin/ 154 | [Ss]tyle[Cc]op.* 155 | ~$* 156 | *~ 157 | *.dbmdl 158 | *.dbproj.schemaview 159 | *.pfx 160 | *.publishsettings 161 | node_modules/ 162 | 163 | # RIA/Silverlight projects 164 | Generated_Code/ 165 | 166 | # Backup & report files from converting an old project file 167 | # to a newer Visual Studio version. Backup files are not needed, 168 | # because we have git ;-) 169 | _UpgradeReport_Files/ 170 | Backup*/ 171 | UpgradeLog*.XML 172 | UpgradeLog*.htm 173 | 174 | # SQL Server files 175 | *.mdf 176 | *.ldf 177 | 178 | # Business Intelligence projects 179 | *.rdl.data 180 | *.bim.layout 181 | *.bim_*.settings 182 | 183 | # Microsoft Fakes 184 | FakesAssemblies/ 185 | 186 | # LightSwitch generated files 187 | GeneratedArtifacts/ 188 | _Pvt_Extensions/ 189 | ModelManifest.xml -------------------------------------------------------------------------------- /AkkaWebApi.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AkkaWebApi", "AkkaWebApi\AkkaWebApi.csproj", "{F8AC0050-1FC5-4307-B7EC-415F2FEBEF7C}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {F8AC0050-1FC5-4307-B7EC-415F2FEBEF7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {F8AC0050-1FC5-4307-B7EC-415F2FEBEF7C}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {F8AC0050-1FC5-4307-B7EC-415F2FEBEF7C}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {F8AC0050-1FC5-4307-B7EC-415F2FEBEF7C}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /AkkaWebApi.sln.GhostDoc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | *.min.js 4 | jquery*.js 5 | 6 | 7 | -------------------------------------------------------------------------------- /AkkaWebApi/Actors/BaseApi.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | using System.Text; 4 | using Akka; 5 | using Akka.Actor; 6 | using Newtonsoft.Json; 7 | 8 | namespace AkkaWebApi.Actors 9 | { 10 | public class BaseApi : ReceiveActor 11 | { 12 | public void Get(Func handler) 13 | { 14 | Get(req => new Responce(HttpStatusCode.OK) { Result = handler(req) }); 15 | } 16 | 17 | public void Get(Func handler) 18 | { 19 | Receive(ctx => ctx.Request.HttpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase), 20 | ctx => SendReply(handler, ctx)); 21 | } 22 | 23 | public void Put(Func handler) 24 | { 25 | Put(req => new Responce(HttpStatusCode.OK) { Result = handler(req) }); 26 | } 27 | 28 | public void Put(Func handler) 29 | { 30 | Receive(ctx => ctx.Request.HttpMethod.Equals("PUT", StringComparison.OrdinalIgnoreCase), 31 | ctx => SendReply(handler, ctx)); 32 | } 33 | 34 | public void Delete(Func handler) 35 | { 36 | Delete(req => new Responce(HttpStatusCode.OK) { Result = handler(req) }); 37 | } 38 | 39 | public void Delete(Func handler) 40 | { 41 | Receive(ctx => ctx.Request.HttpMethod.Equals("DELETE", StringComparison.OrdinalIgnoreCase), 42 | ctx => SendReply(handler, ctx)); 43 | } 44 | 45 | public void Post(Func handler) 46 | { 47 | Receive(ctx => ctx.Request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase), 48 | ctx => SendReply(handler, ctx)); 49 | } 50 | 51 | public void Post(Func handler) 52 | { 53 | Post(req => new Responce(HttpStatusCode.OK) { Result = handler(req) }); 54 | } 55 | 56 | private void SendReply(Func handler, HttpListenerContext ctx) 57 | { 58 | var res = handler(ctx.Request); 59 | using (var stream = ctx.Response.OutputStream) 60 | { 61 | if (res == null) 62 | { 63 | ctx.Response.StatusCode = (int)HttpStatusCode.InternalServerError; 64 | } 65 | else 66 | { 67 | ctx.Response.StatusCode = (int)res.Code; 68 | if (res.Headers != null) 69 | foreach (var header in res.Headers) 70 | { 71 | ctx.Response.Headers[header.Key] = header.Value; 72 | } 73 | 74 | if (res.Result != null) 75 | { 76 | var s = JsonConvert.SerializeObject(res.Result); 77 | var buffer = Encoding.UTF8.GetBytes(s); 78 | stream.Write(buffer, 0, buffer.Length); 79 | } 80 | } 81 | } 82 | } 83 | 84 | protected override void Unhandled(object message) 85 | { 86 | message.Match() 87 | .With(ctx => 88 | { 89 | SendReply(_ => new Responce(HttpStatusCode.NotFound), ctx); 90 | }); 91 | } 92 | } 93 | } -------------------------------------------------------------------------------- /AkkaWebApi/Actors/ServerActor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | using Akka.Actor; 4 | using AkkaWebApi.Api; 5 | 6 | namespace AkkaWebApi.Actors 7 | { 8 | public class ServerActor : ReceiveActor 9 | { 10 | private HttpListener _listener; 11 | 12 | public ServerActor() 13 | { 14 | _listener = new HttpListener(); 15 | Receive(s => s.Equals("start"), s => 16 | { 17 | _listener.Prefixes.Add("http://*:5555/"); 18 | _listener.Start(); 19 | DoListen(); 20 | }); 21 | 22 | Receive(context => 23 | { 24 | var handler = Context.ActorSelection("/user" + context.Request.RawUrl); 25 | try 26 | { 27 | var actorRef = handler.ResolveOne(TimeSpan.FromMilliseconds(10)).Result; 28 | actorRef.Tell(context); 29 | } 30 | catch (Exception ex) 31 | { 32 | context.Response.StatusCode = (int)HttpStatusCode.NotFound; 33 | using (var str = context.Response.OutputStream) { } 34 | } 35 | finally 36 | { 37 | DoListen(); 38 | } 39 | }); 40 | } 41 | 42 | protected override void Unhandled(object message) 43 | { 44 | base.Unhandled(message); 45 | } 46 | 47 | private void DoListen() 48 | { 49 | _listener.GetContextAsync().PipeTo(Self); 50 | } 51 | 52 | protected override void PreStart() 53 | { 54 | var actorRef = Context.ActorOf("hello"); 55 | base.PreStart(); 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /AkkaWebApi/AkkaWebApi.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {F8AC0050-1FC5-4307-B7EC-415F2FEBEF7C} 8 | Exe 9 | Properties 10 | AkkaWebApi 11 | AkkaWebApi 12 | v4.5 13 | 512 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | ..\packages\Akka.1.0.1\lib\net45\Akka.dll 37 | 38 | 39 | False 40 | ..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 70 | -------------------------------------------------------------------------------- /AkkaWebApi/Api/HelloApi.cs: -------------------------------------------------------------------------------- 1 | using AkkaWebApi.Actors; 2 | 3 | namespace AkkaWebApi.Api 4 | { 5 | public class HelloApi : BaseApi 6 | { 7 | public HelloApi() 8 | { 9 | Get(req => "Hello from Akka"); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /AkkaWebApi/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /AkkaWebApi/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Net.Sockets; 4 | using System.Threading.Tasks; 5 | using Akka.Actor; 6 | using AkkaWebApi.Actors; 7 | 8 | namespace AkkaWebApi 9 | { 10 | class Program 11 | { 12 | public static ActorSystem System { get; private set; } 13 | 14 | static void Main(string[] args) 15 | { 16 | System = ActorSystem.Create("server"); 17 | var api = System.ActorOf("api"); 18 | api.Tell("start"); 19 | 20 | Console.ReadLine(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /AkkaWebApi/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("AkkaWebApi")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("AkkaWebApi")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("8c2f940a-bd8b-48aa-9d09-0ccac1cdb335")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /AkkaWebApi/Responce.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Net; 3 | 4 | namespace AkkaWebApi 5 | { 6 | public class Responce 7 | { 8 | public Responce(HttpStatusCode code) 9 | { 10 | Code = code; 11 | } 12 | 13 | public HttpStatusCode Code { get; private set; } 14 | 15 | public object Result { get; set; } 16 | 17 | public Dictionary Headers { get; set; } 18 | } 19 | } -------------------------------------------------------------------------------- /AkkaWebApi/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 shersh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AkkaNetRest 2 | Proof of concept for using Akka.NET for creating REST api services 3 | 4 | ## Using 5 | 6 | Creating simple api "controller": 7 | 8 | ```CSHARP 9 | public class HelloApi : BaseApi 10 | { 11 | public HelloApi() 12 | { 13 | Get(req => "Hello from Akka"); 14 | } 15 | } 16 | ``` 17 | 18 | And after instiatiate it in ```api``` actor : 19 | 20 | ``` 21 | Context.ActorOf("hello"); 22 | ``` 23 | 24 | now you can call ```GET``` method for ```/api/hello``` 25 | --------------------------------------------------------------------------------