-
Notifications
You must be signed in to change notification settings - Fork 3
/
SnippetController.cs
81 lines (73 loc) · 2.36 KB
/
SnippetController.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Configuration;
namespace LinqpadServer.WebApi.Controllers
{
[RoutePrefix("api/snippet")]
public class SnippetController : ApiController
{
/// <summary>
/// Uncomment and modify this setting in Web.Config to override the default snippet directory
/// </summary>
private const string SnippetDefaultDirConfigurationKey = "SnippetDefaultDir";
// this lists snippets
private readonly SnippetRepo _repo;
// this executes snippets
private readonly LpRunner _runner = new LpRunner();
/// <summary>
/// New instance of snippet controller
/// </summary>
public SnippetController()
{
var snippetDir = ConfigurationManager.AppSettings[SnippetDefaultDirConfigurationKey];
if (snippetDir != null)
{
_repo = new SnippetRepo(new DirectoryInfo(snippetDir));
}
else
{
_repo = new SnippetRepo();
}
}
/// <summary>
/// List the available snippets
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("list")]
public IEnumerable<string> List()
{
return _repo.GetSnippets();
}
/// <summary>
/// Run a snippet with arguments
/// </summary>
/// <param name="snippet">Name of snippet to run</param>
/// <param name="arguments">arguments</param>
/// <returns>Standard output</returns>
[HttpPost]
[Route("run/{snippetName}")]
public IEnumerable<string> RunArgs(string snippetName, [FromBody]string arguments)
{
var file = _repo.GetFileFromName(snippetName);
return _runner.Run(file, arguments);
}
/// <summary>
/// Run a snippet with no arguments
/// </summary>
/// <param name="snippet">Name of snippet to run</param>
/// <returns>Standard output</returns>
[HttpGet]
[Route("run/{snippetName}")]
public IEnumerable<string> Run(string snippetName)
{
var file = _repo.GetFileFromName(snippetName);
return _runner.Run(file);
}
}
}