Skip to content

Commit 21c6f40

Browse files
committed
use gocron for scheduling auto-cert job
Improved error handling
1 parent 1803f13 commit 21c6f40

File tree

6 files changed

+257
-216
lines changed

6 files changed

+257
-216
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/gin-contrib/static v0.0.1
88
github.com/gin-gonic/gin v1.7.4
99
github.com/go-acme/lego/v4 v4.4.0
10+
github.com/go-co-op/gocron v1.15.0
1011
github.com/go-playground/locales v0.13.0
1112
github.com/go-playground/universal-translator v0.17.0
1213
github.com/go-playground/validator/v10 v10.4.1
@@ -38,10 +39,12 @@ require (
3839
github.com/miekg/dns v1.1.40 // indirect
3940
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4041
github.com/modern-go/reflect2 v1.0.1 // indirect
42+
github.com/robfig/cron/v3 v3.0.1 // indirect
4143
github.com/tklauser/go-sysconf v0.3.7 // indirect
4244
github.com/tklauser/numcpus v0.2.3 // indirect
4345
github.com/ugorji/go/codec v1.1.7 // indirect
4446
golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d // indirect
47+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
4548
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
4649
golang.org/x/text v0.3.4 // indirect
4750
gopkg.in/square/go-jose.v2 v2.5.1 // indirect

go.sum

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ github.com/go-acme/lego/v4 v4.4.0 h1:uHhU5LpOYQOdp3aDU+XY2bajseu8fuExphTL1Ss6/Fc
115115
github.com/go-acme/lego/v4 v4.4.0/go.mod h1:l3+tFUFZb590dWcqhWZegynUthtaHJbG2fevUpoOOE0=
116116
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
117117
github.com/go-cmd/cmd v1.0.5/go.mod h1:y8q8qlK5wQibcw63djSl/ntiHUHXHGdCkPk0j4QeW4s=
118+
github.com/go-co-op/gocron v1.15.0 h1:XmiPazahD9aq0/QdK5toCVHfgTXfrZ/s83RpAgzr6SM=
119+
github.com/go-co-op/gocron v1.15.0/go.mod h1:On9zUZTv7FBeuj9D/cdYyAWcPUiLqqAx7nsPHd0EmKM=
118120
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
119121
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
120122
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -366,6 +368,8 @@ github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDa
366368
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
367369
github.com/rainycape/memcache v0.0.0-20150622160815-1031fa0ce2f2/go.mod h1:7tZKcyumwBO6qip7RNQ5r77yrssm9bfCowcLEBcU5IA=
368370
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
371+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
372+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
369373
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
370374
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
371375
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -406,13 +410,16 @@ github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
406410
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
407411
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
408412
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
413+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
409414
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
410415
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
411416
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
412417
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
413418
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
414-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
415419
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
420+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
421+
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
422+
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
416423
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
417424
github.com/tklauser/go-sysconf v0.3.7 h1:HT7h4+536gjqeq1ZIJPgOl1rg1XFatQGVZWp7Py53eg=
418425
github.com/tklauser/go-sysconf v0.3.7/go.mod h1:JZIdXh4RmBvZDBZ41ld2bGxRV3n4daiiqA3skYhAoQ4=
@@ -534,8 +541,9 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ
534541
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
535542
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
536543
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
537-
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
538544
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
545+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
546+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
539547
golang.org/x/sys v0.0.0-20180622082034-63fc586f45fe/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
540548
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
541549
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -707,8 +715,9 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
707715
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
708716
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
709717
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
710-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
711718
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
719+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
720+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
712721
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
713722
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
714723
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=

main.go

Lines changed: 76 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,84 @@
11
package main
22

33
import (
4-
"context"
5-
"flag"
6-
"github.com/0xJacky/Nginx-UI/server/analytic"
7-
"github.com/0xJacky/Nginx-UI/server/model"
8-
"github.com/0xJacky/Nginx-UI/server/router"
9-
"github.com/0xJacky/Nginx-UI/server/settings"
10-
"github.com/0xJacky/Nginx-UI/server/tool"
11-
"github.com/0xJacky/Nginx-UI/server/tool/nginx"
12-
"github.com/gin-gonic/gin"
13-
"log"
14-
"mime"
15-
"net/http"
16-
"os/signal"
17-
"syscall"
18-
"time"
4+
"context"
5+
"flag"
6+
"github.com/0xJacky/Nginx-UI/server/analytic"
7+
"github.com/0xJacky/Nginx-UI/server/model"
8+
"github.com/0xJacky/Nginx-UI/server/router"
9+
"github.com/0xJacky/Nginx-UI/server/settings"
10+
"github.com/0xJacky/Nginx-UI/server/tool"
11+
"github.com/0xJacky/Nginx-UI/server/tool/nginx"
12+
"github.com/gin-gonic/gin"
13+
"github.com/go-co-op/gocron"
14+
"log"
15+
"mime"
16+
"net/http"
17+
"os/signal"
18+
"syscall"
19+
"time"
1920
)
2021

2122
func main() {
22-
// Create context that listens for the interrupt signal from the OS.
23-
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
24-
defer stop()
25-
26-
// Hack: fix wrong Content Type of .js file on some OS platforms
27-
// See https://github.com/golang/go/issues/32350
28-
_ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8")
29-
30-
var confPath string
31-
flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file")
32-
flag.Parse()
33-
34-
gin.SetMode(settings.ServerSettings.RunMode)
35-
36-
settings.Init(confPath)
37-
log.Printf("nginx config dir path: %s", nginx.GetNginxConfPath(""))
38-
if "" != settings.ServerSettings.JwtSecret {
39-
model.Init()
40-
go tool.AutoCert()
41-
go analytic.RecordServerAnalytic()
42-
}
43-
44-
srv := &http.Server{
45-
Addr: ":" + settings.ServerSettings.HttpPort,
46-
Handler: router.InitRouter(),
47-
}
48-
49-
// Initializing the server in a goroutine so that
50-
// it won't block the graceful shutdown handling below
51-
go func() {
52-
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
53-
log.Fatalf("listen: %s\n", err)
54-
}
55-
}()
56-
57-
// Listen for the interrupt signal.
58-
<-ctx.Done()
59-
60-
// Restore default behavior on the interrupt signal and notify user of shutdown.
61-
stop()
62-
log.Println("shutting down gracefully, press Ctrl+C again to force")
63-
64-
// The context is used to inform the server it has 5 seconds to finish
65-
// the request it is currently handling
66-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
67-
defer cancel()
68-
if err := srv.Shutdown(ctx); err != nil {
69-
log.Fatal("Server forced to shutdown: ", err)
70-
}
71-
72-
log.Println("Server exiting")
23+
// Create context that listens for the interrupt signal from the OS.
24+
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
25+
defer stop()
26+
27+
// Hack: fix wrong Content Type of .js file on some OS platforms
28+
// See https://github.com/golang/go/issues/32350
29+
_ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8")
30+
31+
var confPath string
32+
flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file")
33+
flag.Parse()
34+
35+
gin.SetMode(settings.ServerSettings.RunMode)
36+
37+
settings.Init(confPath)
38+
log.Printf("nginx config dir path: %s", nginx.GetNginxConfPath(""))
39+
if "" != settings.ServerSettings.JwtSecret {
40+
model.Init()
41+
42+
s := gocron.NewScheduler(time.UTC)
43+
job, err := s.Every(1).Hour().SingletonMode().Do(tool.AutoCert)
44+
45+
if err != nil {
46+
log.Fatalf("AutoCert Job: %v, Err: %v\n", job, err)
47+
}
48+
49+
s.StartAsync()
50+
51+
go analytic.RecordServerAnalytic()
52+
}
53+
54+
srv := &http.Server{
55+
Addr: ":" + settings.ServerSettings.HttpPort,
56+
Handler: router.InitRouter(),
57+
}
58+
59+
// Initializing the server in a goroutine so that
60+
// it won't block the graceful shutdown handling below
61+
go func() {
62+
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
63+
log.Fatalf("listen: %s\n", err)
64+
}
65+
}()
66+
67+
// Listen for the interrupt signal.
68+
<-ctx.Done()
69+
70+
// Restore default behavior on the interrupt signal and notify user of shutdown.
71+
stop()
72+
log.Println("shutting down gracefully, press Ctrl+C again to force")
73+
74+
// The context is used to inform the server it has 5 seconds to finish
75+
// the request it is currently handling
76+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
77+
defer cancel()
78+
if err := srv.Shutdown(ctx); err != nil {
79+
log.Fatal("Server forced to shutdown: ", err)
80+
}
81+
82+
log.Println("Server exiting")
7383

7484
}

0 commit comments

Comments
 (0)