Skip to content

Commit

Permalink
增加日志
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiwy committed Dec 6, 2019
1 parent 24d7b0b commit a8da7ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
14 changes: 3 additions & 11 deletions src/contoller/controler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ const (
day = 24 * hour
)

var begin time.Time

func init() {
begin = time.Now()
}
var begin = time.Now()

func Uptime(c *gin.Context) {
diff := time.Now().Unix() - begin.Unix()
Expand All @@ -30,12 +26,8 @@ func Uptime(c *gin.Context) {

c.String(http.StatusOK,
fmt.Sprintf("The system launched in %s. already running for %d days, %d hours, %d minues, %d seconds.",
begin.Format("2006-01-02 15:04:05"),
days,
hours,
minutes,
seconds,
))
begin.Format("2006-01-02 15:04:05"), days, hours, minutes, seconds),
)
}

func Ping(c *gin.Context) {
Expand Down
22 changes: 19 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
Expand All @@ -11,6 +12,7 @@ import (
"oktools/src/contoller"
"oktools/src/middleware"
"os"
"time"
)

func main() {
Expand Down Expand Up @@ -53,8 +55,11 @@ func main() {
}

r := initRouter()

var err error
if gin.Mode() == gin.ReleaseMode {
serverChan("Oktools server start")

runNoTLS()
err = r.RunTLS(":"+conf.Conf.Http.Port, conf.Conf.Http.SSL.Crt, conf.Conf.Http.SSL.Key)
} else {
Expand All @@ -63,14 +68,25 @@ func main() {

if err != nil {
if gin.Mode() == gin.ReleaseMode {
_, _ = http.Get(fmt.Sprintf("https://sc.ftqq.com/%s.send?text=%s",
conf.Conf.ThirdParty.ServerChan.Key, url.QueryEscape("主人服务器又挂掉啦~")))
serverChan("Oktools server shutdown")
}
log.Println("Something terrible happened:", err)
panic(err)
}
}

func serverChan(msg string) {
key := conf.Conf.ThirdParty.ServerChan.Key
msg += time.Now().Format(" - 2006-01-02 15:04:05")

resp, err := http.Get(fmt.Sprintf("https://sc.ftqq.com/%s.send?text=%s", key, url.QueryEscape(msg)))
if err == nil {
bytes, _ := ioutil.ReadAll(resp.Body)
log.Println("ServerChan resp:", string(bytes))
} else {
log.Println("ServerChan error:", err)
}
}

func runNoTLS() {
go func() {
e := gin.Default()
Expand Down

0 comments on commit a8da7ea

Please sign in to comment.