-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (109 loc) · 3.15 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
package main
import (
"context"
"database/sql"
"embed"
"encoding/gob"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
amqp "github.com/rabbitmq/amqp091-go"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/golang-migrate/migrate/v4/source/httpfs"
"github.com/infnetdanpro/go-mongo-stats/model"
"github.com/infnetdanpro/go-mongo-stats/store"
"github.com/infnetdanpro/go-mongo-stats/worker"
"github.com/joho/godotenv"
)
//go:embed migrations
var migrations embed.FS
// Go Migrations latest up version
const schemaVersion = 3
// Cookie Based authorization
var cookieStore *sessions.CookieStore
func ensureSchema(db *sql.DB) error {
sourceInstance, err := httpfs.New(http.FS(migrations), os.Getenv("MIGRATION_DIR"))
if err != nil {
return fmt.Errorf("invalid source instance, %w", err)
}
targetInstance, err := sqlite3.WithInstance(db, new(sqlite3.Config))
if err != nil {
return fmt.Errorf("invalid target sqlite instance, %w", err)
}
m, err := migrate.NewWithInstance("httpfs", sourceInstance, "sqlite3", targetInstance)
if err != nil {
return fmt.Errorf("failed to initialize migrate instance, %w", err)
}
err = m.Migrate(schemaVersion)
if err != nil && err != migrate.ErrNoChange {
return err
}
log.Println("Migration applied")
return sourceInstance.Close()
}
func initSessionStore() {
// Prepare cookie based authorization for web
cookieStore = sessions.NewCookieStore(
securecookie.GenerateRandomKey(64),
securecookie.GenerateRandomKey(32),
)
cookieStore.Options = &sessions.Options{
Path: "/",
MaxAge: 60 * 15,
HttpOnly: true,
}
gob.Register(model.User{})
}
func main() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
db, err := sql.Open(os.Getenv("DB_DRIVER"), os.Getenv("DB_DSN"))
if err != nil {
log.Fatal(err.Error())
panic("Problem with DB!")
}
defer db.Close()
if err = db.Ping(); err != nil {
log.Fatal(err.Error())
panic(err.Error())
}
// Apply migrations
migrationError := ensureSchema(db)
if migrationError != nil {
log.Fatal(migrationError.Error())
panic(migrationError.Error())
}
initSessionStore()
qConnection, err := amqp.Dial(os.Getenv("RABBITMQ_URL"))
if err != nil {
panic(err.Error())
}
defer qConnection.Close()
mgClient, _ := mongo.Connect(context.TODO(), options.Client().ApplyURI(os.Getenv("MONGODB_URI")))
appRepo := store.AppRepository{DB: db}
eventRepo := store.EventRepository{Connection: qConnection}
eventStorageRepo := store.EventStorageRepository{MG: mgClient}
userRepo := store.UserRepository{DB: db}
server := Server{
AppRepository: appRepo,
EventRepository: eventRepo,
StorageEventRepository: eventStorageRepo,
UserRepository: userRepo,
CookieStore: cookieStore,
}
log.Println("Ready to start")
go worker.QueueConsumer(qConnection, mgClient, os.Getenv("QUEUE_NAME"))
go server.Start()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
}