-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (111 loc) · 3.86 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
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
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"log"
"net/http"
"os"
"simple-go-app/internal/helpers"
"simple-go-app/internal/logging"
"simple-go-app/internal/parsing"
"simple-go-app/internal/store"
"strconv"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/gin-gonic/gin"
"simple-go-app/internal/dispatcher"
)
var (
healthStatus bool
healthMutex sync.Mutex
)
func main() {
// Load environment variables
helpers.LoadEnv()
logging.InfoLogger.Println("app env: " + helpers.GetEnvVariable("APP_ENV"))
// Get environment variables
sqsPrefix := helpers.GetEnvVariable("SQS_PREFIX")
requestsQueueName := helpers.GetEnvVariable("REQUESTS_QUEUE")
sqsURL := fmt.Sprintf("%s/%s", sqsPrefix, requestsQueueName)
awsSecretKey := helpers.GetEnvVariable("AWS_SECRET_ACCESS_KEY")
awsAccessKey := helpers.GetEnvVariable("AWS_ACCESS_KEY_ID")
awsRegion := helpers.GetEnvVariable("AWS_REGION")
awsBucket := helpers.GetEnvVariable("AWS_BUCKET")
//dbConnectionString := envHelper.GetEnvVariable("DB_CONNECTION_STRING")
dbHost := helpers.GetEnvVariable("DB_HOST")
dbPort := helpers.GetEnvVariable("DB_PORT")
dbUser := helpers.GetEnvVariable("DB_USERNAME")
dbPassword := helpers.GetEnvVariable("DB_PASSWORD")
dbName := helpers.GetEnvVariable("DB_DATABASE")
cacheTableName := helpers.GetEnvVariable("DYNAMODB_CACHE_TABLE")
logging.InfoLogger.Println("Environment variables loaded successfully.")
// Set up mysql connection
logging.InfoLogger.Println("Database connection string: " + dbUser + ":" + dbPassword + "@tcp(" + dbHost + ":" + dbPort + ")/" + dbName)
db, err := sql.Open("mysql", dbUser+":"+dbPassword+"@tcp("+dbHost+":"+dbPort+")/"+dbName)
if err != nil {
logging.ErrorLogger.Println("Error opening database:", err)
}
s := store.New(db)
// ping database
err = s.GetDB().Ping()
if err != nil {
log.Fatal("Error pinging database:", err)
} else {
log.Println("Database pinged successfully.")
}
// Set up AWS session
sess, err := session.NewSession(&aws.Config{
Region: aws.String(awsRegion),
Credentials: credentials.NewStaticCredentials(awsAccessKey, awsSecretKey, ""),
})
if err != nil {
log.Fatal("Error creating AWS session:", err)
}
// Set up the queue service
sqsSvc := sqs.New(sess)
// Create a channel for communication between dispatcher and workers
messageQueue := make(chan *sqs.Message, 10) // Adjust the buffer size as needed
// Start dispatcher
go dispatcher.Dispatcher(sqsSvc, sqsURL, messageQueue)
// set up cache service
cacheSvc, err := helpers.NewCacheHelper(sess, cacheTableName)
if err != nil {
log.Fatal("Error creating cache service:", err)
}
workFunc := func() {
numWorkers, _ := strconv.Atoi(helpers.GetEnvVariable("WORKER_COUNT"))
// Start three workers
for i := 1; i <= numWorkers; i++ {
go dispatcher.Worker(i, messageQueue, sqsSvc, sqsURL, awsBucket, s, cacheSvc)
}
}
// Start a timer for periodic health checks
go func() {
// Introduce a 15-second delay before updating healthStatus to true
startDelay := helpers.GetEnvVariable("START_DELAY_SECONDS")
// Convert the startDelay string to an int
startDelayInt, _ := strconv.Atoi(startDelay)
time.Sleep(time.Duration(startDelayInt) * time.Second)
parsing.CheckGrobidHealth(&healthStatus, &healthMutex, workFunc)
for {
// this is backup if server doesn't shutdown on bad response
time.Sleep(1 * time.Minute) // Adjust the interval as needed
parsing.CheckGrobidHealth(&healthStatus, &healthMutex)
}
}()
r := gin.Default()
r.GET("/", func(c *gin.Context) {
host, _ := os.Hostname()
c.JSON(http.StatusOK, gin.H{"hostname": host})
})
r.GET("/health", func(c *gin.Context) {
// Return the global health status
c.JSON(http.StatusOK, gin.H{"healthy": healthStatus})
})
r.Run()
}