Skip to content

Commit

Permalink
release 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnoyong committed Aug 22, 2024
1 parent 3a29dc2 commit 4b3f4b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 165 deletions.
3 changes: 1 addition & 2 deletions OneNoteSearcher.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputType>Library</OutputType>
<TargetFramework>net481</TargetFramework>
<LangVersion>9.0</LangVersion>
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -24,7 +24,6 @@
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="ScipBe.Common.Office.OneNote" Version="3.0.1" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1" />
</ItemGroup>

</Project>
180 changes: 17 additions & 163 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,179 +1,33 @@
using System;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.WebUtilities;
using Newtonsoft.Json;
using Newtonsoft.Json;
using ScipBe.Common.Office.OneNote;
using System.Timers;
using System.Threading;
using System.CommandLine;
using System.CommandLine.Invocation;
using Microsoft.Office.Interop.OneNote;


class Program
namespace OneNoteSearcher
{
static System.Timers.Timer timer;
static RootCommand rootCommand;

static async Task Main(string[] args)
{
rootCommand = new RootCommand();
var waitingTimeOption = new Option<int>(
new string[] { "--time", "-t" },
() => 0,
"待命时间(秒),默认为0"
);
var portOption = new Option<int>(
new string[] { "--port", "-p" },
() => 8022,
"监听端口,默认 8022"
);

// 创建根命令并添加选项
rootCommand.AddOption(waitingTimeOption);
rootCommand.AddOption(portOption);
Func<int, int, Task> action = async (time, port) =>
{
using (Mutex mutex = new Mutex(true, "OneNoteSearcher", out bool createdNew))
{
if (createdNew)
{
// 设置 HTTP 服务监听地址和端口号
string ipAddress = "127.0.0.1";
// 创建 HttpListener 以监听 HTTP 连接
HttpListener listener = new HttpListener();
listener.Prefixes.Add($"http://{ipAddress}:{port}/");
listener.Start();
Console.WriteLine($"HTTP server started on {ipAddress}:{port}");
if (time > 0)
{
time *= 1000;
timer = new System.Timers.Timer(time);
timer.Elapsed += TimerElapsed;
timer.AutoReset = false;
timer.Start();
}
while (true)
{
// 接受客户端连接并处理请求
var context = await listener.GetContextAsync();
_ = Task.Run(() => ProcessHttpRequest(context, time)); // 使用 Task.Run 启动新的线程处理请求
}
}
else
{
// 如果互斥体已存在,则关闭当前实例
Console.WriteLine("Another instance is already running. Exiting...");
Thread.Sleep(2000); // 为了让用户看到提示信息,程序暂停2秒钟
}
}
};
rootCommand.Handler = CommandHandler.Create<int, int>(action);
await rootCommand.InvokeAsync(args);
}

private static void TimerElapsed(object sender, ElapsedEventArgs e)
{
Thread.Sleep(2000);
Console.WriteLine("Timeout, The instance will exit after 2 seconds");
Environment.Exit(0);
}

static async Task ProcessHttpRequest(HttpListenerContext context, int waiting)
public class Utils
{
if (waiting > 0)
{
timer.Stop();
timer.Interval = waiting;
timer.Start();
}

// 获取请求路径
string path = context.Request.Url.AbsolutePath;
string responseMessage = string.Empty;

if (path.StartsWith("/search"))
public static string search(string keyword)
{
if (context.Request.HttpMethod == "GET")
if (string.IsNullOrEmpty(keyword))
{
responseMessage = HandleSearchRequest(context);
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
responseMessage = "Method not allowed";
return "Missing keyword parameter";
}

var pages = OneNoteProvider.FindPages(keyword);
return JsonConvert.SerializeObject(pages);
}
else if (path.StartsWith("/open"))

public static void open(string id)
{
if (context.Request.HttpMethod == "GET")
if (string.IsNullOrEmpty(id))
{
responseMessage = HandleOpenRequest(context);
return "Missing id parameter";
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
responseMessage = "Method not allowed";
}
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
responseMessage = "Invalid API endpoint";
}

// 设置响应状态码和内容类型
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "application/json";
// 发送响应
byte[] buffer = Encoding.UTF8.GetBytes(responseMessage);
context.Response.ContentLength64 = buffer.Length;
await context.Response.OutputStream.WriteAsync(buffer, 0, buffer.Length);
context.Response.OutputStream.Close();

Console.WriteLine("Response sent.");
}

static string HandleSearchRequest(HttpListenerContext context)
{
// 获取 URL 参数
string query = context.Request.Url.Query;
var queryParameters = QueryHelpers.ParseQuery(query);
string keyword = queryParameters.ContainsKey("keyword") ? queryParameters["keyword"].ToString() : null;

if (string.IsNullOrEmpty(keyword))
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
return "Missing keyword parameter";
Microsoft.Office.Interop.OneNote.Application oneNote;
oneNote = new Microsoft.Office.Interop.OneNote.Application();
oneNote.NavigateTo(id);
}

Console.WriteLine($"Searching for {keyword}");
var pages = OneNoteProvider.FindPages(keyword);
return JsonConvert.SerializeObject(pages);
}

static string HandleOpenRequest(HttpListenerContext context)
{
// 获取 URL 参数
string query = context.Request.Url.Query;
var queryParameters = QueryHelpers.ParseQuery(query);
string id = queryParameters.ContainsKey("id") ? queryParameters["id"].ToString() : null;

if (string.IsNullOrEmpty(id))
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
return "Missing id parameter";
}

Console.WriteLine($"Open {id}");
Microsoft.Office.Interop.OneNote.Application oneNote;
oneNote = new Microsoft.Office.Interop.OneNote.Application();
oneNote.NavigateTo(id);
return "Succeed";
}
}

0 comments on commit 4b3f4b1

Please sign in to comment.