1+ mod web;
2+
13use axum:: extract:: Query ;
24use clap:: Parser ;
35use serde:: Deserialize ;
@@ -8,7 +10,7 @@ use crate::{ffi::{set_toast_alpha, set_toast_position, set_toast_wh, show_toast}
810#[ derive( Parser , Debug , Clone ) ]
911#[ command( version = "0.0.1" , about = "Windows文本消息吐司API服务" , long_about = "Windows文本消息吐司API服务
1012API:
11- - GET http://IP:PORT/toast?msg=123456&time=2000 (设置消息与显示时长)
13+ - GET http://IP:PORT/toast?msg=123456&time=2000 (设置消息与显示时长[毫秒], 0为永久显示 )
1214- GET http://IP:PORT/setpos?x=1000&y=550 (设置窗口位置)
1315- GET http://IP:PORT/setwh?w=860&h=200 (设置窗口宽高)
1416- GET http://IP:PORT/setalpha?alpha=190 (设置窗口整体不透明度, 0为完全透明, 255为完全不透明)
@@ -62,12 +64,21 @@ async fn handler_set_alpha(Query(alpha): Query<Alpha>) {
6264 set_toast_alpha ( alpha. alpha ) ;
6365}
6466
67+ #[ cfg( windows) ]
68+ #[ allow( unused) ]
69+ unsafe extern "system" {
70+ fn FreeConsole ( ) ;
71+ fn AllocConsole ( ) ;
72+ fn WinExec ( cmd : * const u8 , cmdShow : u8 ) ;
73+ }
74+
6575pub async fn main ( param : Param ) -> tokio:: io:: Result < ( ) > {
6676 let router = axum:: Router :: new ( )
6777 . route ( "/toast" , axum:: routing:: get ( handler) )
6878 . route ( "/setpos" , axum:: routing:: get ( handler_setpos) )
6979 . route ( "/setwh" , axum:: routing:: get ( handler_setwh) )
7080 . route ( "/setalpha" , axum:: routing:: get ( handler_set_alpha) )
81+ . fallback_service ( web:: route_assets ( ) )
7182 . layer ( CorsLayer :: permissive ( ) )
7283 . with_state ( param. clone ( ) )
7384 . into_make_service ( ) ;
@@ -84,6 +95,12 @@ pub async fn main(param: Param) -> tokio::io::Result<()> {
8495 show_toast ( "服务已启动, 调用/toast接口, 您的消息将在这里显示" , 0 ) ;
8596 set_toast_position ( 1000 , 550 ) ;
8697 } ) ;
98+ #[ cfg( windows) ]
99+ unsafe {
100+ WinExec ( format ! ( "explorer http://127.0.0.1:{}\0 " , port) . as_ptr ( ) , 0 ) ;
101+ // FreeConsole();
102+ // AllocConsole();
103+ }
87104 axum:: serve ( listener, router) . await ?;
88105 Ok ( ( ) )
89106}
0 commit comments