-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
171 lines (148 loc) · 3.71 KB
/
main.go
File metadata and controls
171 lines (148 loc) · 3.71 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"internal/rapidstocks"
"internal/stockyboiapi"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/robfig/cron"
)
// Stores tickers that will be tracked daily
var tickers []string
type AddTickerReq struct {
Text string `form:"text" binding:"required"`
ResponseUrl string `form:"response_url" binding:"required"`
}
// Init the .env file if not running in production
func init() {
env := os.Getenv("ENVIRONMENT")
if env != "production" && env != "docker" {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
}
}
func main() {
rapidstocks.Configure(
os.Getenv("RAPID_STOCKS_URL"),
os.Getenv("RAPID_STOCKS_TOKEN"),
)
stockyboiapi.Configure(
os.Getenv("SLACK_API_KEY"),
os.Getenv("SLACK_API_ENDPOINT"),
os.Getenv("SLACK_API_CHANNEL"),
)
StartCron()
StartGin()
}
func StartCron() {
asxTimeZone := os.Getenv("TIMEZONE")
timeZone, err := time.LoadLocation(asxTimeZone)
if err != nil {
log.Fatal(err)
}
openingJob := cron.NewWithLocation(timeZone)
openingJob.AddFunc("0 5 10 * * *", func() {
cronStockSummary(asxTimeZone)
})
middayJob := cron.NewWithLocation(timeZone)
middayJob.AddFunc("0 0 13 * * *", func() {
cronStockSummary(asxTimeZone)
})
closingJob := cron.NewWithLocation(timeZone)
closingJob.AddFunc("0 50 15 * * *", func() {
cronStockSummary(asxTimeZone)
})
openingJob.Start()
middayJob.Start()
closingJob.Start()
}
func StartGin() {
port := os.Getenv("PORT")
env := os.Getenv("ENVIRONMENT")
var address string
if env == "" {
log.Fatal("$ENVIRONMENT must be set")
}
if env == "production" || env == "development" || env == "docker" {
if env == "production" {
if port == "" {
log.Fatal("$PORT must be set")
}
address = ":" + port
} else {
address = ":8080"
}
} else {
log.Fatal("$ENVIRONMENT not recognised")
}
router := gin.Default()
router.SetTrustedProxies(nil)
router.GET("/ping", respondPong)
router.POST("/showSummary", showSummary)
router.POST("/showTickers", showTickers)
router.POST("/addTicker", addTicker)
router.Run(address)
}
// respondPong is used to run uptime checks and
// definitely not to keep this app running on a heroku
func respondPong(c *gin.Context) {
c.String(http.StatusOK, "pong")
}
// Gets details on current tickers and posts them
// to slack.
func stockSummary(timezone string) {
if len(tickers) > 0 {
quotes := rapidstocks.GetStocks(tickers)
formattedQuoteBlocks := stockyboiapi.FormatQuotes(quotes, timezone)
jsonResp, _ := json.Marshal(formattedQuoteBlocks)
stockyboiapi.PostToSlack("", jsonResp)
} else {
stockyboiapi.SlackPostText(
"You're not currently tracking any tickers",
"",
)
}
}
// Simple slash command support for showing current tickers
func showSummary(c *gin.Context) {
stockSummary(os.Getenv("TIMEZONE"))
}
// Automated cron job for posting ticker details
func cronStockSummary(timezone string) {
if len(tickers) > 0 {
time.LoadLocation(timezone)
day := time.Now().Weekday()
// Sunday = 0, Saturday = 6
if day != 0 && day != 6 {
stockSummary(timezone)
}
}
}
// Returns all tickers currently registered for the session.
func showTickers(c *gin.Context) {
stockyboiapi.SlashCommandShowTickers(tickers)
c.String(http.StatusOK, "Tickers Sent")
}
// First checks the ticker against the API then stores it.
func addTicker(c *gin.Context) {
var request AddTickerReq
err := c.ShouldBind(&request)
if err != nil {
log.Fatal(err)
}
valid := rapidstocks.ValidateTicker(request.Text)
fmt.Println(valid)
if !valid {
c.String(http.StatusOK, "Ticker not valid")
return
}
tickers = append(tickers, request.Text)
c.String(http.StatusOK, "Ticker added")
}