Skip to content

Commit

Permalink
feat: 完成基础功能开发
Browse files Browse the repository at this point in the history
  • Loading branch information
nashaofu committed Jun 16, 2023
1 parent 1ad63b2 commit 2f152cf
Show file tree
Hide file tree
Showing 55 changed files with 8,341 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
199 changes: 199 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
name: build

on:
workflow_dispatch: # 手动触发
push:
branches: ["master"]
# Publish semver tags as releases.
tags: ["v*.*.*"]
pull_request:
branches: ["master"]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io

jobs:
build-web:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: latest
cache: "yarn"
cache-dependency-path: web/yarn.lock
- name: Build web
run: cd web && yarn && yarn build
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: wol-web
path: web/dist
if-no-files-found: error
build-server:
strategy:
fail-fast: false
matrix:
settings:
- target: x86_64-apple-darwin
host: macos-latest
use-cross: false
post-build: |
mkdir build && \
cp target/x86_64-apple-darwin/release/wol build/wol && \
strip -x build/wol
- target: aarch64-apple-darwin
host: macos-latest
use-cross: false
post-build: |
mkdir build && \
cp target/aarch64-apple-darwin/release/wol build/wol && \
strip -x build/wol
- target: x86_64-pc-windows-msvc
host: windows-latest
use-cross: false
post-build: |
md build
copy target/x86_64-pc-windows-msvc/release/wol.exe build/wol.exe
# crate ring not support: https://github.com/briansmith/ring/issues/1167
# - target: aarch64-pc-windows-msvc
# host: windows-latest
# use-cross: false
# post-build: |
# md build
# copy target/aarch64-pc-windows-msvc/release/wol.exe build/wol.exe

- target: x86_64-unknown-linux-gnu
host: ubuntu-latest
use-cross: true
post-build: |
mkdir build && \
cp target/x86_64-unknown-linux-gnu/release/wol build/wol && \
strip -x build/wol
- target: x86_64-unknown-linux-musl
host: ubuntu-latest
use-cross: true
post-build: |
mkdir build && \
cp target/x86_64-unknown-linux-musl/release/wol build/wol && \
strip -x build/wol
- target: aarch64-unknown-linux-gnu
host: ubuntu-latest
use-cross: true
post-build: |
mkdir build && \
cp target/aarch64-unknown-linux-gnu/release/wol build/wol
- target: aarch64-unknown-linux-musl
host: ubuntu-latest
use-cross: true
post-build: |
mkdir build && \
cp target/aarch64-unknown-linux-musl/release/wol build/wol
name: build ${{ matrix.settings.target }}
runs-on: ${{ matrix.settings.host }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Cache cargo
uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-${{ matrix.settings.target }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Install rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.settings.target }}
override: true
- name: Build
uses: actions-rs/cargo@v1
with:
use-cross: ${{ matrix.settings.use-cross }}
command: build
args: --release --target ${{ matrix.settings.target }}
- name: Post build
run: ${{ matrix.settings.post-build }}

- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: wol-${{ matrix.settings.target }}
path: build/
if-no-files-found: error

