-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
116 lines (89 loc) · 2.89 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
package main
import (
"context"
"fmt"
"database/sql"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"log"
"os"
"time"
)
type Myresponse struct {
Message int32
}
func main() {
godotenv.Load()
lambda.Start(requestHandler)
}
func requestHandler(ctx context.Context, sqsEvent events.SQSEvent) (Myresponse , error) {
connStr := os.Getenv("DB_URL")
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
}
defer db.Close()
if err = db.Ping(); err != nil {
log.Fatal(err)
}
query := `SELECT * FROM userdetails WHERE apiKey = $1`
for _ , mes := range sqsEvent.Records {
var id string
var plantype string
var apikey string
var expiryon string
var hits int32
var email string
println("CHAL RAHA HAI ?????????????????????????????????")
err := db.QueryRow(query, mes.Body).Scan(&id, &plantype, &apikey, &expiryon, &hits, &email)
if err != nil {
println("plantype", plantype, "apikey", apikey, "hits", hits, "expiryon", expiryon)
log.Fatal("QUERY RETURNED: ", err)
}
// log.Fatal("email", email, "apikey", apikey, "hits", hits, "plantype", plantype, "expiryon", expiryon)
rateLimit(apikey, plantype, hits, expiryon, db)
}
return Myresponse{200}, nil
}
func DateToString(year int, month time.Month, day int) string {
return fmt.Sprintf("%d-%02d-%02d", year, int(month), day)
}
func rateLimit(apiKey string, plantype string, hits int32, expiryon string, db *sql.DB) {
year, month, day := time.Now().Date()
currentDate := DateToString(year, month, day)
switch plantype {
case "Hobby":
{
if hits > 100 {
db.Exec("UPDATE userkeystatus SET status = 'Daily limit reached' WHERE apikey = $1;", apiKey)
println("A user with Hobby plan reached its daily limit")
} else {
db.Exec("UPDATE userdetails SET hits = hits + 1 WHERE apikey = $1;", apiKey)
println("A user with hobby plan incremented its hits !!")
}
}
case "Priority":
{
println("DATES: ", expiryon[:10], currentDate)
// expirion is this format: 2023-03-09T00:00:00Z hence slicing it till 10th charcater ..
if expiryon[:10] == currentDate {
db.Exec("UPDATE userdetails SET plantype = 'Hobby', hits = 1 WHERE apikey = $1", apiKey)
println("User's priority plan with has expired !")
// send some notification to the user that his priority plan has expired and he has been re-subscribed to hobby plan
}
if hits > 5000 {
db.Exec("UPDATE userkeystatus SET status = 'Daily limit reached' WHERE apikey = $1;", apiKey)
println("A user with Priority plan reached its daily limit")
} else {
db.Exec("UPDATE userdetails SET hits = hits + 1 WHERE apikey = $1;", apiKey)
println("A user with Priority plan incremented its hits !!")
}
}
case "Enterprize":
{
println("ENTERPRIZE PLAN EXPIRED RE-SUBSCRIBE TO YOUR ENTERPRIZE PLAN TO CONTINUE !!")
}
}
}