2021-03-23 08:43:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-27 06:33:27 +00:00
|
|
|
"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"
|
2022-04-25 02:47:15 +00:00
|
|
|
"gitlab.com/idanoo/goscrobble/internal/goscrobble"
|
2021-03-23 08:43:44 +00:00
|
|
|
)
|
|
|
|
|
2021-04-02 09:24:00 +00:00
|
|
|
func init() {
|
|
|
|
// Set UTC for errything
|
|
|
|
os.Setenv("TZ", "Etc/UTC")
|
|
|
|
}
|
|
|
|
|
2021-03-23 08:43:44 +00:00
|
|
|
func main() {
|
2021-03-27 06:33:27 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-06 08:28:04 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-12-25 00:30:55 +00:00
|
|
|
goscrobble.DataDirectory = os.Getenv("DATA_DIRECTORY")
|
|
|
|
if goscrobble.DataDirectory == "" {
|
|
|
|
panic("DATA_DIRECTORY required in .env")
|
|
|
|
}
|
|
|
|
|
|
|
|
goscrobble.FrontendDirectory = os.Getenv("FRONTEND_DIRECTORY")
|
|
|
|
if goscrobble.FrontendDirectory == "" {
|
|
|
|
panic("FRONTEND_DIRECTORY required in .env")
|
2021-04-11 08:10:52 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 02:38:42 +00:00
|
|
|
goscrobble.ApiDocsDirectory = os.Getenv("API_DOCS_DIRECTORY")
|
|
|
|
if goscrobble.ApiDocsDirectory == "" {
|
|
|
|
panic("API_DOCS_DIRECTORY required in .env")
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:21:28 +00:00
|
|
|
// Ignore reverse proxies
|
|
|
|
goscrobble.ReverseProxies = strings.Split(os.Getenv("REVERSE_PROXIES"), ",")
|
|
|
|
|
2021-08-13 09:37:53 +00:00
|
|
|
goscrobble.DevMode = false
|
|
|
|
devModeString := os.Getenv("DEV_MODE")
|
|
|
|
if strings.ToLower(devModeString) == "true" {
|
|
|
|
goscrobble.DevMode = true
|
|
|
|
}
|
|
|
|
|
2021-03-27 06:33:27 +00:00
|
|
|
// Store port
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "42069"
|
|
|
|
}
|
|
|
|
|
2021-03-29 07:56:34 +00:00
|
|
|
// 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
|
|
|
|
2021-03-29 07:56:34 +00:00
|
|
|
// Boot up Redis connection
|
|
|
|
goscrobble.InitRedis()
|
|
|
|
defer goscrobble.CloseRedisConn()
|
|
|
|
|
2021-08-13 09:37:53 +00:00
|
|
|
// Start background workers if not DevMode
|
|
|
|
if !goscrobble.DevMode {
|
|
|
|
go goscrobble.StartBackgroundWorkers()
|
|
|
|
defer goscrobble.EndBackgroundWorkers()
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Running in DevMode. No background workers running")
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
2021-04-01 12:56:08 +00:00
|
|
|
|
2021-03-23 08:43:44 +00:00
|
|
|
// Boot up API webserver \o/
|
2021-03-27 06:33:27 +00:00
|
|
|
goscrobble.HandleRequests(port)
|
2021-03-23 08:43:44 +00:00
|
|
|
}
|