GoScrobble/cmd/go-scrobble/main.go

84 lines
1.7 KiB
Go
Raw Normal View History

2021-03-23 08:43:44 +00:00
package main
import (
"fmt"
2021-03-25 05:15:01 +00:00
"log"
"os"
2021-03-25 23:21:28 +00:00
"strconv"
"strings"
"time"
2021-03-25 05:15:01 +00:00
"github.com/joho/godotenv"
2021-03-28 09:07:11 +00:00
"gitlab.com/idanoo/go-scrobble/internal/goscrobble"
2021-03-23 08:43:44 +00:00
)
func init() {
// Set UTC for errything
os.Setenv("TZ", "Etc/UTC")
}
2021-03-23 08:43:44 +00:00
func main() {
fmt.Println("Starting goscrobble")
2021-03-25 05:15:01 +00:00
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// Store JWT secret
goscrobble.JwtToken = []byte(os.Getenv("JWT_SECRET"))
2021-03-25 23:21:28 +00:00
// Store JWT expiry
goscrobble.JwtExpiry = 86400
jwtExpiryStr := os.Getenv("JWT_EXPIRY")
if jwtExpiryStr != "" {
i, err := strconv.ParseFloat(jwtExpiryStr, 64)
if err != nil {
panic("Invalid JWT_EXPIRY value")
}
goscrobble.JwtExpiry = time.Duration(i) * time.Second
}
// Store Refresh expiry
goscrobble.RefereshExpiry = (86400 * 7)
refreshExpiryStr := os.Getenv("REFRESH_EXPIRY")
if refreshExpiryStr != "" {
i, err := strconv.ParseFloat(refreshExpiryStr, 64)
if err != nil {
panic("Invalid REFRESH_EXPIRY value")
}
goscrobble.RefereshExpiry = time.Duration(i) * time.Second
}
goscrobble.StaticDirectory = "web"
staticDirectoryStr := os.Getenv("STATIC_DIR")
if staticDirectoryStr != "" {
goscrobble.StaticDirectory = staticDirectoryStr
}
2021-03-25 23:21:28 +00:00
// Ignore reverse proxies
goscrobble.ReverseProxies = strings.Split(os.Getenv("REVERSE_PROXIES"), ",")
// Store port
port := os.Getenv("PORT")
if port == "" {
port = "42069"
}
// Boot up DB connection
2021-03-24 09:28:05 +00:00
goscrobble.InitDb()
defer goscrobble.CloseDbConn()
2021-03-23 08:43:44 +00:00
// Boot up Redis connection
goscrobble.InitRedis()
defer goscrobble.CloseRedisConn()
// Start background workers
2021-08-12 09:23:48 +00:00
go goscrobble.StartBackgroundWorkers()
defer goscrobble.EndBackgroundWorkers()
2021-03-23 08:43:44 +00:00
// Boot up API webserver \o/
goscrobble.HandleRequests(port)
2021-03-23 08:43:44 +00:00
}