main.go 1019 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "backup-x/client"
  4. "backup-x/web"
  5. "embed"
  6. "os"
  7. "log"
  8. "net/http"
  9. "time"
  10. )
  11. var defaultPort = "9977"
  12. //go:embed static
  13. var staticEmbededFiles embed.FS
  14. //go:embed favicon.ico
  15. var faviconEmbededFile embed.FS
  16. func main() {
  17. // 启动静态文件服务
  18. http.Handle("/static/", http.FileServer(http.FS(staticEmbededFiles)))
  19. http.Handle("/favicon.ico", http.FileServer(http.FS(faviconEmbededFile)))
  20. http.HandleFunc("/", web.BasicAuth(web.WritingConfig))
  21. http.HandleFunc("/save", web.BasicAuth(web.Save))
  22. http.HandleFunc("/logs", web.BasicAuth(web.Logs))
  23. http.HandleFunc("/clearLog", web.BasicAuth(web.ClearLog))
  24. http.HandleFunc("/webhookTest", web.BasicAuth(web.WebhookTest))
  25. // 运行
  26. go client.DeleteOldBackup()
  27. go client.RunLoop()
  28. if os.Getenv("port") != "" {
  29. defaultPort = os.Getenv("port")
  30. }
  31. err := http.ListenAndServe(":"+defaultPort, nil)
  32. if err != nil {
  33. log.Println("启动端口发生异常, 请检查端口是否被占用", err)
  34. time.Sleep(time.Minute)
  35. }
  36. }