-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use gocron for scheduling auto-cert job
Improved error handling
- Loading branch information
Showing
6 changed files
with
257 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"github.com/0xJacky/Nginx-UI/server/analytic" | ||
"github.com/0xJacky/Nginx-UI/server/model" | ||
"github.com/0xJacky/Nginx-UI/server/router" | ||
"github.com/0xJacky/Nginx-UI/server/settings" | ||
"github.com/0xJacky/Nginx-UI/server/tool" | ||
"github.com/0xJacky/Nginx-UI/server/tool/nginx" | ||
"github.com/gin-gonic/gin" | ||
"log" | ||
"mime" | ||
"net/http" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
"context" | ||
"flag" | ||
"github.com/0xJacky/Nginx-UI/server/analytic" | ||
"github.com/0xJacky/Nginx-UI/server/model" | ||
"github.com/0xJacky/Nginx-UI/server/router" | ||
"github.com/0xJacky/Nginx-UI/server/settings" | ||
"github.com/0xJacky/Nginx-UI/server/tool" | ||
"github.com/0xJacky/Nginx-UI/server/tool/nginx" | ||
"github.com/gin-gonic/gin" | ||
"github.com/go-co-op/gocron" | ||
"log" | ||
"mime" | ||
"net/http" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func main() { | ||
// Create context that listens for the interrupt signal from the OS. | ||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
defer stop() | ||
|
||
// Hack: fix wrong Content Type of .js file on some OS platforms | ||
// See https://github.com/golang/go/issues/32350 | ||
_ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8") | ||
|
||
var confPath string | ||
flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file") | ||
flag.Parse() | ||
|
||
gin.SetMode(settings.ServerSettings.RunMode) | ||
|
||
settings.Init(confPath) | ||
log.Printf("nginx config dir path: %s", nginx.GetNginxConfPath("")) | ||
if "" != settings.ServerSettings.JwtSecret { | ||
model.Init() | ||
go tool.AutoCert() | ||
go analytic.RecordServerAnalytic() | ||
} | ||
|
||
srv := &http.Server{ | ||
Addr: ":" + settings.ServerSettings.HttpPort, | ||
Handler: router.InitRouter(), | ||
} | ||
|
||
// Initializing the server in a goroutine so that | ||
// it won't block the graceful shutdown handling below | ||
go func() { | ||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
log.Fatalf("listen: %s\n", err) | ||
} | ||
}() | ||
|
||
// Listen for the interrupt signal. | ||
<-ctx.Done() | ||
|
||
// Restore default behavior on the interrupt signal and notify user of shutdown. | ||
stop() | ||
log.Println("shutting down gracefully, press Ctrl+C again to force") | ||
|
||
// The context is used to inform the server it has 5 seconds to finish | ||
// the request it is currently handling | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
if err := srv.Shutdown(ctx); err != nil { | ||
log.Fatal("Server forced to shutdown: ", err) | ||
} | ||
|
||
log.Println("Server exiting") | ||
// Create context that listens for the interrupt signal from the OS. | ||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
defer stop() | ||
|
||
// Hack: fix wrong Content Type of .js file on some OS platforms | ||
// See https://github.com/golang/go/issues/32350 | ||
_ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8") | ||
|
||
var confPath string | ||
flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file") | ||
flag.Parse() | ||
|
||
gin.SetMode(settings.ServerSettings.RunMode) | ||
|
||
settings.Init(confPath) | ||
log.Printf("nginx config dir path: %s", nginx.GetNginxConfPath("")) | ||
if "" != settings.ServerSettings.JwtSecret { | ||
model.Init() | ||
|
||
s := gocron.NewScheduler(time.UTC) | ||
job, err := s.Every(1).Hour().SingletonMode().Do(tool.AutoCert) | ||
|
||
if err != nil { | ||
log.Fatalf("AutoCert Job: %v, Err: %v\n", job, err) | ||
} | ||
|
||
s.StartAsync() | ||
|
||
go analytic.RecordServerAnalytic() | ||
} | ||
|
||
srv := &http.Server{ | ||
Addr: ":" + settings.ServerSettings.HttpPort, | ||
Handler: router.InitRouter(), | ||
} | ||
|
||
// Initializing the server in a goroutine so that | ||
// it won't block the graceful shutdown handling below | ||
go func() { | ||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
log.Fatalf("listen: %s\n", err) | ||
} | ||
}() | ||
|
||
// Listen for the interrupt signal. | ||
<-ctx.Done() | ||
|
||
// Restore default behavior on the interrupt signal and notify user of shutdown. | ||
stop() | ||
log.Println("shutting down gracefully, press Ctrl+C again to force") | ||
|
||
// The context is used to inform the server it has 5 seconds to finish | ||
// the request it is currently handling | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
if err := srv.Shutdown(ctx); err != nil { | ||
log.Fatal("Server forced to shutdown: ", err) | ||
} | ||
|
||
log.Println("Server exiting") | ||
|
||
} |
Oops, something went wrong.