-
Notifications
You must be signed in to change notification settings - Fork 83
/
main.go
106 lines (80 loc) · 2.19 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
socketio "github.com/googollee/go-socket.io"
"github.com/gorilla/handlers"
"github.com/joho/godotenv"
"github.com/stripe/stripe-go/v72"
transportHttp "zuri.chat/zccore/internal/transport"
"zuri.chat/zccore/logger"
"zuri.chat/zccore/utils"
sentry "github.com/getsentry/sentry-go"
"github.com/rs/cors"
"zuri.chat/zccore/messaging"
)
type App struct {
Port string
}
func (app *App) Run() error {
// Socket events
var Server = socketio.NewServer(nil)
messaging.SocketEvents(Server)
// Set Stripe api key
stripe.Key = os.Getenv("STRIPE_KEY")
if err := utils.ConnectToDB(os.Getenv("CLUSTER_URL")); err != nil {
return fmt.Errorf("could not connect to MongoDB: \n%v", err)
}
err := sentry.Init(sentry.ClientOptions{
Dsn: os.Getenv("SENTRY_DNS"),
Environment: os.Getenv("ENV"),
Release: "[email protected]",
Debug: true,
})
if err != nil {
return fmt.Errorf("sentry.Init: %s", err)
}
// Flush buffered events before the program terminates.
// Set the timeout to the maximum duration the program can afford to wait.
num := 2
defer sentry.Flush(time.Duration(num) * time.Second)
sentry.CaptureMessage("It works!")
// transporter
handler := transportHttp.NewHandler(Server)
handler.SetupRoutes()
c := cors.AllowAll()
h := transportHttp.RequestDurationMiddleware(handler.Router)
srv := &http.Server{
Handler: handlers.LoggingHandler(os.Stdout, c.Handler(h)),
Addr: ":" + app.Port,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
//nolint:errcheck //CODEI8: ignore error check
go Server.Serve()
logger.Info("Socket Served")
logger.Info("Zuri Chat API running on port %s", app.Port)
if err := srv.ListenAndServe(); err != nil {
return err
}
defer Server.Close()
return nil
}
func main() {
// load .env file if it exists
err := godotenv.Load(".env")
if err != nil {
logger.Error("Error loading .env file: %v", err)
}
logger.Info("Environment variables successfully loaded. Starting application...")
// get PORT from environment variables
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
app := App{Port: port}
log.Fatal(app.Run())
}