-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (98 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
package main
import (
"context"
"fmt"
"net/http"
"os"
cors "github.com/efuchsman/EliFuchsmanBE/config"
"github.com/efuchsman/EliFuchsmanBE/handlers"
elifuchsman "github.com/efuchsman/EliFuchsmanBE/internal/eli_fuchsman"
elifuchsmandb "github.com/efuchsman/EliFuchsmanBE/internal/eli_fuchsman_db"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
)
func main() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
log.Info("Starting Eli's Backend Application!")
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
awsRegion := os.Getenv("AWS_REGION")
log.Infof("AWS Region: %s", awsRegion)
edb, err := elifuchsmandb.NewEliFuchsmanDB(awsRegion, "")
if err != nil {
log.WithError(err).Fatal("Failure opening database connection")
}
eliClient := elifuchsman.NewEliFuchsmanClient(edb)
tableOne := os.Getenv("DYNAMO_TABLE_1")
tableTwo := os.Getenv("DYNAMO_TABLE_2")
tableThree := os.Getenv("DYNAMO_TABLE_3")
bucket := os.Getenv("S3_BUCKET")
bucketKey := os.Getenv("S3_BUCKET_KEY")
expectedAPIKey := os.Getenv("API_KEY")
router := mux.NewRouter()
router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
next.ServeHTTP(w, r)
return
}
apiKey := r.Header.Get("API-Key")
if apiKey != expectedAPIKey {
handlers.Forbidden403(w, "API KEY")
return
}
ctx := context.WithValue(r.Context(), "TableOne", tableOne)
infoFile := "data/basic_info.json"
ctx = context.WithValue(ctx, "InfoFile", infoFile)
summaryFile := "data/summary.json"
ctx = context.WithValue(ctx, "SummaryFile", summaryFile)
ctx = context.WithValue(ctx, "TableTwo", tableTwo)
ctx = context.WithValue(ctx, "Bucket", bucket)
ctx = context.WithValue(ctx, "BucketKey", bucketKey)
ctx = context.WithValue(ctx, "AWSRegion", awsRegion)
ctx = context.WithValue(ctx, "TableThree", tableThree)
next.ServeHTTP(w, r.WithContext(ctx))
})
})
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Info("Endpoint Hit: /")
fmt.Fprintf(w, "Welcome to Eli's Backend!")
})
eliHandler := handlers.NewHandler(eliClient)
router.HandleFunc("/info", func(w http.ResponseWriter, r *http.Request) {
log.Info("Endpoint Hit: /info")
eliHandler.GetBasicInfo(w, r)
}).Methods("GET")
router.HandleFunc("/education", func(w http.ResponseWriter, r *http.Request) {
log.Info("Endpoint Hit: /education")
eliHandler.GetEducationHistory(w, r)
}).Methods("GET")
router.HandleFunc("/summary", func(w http.ResponseWriter, r *http.Request) {
log.Info("Endpoint Hit: /summary")
eliHandler.GetSummary(w, r)
}).Methods("GET")
router.HandleFunc("/experience", func(w http.ResponseWriter, r *http.Request) {
log.Info("Endpoint Hit: /experience")
eliHandler.GetExperienceHistory(w, r)
}).Methods("GET")
router.HandleFunc("/resume", func(w http.ResponseWriter, r *http.Request) {
log.Info("Endpoint Hit: /resume")
eliHandler.GetResume(w, r)
}).Methods("GET")
router.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
log.Info("Endpoint Hit: /projects")
eliHandler.GetProjects(w, r)
}).Methods("GET")
handler := cors.SetCORS(router)
port := 80
fmt.Printf("Server is running on :%d\n", port)
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), handler); err != nil {
log.Fatalf("Error starting server: %v", err)
}
fmt.Println("Application started successfully")
}