-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
51 lines (50 loc) · 2.23 KB
/
Program.cs
File metadata and controls
51 lines (50 loc) · 2.23 KB
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
namespace PicoBox
{
using PicoServer;
internal class Program
{
//核心:创建一个全局的 WebAPIServer 实例
static readonly WebAPIServer MyAPI = new();
static void Main(string[] args)
{
//绘制一个简约的PicoBox控制台界面
Console.Title = "PicoBox";
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("╔═════════════════════════════════╗");
Console.WriteLine("║ PicoBox 已启动 ║");
Console.WriteLine("║ 人人可用的 AI 网页托管工具 ║");
Console.WriteLine("║ 开源免费 ║");
Console.WriteLine("╚═════════════════════════════════╝");
Console.ResetColor();
//输出访问地址
MyAPI.GetLocalIPAddresses().ForEach(ip => Console.WriteLine($" 访问地址: http://{ip}:8090"));
//初始化 html 目录和文件
if (!Directory.Exists("www")) Directory.CreateDirectory("www");
if(!File.Exists("data")) File.WriteAllText("data","hello PicoBox!");
//添加静态文件服务
MyAPI.AddStaticFiles("/", "www");
//添加一个数据API接口
MyAPI.AddRoute("/api/data", async (req, resp) =>
{
switch (req.HttpMethod)
{
case "GET":
//返回data文件内容
await resp.SendFileAsync("data"); break;
case "POST":
//保存请求体到data文件
await req.SaveFileAsync("data");
//返回ok
await resp.WriteAsync("ok"); break;
default:
//不支持其他方法
resp.StatusCode = 405;
await resp.WriteAsync("error"); break;
}
});
//启动服务器
MyAPI.StartServer();
Thread.Sleep(Timeout.Infinite);
}
}
}