publish:
runs-on: ubuntu-latest
needs:
- build-web
- build-server
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Download all artifact
uses: actions/download-artifact@v3
with:
path: artifacts
- name: Display structure of downloaded files
run: ls -R
working-directory: artifacts
- name: Pack as zip file
run: ls | xargs -I filename zip -r -m filename.zip filename
working-directory: artifacts
- name: Display structure of zip files
run: ls -R
working-directory: artifacts
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: artifacts/*.zip

docker:
runs-on: ubuntu-latest
needs:
- build-web
- build-server
- publish
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup QEMU
uses: docker/setup-qemu-action@v2
# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup docker buildx
uses: docker/setup-buildx-action@v2

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Login to registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker images
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ env.REGISTRY }}/nashaofu/wol:latest
cache-from: type=gha
cache-to: type=gha,mode=max
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

wol.yaml
wol.yml
21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "wol"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-files = "0.6.2"
actix-web = "4.3.1"
actix-web-httpauth = "0.8.0"
anyhow = "1.0.71"
clap = { version = "4.3.1", features = ["derive"] }
config = "0.13.3"
dotenv = "0.15.0"
env_logger = "0.10.0"
lazy_static = "1.4.0"
log = "0.4.18"
serde = "1.0.163"
serde_yaml = "0.9.21"
surge-ping = "0.8.0"
2 changes: 2 additions & 0 deletions Cross.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.x86_64-unknown-linux-gnu]
image = "rust:latest"
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM --platform=amd64 alpine:latest as builder

WORKDIR /build

COPY build-docker.sh .

ARG TARGETARCH

RUN chmod +x ./build-docker.sh
RUN ./build-docker.sh

FROM alpine:latest

WORKDIR /opt/wol

COPY --from=builder /build/wol .

EXPOSE 3000
VOLUME ["/opt/wol/data"]

ENV RUST_LOG=info \
RUST_BACKTRACE=1 \
DATA_DIR=/opt/wol/data

CMD ["/opt/wol/wol"]
28 changes: 28 additions & 0 deletions build-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env ash

# https://stackoverflow.com/a/9893727
set -e

case $TARGETARCH in
"amd64")
target="x86_64-unknown-linux-musl"
;;
"arm64")
target="aarch64-unknown-linux-musl"
;;
*)
echo "unsupported architecture"
exit 1
;;
esac

mkdir wol

wget https://github.com/nashaofu/wol/releases/latest/download/wol-${target}.zip
unzip wol-${target}.zip
mv wol-${target}/wol wol/wol
chmod +x wol/wol

wget https://github.com/nashaofu/wol/releases/latest/download/wol-web.zip
unzip wol-web.zip
mv wol-web wol/www
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tab_spaces = 2
edition = "2021"
46 changes: 46 additions & 0 deletions src/api/device.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{
errors::Result,
settings::{Device, SETTINGS},
};

use actix_web::{get, post, web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use surge_ping;

#[get("/all")]
async fn all() -> Result<impl Responder> {
Ok(HttpResponse::Ok().json(&SETTINGS.read()?.devices))
}

#[post("/save")]
async fn save(data: web::Json<Vec<Device>>) -> Result<impl Responder> {
let settings = &mut SETTINGS.write()?;
settings.devices = data.clone();
settings.save()?;

Ok(HttpResponse::Ok().json(&settings.devices))
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PingData {
ip: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum DeviceStatus {
Online,
Offline,
}

#[get("/status/{ip}")]
async fn status(ip: web::Path<String>) -> Result<impl Responder> {
let payload = [0; 8];
let device = ip.parse()?;

let device_status = surge_ping::ping(device, &payload)
.await
.map(|_| DeviceStatus::Online)
.unwrap_or(DeviceStatus::Offline);

Ok(HttpResponse::Ok().json(device_status))
}
15 changes: 15 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mod device;
mod wol;

use actix_web::web;

pub fn init(cfg: &mut web::ServiceConfig) {
cfg
.service(
web::scope("/device")
.service(device::all)
.service(device::save)
.service(device::status)
)
.service(web::scope("/wol").service(wol::wake));
}
9 changes: 9 additions & 0 deletions src/api/wol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::{errors::Result, wol};

use actix_web::{post, web, HttpResponse, Responder};

#[post("/wake")]
async fn wake(data: web::Json<wol::WakeData>) -> Result<impl Responder> {
wol::wake(&data)?;
Ok(HttpResponse::Ok().json(&data))
}
18 changes: 18 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use clap::Parser;
use lazy_static::lazy_static;

lazy_static! {
pub static ref ARGS: Args = Args::parse();
}

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
/// App listen port
#[arg(short, long, default_value_t = 3000)]
pub port: u16,

/// Config file path
#[arg(short, long, default_value_t = String::from("./wol.yaml"))]
pub config: String,
}
Loading

0 comments on commit 2f152cf

Please sign in to comment.