/
main.go
133 lines (117 loc) · 3.16 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
130
131
132
133
package main
import (
"context"
"encoding/json"
"os"
"github.com/akrylysov/algnhsa"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
"github.com/nextdotid/proof_server/common"
myconfig "github.com/nextdotid/proof_server/config"
"github.com/nextdotid/proof_server/controller"
"github.com/nextdotid/proof_server/model"
"github.com/nextdotid/proof_server/util/sqs"
"github.com/nextdotid/proof_server/validator/activitypub"
"github.com/nextdotid/proof_server/validator/das"
"github.com/nextdotid/proof_server/validator/discord"
"github.com/nextdotid/proof_server/validator/dns"
"github.com/nextdotid/proof_server/validator/ethereum"
"github.com/nextdotid/proof_server/validator/github"
"github.com/nextdotid/proof_server/validator/keybase"
"github.com/nextdotid/proof_server/validator/minds"
"github.com/nextdotid/proof_server/validator/solana"
"github.com/nextdotid/proof_server/validator/steam"
"github.com/nextdotid/proof_server/validator/twitter"
"github.com/sirupsen/logrus"
)
var (
initialized = false
)
func init_db(cfg aws.Config) {
model.Init()
}
func init_sqs(cfg aws.Config) {
sqs.Init(cfg)
}
func init_validators() {
twitter.Init()
ethereum.Init()
keybase.Init()
github.Init()
discord.Init()
das.Init()
solana.Init()
minds.Init()
dns.Init()
steam.Init()
activitypub.Init()
}
func init() {
cfg, err := config.LoadDefaultConfig(
context.Background(),
// TODO: change region
config.WithRegion("ap-east-1"),
)
if err != nil {
logrus.Fatalf("Unable to load AWS config: %s", err)
}
init_config_from_aws_secret()
logrus.SetLevel(logrus.WarnLevel)
common.CurrentRuntime = common.Runtimes.Lambda
init_db(cfg)
init_sqs(cfg)
init_validators()
controller.Init()
}
func main() {
algnhsa.ListenAndServe(controller.Engine, nil)
}
func init_config_from_aws_secret() {
if initialized {
return
}
secret_name := getE("SECRET_NAME", "")
region := getE("SECRET_REGION", "")
// Create a Secrets Manager client
cfg, err := config.LoadDefaultConfig(
context.Background(),
config.WithRegion(region),
)
if err != nil {
logrus.Fatalf("Unable to load SDK config: %v", err)
}
client := secretsmanager.NewFromConfig(cfg)
input := secretsmanager.GetSecretValueInput{
SecretId: aws.String(secret_name),
VersionStage: aws.String("AWSCURRENT"),
}
result, err := client.GetSecretValue(context.Background(), &input)
if err != nil {
logrus.Fatalf("Error occured: %s", err.Error())
}
// Decrypts secret using the associated KMS CMK.
// Depending on whether the secret is a string or binary, one of these fields will be populated.
if result.SecretString == nil {
logrus.Fatalf("cannot get secret string")
}
secret_string := *result.SecretString
err = json.Unmarshal([]byte(secret_string), myconfig.C)
if err != nil {
logrus.Fatalf("Error during parsing config JSON: %v", err)
}
initialized = true
}
func getE(env_key, default_value string) string {
result := os.Getenv(env_key)
if len(result) == 0 {
if len(default_value) > 0 {
return default_value
} else {
logrus.Fatalf("ENV %s must be given! Abort.", env_key)
return ""
}
} else {
return result
}
}