/
InfoController.cs
115 lines (94 loc) · 3.44 KB
/
InfoController.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.IO;
using System.Text;
using TeamMngtWS.Model;
using TeamMngtWS.Log;
namespace TeamMngtWS.Controllers
{
[Route("/")]
public class InfoController : BaseController
{
private const string MESSAGE_FORMAT = "Hello, {0}!";
public InfoController(ILogger logger):base(logger)
{
}
//public InfoController() : base(null) { }
[HttpGet()]
public JsonResult Get([FromQuery] string name = "GET command")
{
Log("call Get method");
return Json(new Response
{
output = string.Format(MESSAGE_FORMAT, name)
});
}
[HttpGet(nameof(About))]
public JsonResult About()
{
Log("call About method");
return Json(new Response
{
output = string.Format("Process info: Path: {0}, Process ID: {1}", Environment.CurrentDirectory, Process.GetCurrentProcess()?.Id)
});
}
[HttpGet(nameof(ListDir))]
public JsonResult ListDir()
{
StringBuilder result = new StringBuilder();
List<DirectoryInfo> info = DirectoryUtil.GetAllDirecrotiesUnderDirectory(Environment.CurrentDirectory);
//result.AppendLine($"There are {info.GetFiles().Length} files in folder {Environment.CurrentDirectory}")
foreach (DirectoryInfo directory in info)
{
result.AppendLine($"Directory {directory.FullName}, modified: {directory.LastWriteTimeUtc}");
}
return Json(result.ToString());
}
[HttpGet(nameof(ListFiles))]
public JsonResult ListFiles()
{
StringBuilder result = new StringBuilder();
List<FileInfo> info = DirectoryUtil.GetAllFileUnderDirectory(Environment.CurrentDirectory);
//result.AppendLine($"There are {info.GetFiles().Length} files in folder {Environment.CurrentDirectory}")
foreach (FileInfo file in info)
{
result.AppendLine($"File {file.FullName}, created: {file.CreationTimeUtc}, modified: {file.LastWriteTimeUtc}");
}
return Json(result.ToString());
}
// GET http://*:*/ReadLog/{rows}
[HttpGet(nameof(ReadLog))]
[Route(nameof(ReadLog) + "/{rows}")]
public JsonResult ReadLog(int rows)
{
string content = _logger.Read(rows);
return Json(string.Format("Log content: {0}", content));
}
[HttpGet(nameof(Ping))]
public IActionResult Ping(string url)
{
if(Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute)==false)
{
return BadRequest($"Invalid URL: {url}");
}
try
{
Ping ping = new Ping();
PingReply reply = ping.Send(url);
return Json(new Response
{
output = string.Format($"Ping result to URL {url}: {reply.Status}")
});
}
catch(Exception ex)
{
return BadRequest($"Error while pinging to {url}: {ex.Message}");
}
}
}
}