-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (127 loc) · 3.49 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
srvFile "upload/pkg/file/httphandler"
"upload/shared/blob"
"upload/shared/composer"
"upload/shared/zombiekiller"
"github.com/go-chi/jwtauth/v5"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
ctx := context.Background()
if err := run(ctx, os.Args, os.Getenv, os.Getwd); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run(
ctx context.Context,
args []string,
getenv func(string) string,
getwd func() (string, error),
) error {
// load environment variables
if err := loadenv(args, getwd); err != nil {
return err
}
// setup auth
auth, err := auth(getenv)
if err != nil {
return err
}
// init mongodb (servers dependency)
uri := getenv("MONGODB_URI")
if uri == "" {
return errors.New(`environment variable "MONGODB_URI" is either empty or does not exist`)
}
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
return err
}
defer func() {
if err := mongoClient.Disconnect(ctx); err != nil {
panic(err)
}
}()
dbName := getenv("MONGODB_NAME")
if dbName == "" {
return errors.New(`environment variable "MONGODB_NAME" is either empty or does not exist`)
}
dbClient := mongoClient.Database(dbName)
// init zombie killer (servers dependency)
doneChan := make(chan any)
defer func() {
close(doneChan)
log.Println("Zombie killer received a signal to stop listening for incoming operations.")
}()
const thrashBuffer = 1 << 10 * 4
thrashChan := make(chan zombiekiller.KillOperation, thrashBuffer)
log.Println("Zombie Killer is active and listening for incoming operations.")
go zombiekiller.ListenForKillOperations(doneChan, thrashChan, 5, os.Stdout)
// init azure blob storage (servers dependency)
blobstg, err := blob.NewAzureBlobStorage()
if err != nil {
return err
}
// compose servers into a single mux
port := getenv("APP_PORT")
if port == "" {
return errors.New(`environment variable "APP_PORT" is either empty or does not exist`)
}
srv := composer.NewComposer()
file := srvFile.NewServer(auth, dbClient.Collection("files"), blobstg, thrashChan)
if err := srv.Compose(file); err != nil {
return err
}
log.Printf("Server listening on port %s...\n", port)
log.Fatalln(http.ListenAndServe(":"+port, srv))
return nil
}
func loadenv(args []string, getwd func() (string, error)) error {
envflag := flag.String("env", "unset", "set which environment to load variables")
flag.Parse()
env := *envflag
switch env {
case "unset":
fmt.Printf("Program executed without setting an environment. Using default option: %q.\n", "dev")
fallthrough
case "dev":
fmt.Printf("Running Go app using %q environment.\n", "development")
env = "dev.env"
case "test":
env = "test.env"
default:
return fmt.Errorf("invalid environment. valid options are: [%q, %q]", "dev", "test")
}
wd, err := getwd()
if err != nil {
return err
}
env = filepath.Join(wd, env)
if err := godotenv.Overload(env); err != nil {
return err
}
return nil
}
func auth(getenv func(string) string) (*jwtauth.JWTAuth, error) {
alg := getenv("JWT_ALG")
sign := getenv("JWT_SIGN")
switch {
case alg == "":
return nil, errors.New(`environment variable "JWT_ALG" is either empty or not set`)
case sign == "":
return nil, errors.New(`environment variable "JWT_SIGN" is either empty or not set`)
}
auth := jwtauth.New(alg, []byte(sign), nil)
return auth, nil
}