Skip to content

Commit

Permalink
Feat: support no web service (jeessy2#405)
Browse files Browse the repository at this point in the history
* feat: support to set flag for not starting web service

* docs: support Google Domains and `-noweb` flag
  • Loading branch information
yin1999 authored Oct 11, 2022
1 parent df79284 commit 40910a9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
## 特性

- 支持Mac、Windows、Linux系统,支持ARM、x86架构
- 支持的域名服务商 `Alidns(阿里云)` `Dnspod(腾讯云)` `Cloudflare` `华为云` `Callback` `百度云` `porkbun` `GoDaddy`
- 支持的域名服务商 `Alidns(阿里云)` `Dnspod(腾讯云)` `Cloudflare` `华为云` `Callback` `百度云` `porkbun` `GoDaddy` `Google Domains`
- 支持接口/网卡获取IP
- 支持以服务的方式运行
- 默认间隔5分钟同步一次
Expand All @@ -40,11 +40,11 @@
- [可选] 安装服务
- Mac/Linux: `sudo ./ddns-go -s install`
- Win(以管理员打开cmd): `.\ddns-go.exe -s install`
- 安装服务也支持 `-l`监听地址 `-f`同步间隔时间(秒) `-c`自定义配置文件路径
- 安装服务也支持 `-l`监听地址 `-f`同步间隔时间(秒) `-c`自定义配置文件路径 `-noweb`不启动web服务
- [可选] 服务卸载
- Mac/Linux: `sudo ./ddns-go -s uninstall`
- Win(以管理员打开cmd): `.\ddns-go.exe -s uninstall`
- [可选] 支持启动带参数 `-l`监听地址 `-f`同步间隔时间(秒) `-c`自定义配置文件路径。如:`./ddns-go -l 127.0.0.1:9876 -f 600 -c /Users/name/ddns-go.yaml`
- [可选] 支持启动带参数 `-l`监听地址 `-f`同步间隔时间(秒) `-c`自定义配置文件路径 `-noweb`不启动web服务。如:`./ddns-go -l 127.0.0.1:9876 -f 600 -c /Users/name/ddns-go.yaml`

## Docker中使用

Expand Down
39 changes: 30 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ var serviceType = flag.String("s", "", "服务管理, 支持install, uninstall")
// 配置文件路径
var configFilePath = flag.String("c", util.GetConfigFilePathDefault(), "自定义配置文件路径")

// Web 服务
var noWebService = flag.Bool("noweb", false, "不启动 web 服务")

//go:embed static
var staticEmbededFiles embed.FS

Expand Down Expand Up @@ -80,6 +83,23 @@ func main() {
}

func run(firstDelay time.Duration) {
if !*noWebService {
go func() {
// 启动web服务
err := runWebServer()
if err != nil {
log.Println(err)
time.Sleep(time.Minute)
os.Exit(1)
}
}()
}

// 定时运行
dns.RunTimer(firstDelay, time.Duration(*every)*time.Second)
}

func runWebServer() error {
// 启动静态文件服务
http.Handle("/static/", http.FileServer(http.FS(staticEmbededFiles)))
http.Handle("/favicon.ico", http.FileServer(http.FS(faviconEmbededFile)))
Expand All @@ -94,18 +114,15 @@ func run(firstDelay time.Duration) {

log.Println("监听", *listen, "...")

l, err := net.Listen("tcp", *listen)
if err != nil {
return fmt.Errorf("监听端口发生异常, 请检查端口是否被占用: %w", err)
}

// 没有配置, 自动打开浏览器
autoOpenExplorer()

// 定时运行
go dns.RunTimer(firstDelay, time.Duration(*every)*time.Second)
err := http.ListenAndServe(*listen, nil)

if err != nil {
log.Println("启动端口发生异常, 请检查端口是否被占用", err)
time.Sleep(time.Minute)
os.Exit(1)
}
return http.Serve(l, nil)
}

type program struct{}
Expand Down Expand Up @@ -138,6 +155,10 @@ func getService() service.Service {
Option: options,
}

if *noWebService {
svcConfig.Arguments = append(svcConfig.Arguments, "-noweb")
}

prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
Expand Down

0 comments on commit 40910a9

Please sign in to comment.