-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
97 lines (77 loc) · 1.93 KB
/
server.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
94
95
96
97
package webpagego
import (
"log"
"net/http"
"os"
"time"
"github.com/Masterminds/sprig"
"github.com/avu12/golangwebpage/webpagego/internal/dailymail"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
_ "github.com/lib/pq"
)
var (
router *gin.Engine
optp sessions.Options
)
func init() {
router = gin.Default()
router.SetFuncMap(sprig.FuncMap())
router.LoadHTMLGlob("static/*")
opt, err := redis.ParseURL(os.Getenv("REDIS_URL"))
if err != nil {
log.Println(err)
}
store, err := sessions.NewRedisStore(10, "tcp", opt.Addr, opt.Password)
if err != nil {
log.Println("Problem with redis store in init", err)
}
optp.Path = "/"
optp.HttpOnly = true
optp.Secure = true
optp.MaxAge = 3600 * 4
store.Options(optp)
router.Use(sessions.Sessions("mysession", store))
log.Println("No problem with redis store in init")
}
//StartApp is the main entrypoint to the app
func StartApp() {
//Mapping urls with handlers
mapUrls()
//sending email every day
go dailymail.SendDailyMail()
//for infinite running:
go NoSleep()
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
router.Use(static.Serve("/", static.LocalFile("./static", true)))
err := router.Run(":" + port)
if err != nil {
panic(err)
}
/* FOR LOCAL: err := router.Run(":3000")
if err != nil {
panic(err)
}*/
}
//NoSleep is to prevent Heroku to stop the app automatically, only needed if Free dyno used.
func NoSleep() {
for range time.NewTicker(time.Minute * 15).C {
log.Println(time.Now(), "I am a still not sleeping dyno!")
req, err := http.NewRequest("GET", "https://golangwebpagev2.herokuapp.com/", nil)
if err != nil {
log.Println("ERROR in self request!")
}
client := http.Client{}
resp, err := client.Do(req)
//log.Println("RESP:", resp)
if err != nil {
log.Println("ERROR in self resp!")
}
resp.Body.Close()
}
}