-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (94 loc) · 3.32 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
package main
import (
"context"
"log"
"github.com/algo7/day-planner-gpt-data-portal/api/middlewares"
"github.com/algo7/day-planner-gpt-data-portal/api/routes"
redisclient "github.com/algo7/day-planner-gpt-data-portal/internal/redis"
"github.com/algo7/day-planner-gpt-data-portal/pkg/utils"
"github.com/gofiber/contrib/swagger"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/healthcheck"
"github.com/gofiber/fiber/v2/middleware/keyauth"
"github.com/gofiber/template/html/v2"
"github.com/redis/go-redis/v9"
)
// @title Day Planner GPT Data Portal API
// @version 1.0
// @description day-planner-gpt-data-portal
// @termsOfService http://swagger.io/terms/
// @contact.name Algo7
// @contact.email tools@algo7.tools
// @license.name The GNU General Public License v3.0
// @license.url https://raw.githubusercontent.com/algo7/day-planner-gpt-data-portal/main/LICENSE
// @host gptportal.algo7.tools
// @BasePath /
func main() {
// Check if google_credentials.json and outlook_credentials.json exist in the ./credentials folder.
gExists := utils.FileExists("./credentials/google_credentials.json")
if !gExists {
log.Fatal("Error: google_credentials.json not found in ./credentials folder.")
}
oExists := utils.FileExists("./credentials/outlook_credentials.json")
if !oExists {
log.Fatal("Error: outlook_credentials.json not found in ./credentials folder.")
}
// Check Redis connection.
err := redisclient.RedisConnectionCheck()
if err != nil {
log.Fatalf("Error Connecting to Redis Server: %v", err)
}
// Check if the initial password is already set in Redis.
err = redisclient.Rdb.Get(context.Background(), "initial_password").Err()
if err != nil {
// If the initial password is not set in Redis, generate one and set it.
if err == redis.Nil {
log.Println("Initial Password not set in Redis. Generating...")
// Generate an API key as the initial password.
apiKey, err := utils.GenerateAPIKey()
if err != nil {
log.Fatalf("Error Generating the initial password: %v", err)
}
// Set the initial password in Redis.
err = redisclient.Rdb.Set(context.Background(), "initial_password", apiKey, 0).Err()
if err != nil {
log.Fatalf("Error Setting the initial password in Redis: %v", err)
}
log.Printf("Initial Password: %s This will expire once used.", apiKey)
} else {
log.Fatalf("Error checking the initial password in Redis: %v", err)
}
}
// Initialize standard Go html template engine
engine := html.New("./assets", ".html")
engine.Layout("embed") // Optional. Default: "embed"
engine.Delims("{{", "}}")
engine.Reload(false)
// App config.
app := fiber.New(fiber.Config{
CaseSensitive: true,
StrictRouting: true,
ServerHeader: "Day Planner GPT Data Portal",
AppName: "Day Planner GPT Data Portal",
Views: engine,
})
// Auth middleware
app.Use(keyauth.New(keyauth.Config{
Next: middlewares.AuthFilter,
KeyLookup: "header:X-API-KEY",
Validator: middlewares.ValidateAPIKey,
}))
// Healthcheck middleware /livez and /readyz routes
app.Use(healthcheck.New())
// Swagger middleware
app.Use(swagger.New(swagger.ConfigDefault))
// Load the routes.
routes.HomeRoutes(app)
routes.EmailsRoutes(app)
routes.AuthRoutes(app)
// Start the server.
err = app.Listen(":3000")
if err != nil {
log.Fatalf("Error Starting the Server: %v", err)
}
}