-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
93 lines (71 loc) · 2.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"flag"
"log"
"net/http"
"os"
"time"
"github.com/gin-gonic/contrib/cors"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"github.com/jasonlvhit/gocron"
"github.com/tonyalaribe/monitor-server/config"
"github.com/tonyalaribe/monitor-server/constants"
"github.com/tonyalaribe/monitor-server/logger"
"github.com/tonyalaribe/monitor-server/models"
"github.com/tonyalaribe/monitor-server/resources"
)
func GetMainEngine() *gin.Engine {
// set Production mode if necessary
if config.Get().IsProduction {
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
//router.Use(ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true))
router.Use(cors.New(cors.Config{
AllowedOrigins: config.Get().Cors.AllowedOrigins,
AllowedMethods: []string{"GET", "POST", "PUT", "PATCH", "HEAD", "OPTIONS", "DELETE"},
AllowedHeaders: []string{constants.CONTENT_TYPE, "X-AUTH-TOKEN"},
AllowCredentials: true,
MaxAge: 24 * time.Hour,
}))
router.Use(static.Serve("/", static.LocalFile("./client", false)))
//router.StaticFile("/", "./client/index.html")
//r.Use(static.Serve("/", static.LocalFile("./build", false)))
router.GET("/ping", func(c *gin.Context) {
c.Status(200)
})
router.GET("/gen4yesterday", func(c *gin.Context) {
models.DoArchiveForYesterdayWrapper()
})
router.Static("/static", "./static")
api := router.Group("/api")
resources.Register("screenshots", resources.Screenshots{}, api)
resources.Register("users", resources.Users{}, api)
resources.Register("timelog", resources.Timelog{}, api)
resources.Register("archive", resources.Archive{}, api)
//resources.Register("/v0.1/login", resources.Users{}, api)
router.POST("/api/v0.1/add", resources.Screenshots{}.Post)
router.GET("/api/v0.1/login", resources.Users{}.Post)
//resources.Register("/advert", resources.Advert{}, apiAuth)
router.NoRoute(func(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "./client/index.html")
c.Abort()
})
return router
}
func main() {
configFile := flag.String("config", "./config.toml", "path to config file")
flag.Parse()
config.Init(*configFile)
logger.Init()
PORT := os.Getenv("PORT")
if PORT == "" {
log.Println("No Global port has been defined, using default")
PORT = config.Get().Port
}
gocron.Every(1).Day().At("10:30").Do(models.DoArchiveForYesterdayWrapper)
gocron.Every(1).Day().At("16:30").Do(models.DoArchiveForYesterdayWrapper)
gocron.Start()
GetMainEngine().Run(":" + PORT)
}