系统平台信息加载、配置文件路径加载

This commit is contained in:
邵静 2024-09-21 18:16:51 +08:00
parent 9b44a57f50
commit 6168677c2b
8 changed files with 227 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
zig-cache
zig-out

52
build.zig Normal file
View File

@ -0,0 +1,52 @@
const std = @import("std");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "igs",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("igs", "Run the app");
run_step.dependOn(&run_cmd.step);
}

14
build.zig.zon Normal file
View File

@ -0,0 +1,14 @@
.{
.name = "IntraGate",
.version = "0.0.0",
.dependencies = .{},
.paths = .{
"",
//"build.zig",
//"build.zig.zon",
//"src",
//"LICENSE",
//"README.md",
},
}

17
doc/igs_dev.md Normal file
View File

@ -0,0 +1,17 @@
# 服务端架构设计(igs)
## 系统概述
服务端(igs)负责管理公网端口映射,处理来自客户端(igc)与用户的连接和请求。
## 组件设计
### 端口管理器
* 功能
负责公网端口的分配与回收,维护端口映射表。
* 实现

26
src/config_manager.zig Normal file
View File

@ -0,0 +1,26 @@
const std = @import("std");
const Lua = @import("utils/lua_util.zig").Lua;
const Platform = @import("utils/platform_util.zig");
const Allocator = std.mem.Allocator;
pub const ConfigOption = struct { url: []const u8 = "intra.lua" };
/// -
pub const ConfigManager = struct {
allocator: *const Allocator,
opt: ConfigOption,
///
pub fn init(allocator: *const Allocator, opt: ConfigOption) ConfigManager {
return ConfigManager{ .allocator = allocator, .opt = opt };
}
///
pub fn load_config(self: *ConfigManager) !void {
const platform = Platform.getPlatformInfo();
const concat_path = try std.mem.concat(self.allocator.*, u8, &[2][]const u8{ platform.home, self.opt.url });
defer self.allocator.free(concat_path);
self.opt.url = concat_path;
std.debug.print("load config from {s}\n", .{self.opt.url});
}
};

12
src/main.zig Normal file
View File

@ -0,0 +1,12 @@
const std = @import("std");
const config = @import("config_manager.zig");
pub fn main() !void {
//
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
//
var config_manager = config.ConfigManager.init(&allocator, .{});
try config_manager.load_config();
}

58
src/utils/lua_util.zig Normal file
View File

@ -0,0 +1,58 @@
const std = @import("std");
const ziglua = @import("ziglua");
const zLua = ziglua.Lua;
const LuaType = ziglua.LuaType;
const Allocator = std.mem.Allocator;
pub const Lua = struct {
allocator: *const Allocator,
url: [:0]const u8,
lua: *ziglua.Lua,
/// Lua
pub fn init(allocator: *const Allocator, url: [:0]const u8) !Lua {
const lua = try ziglua.Lua.init(allocator);
return Lua{
.allocator = allocator,
.url = url,
.lua = lua,
};
}
///
pub fn deinit(self: *Lua) void {
self.allocator.free(self.url);
self.lua.deinit();
}
///
pub fn loadScript(self: *Lua) !void {
try self.lua.loadFile(self.url, .text);
try self.lua.protectedCall(0, 0, 0);
}
///
pub fn getGlobalValue(self: *Lua, comptime T: type, index: i32, var_name: []const u8) !void {
const fmt_var_name = try std.fmt.allocPrintZ(self.allocator.*, "{s}", .{var_name});
//
const v_type = try self.lua.getGlobal(fmt_var_name);
defer {
_ = self.allocator.free(fmt_var_name);
_ = self.lua.pop(1);
}
//
if (v_type == LuaType.table) {
std.debug.print("是表\n", .{});
} else {
const a = try self.getValue(T, index);
std.debug.print("{any}\n", .{a});
}
}
fn getValue(self: *Lua, comptime T: type, index: i32) !T {
return switch (T) {
i64 => try self.lua.toInteger(index),
else => unreachable,
};
}
};

View File

@ -0,0 +1,46 @@
const std = @import("std");
const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
const Os = enum { windows, linux, other, unknown };
const Platform = struct {
os: Os = undefined,
home: []const u8 = undefined,
};
var platform_info: ?Platform = null;
pub fn getPlatformInfo() Platform {
if (platform_info) |info| {
return info;
} else {
platform_info = initPlatformInfo() catch |err| {
std.debug.print("获取系统信息失败:{any}\n", .{err});
return Platform{ .os = .unknown };
};
return platform_info.?;
}
}
fn initPlatformInfo() !Platform {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const env_map = try std.process.getEnvMap(allocator);
var user_profile: []const u8 = undefined;
var os: Os = undefined;
if (builtin.os.tag == .windows) {
user_profile = env_map.get("USERPROFILE").?;
os = .windows;
} else if (builtin.os.tag == .linux) {
user_profile = env_map.get("HOME").?;
os = .linux;
} else {
user_profile = env_map.get("HOME").?;
os = .other;
}
return Platform{
.os = os,
.home = user_profile,
};
}