2024-07-19 22:07:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2024-07-20 01:09:42 +00:00
|
|
|
"grain/db"
|
|
|
|
"grain/events"
|
|
|
|
"grain/requests"
|
|
|
|
"grain/utils"
|
|
|
|
|
2024-07-19 22:07:04 +00:00
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-07-20 01:09:42 +00:00
|
|
|
// Load configuration
|
|
|
|
config, err := utils.LoadConfig("config.yml")
|
2024-07-19 23:05:12 +00:00
|
|
|
if err != nil {
|
2024-07-20 01:09:42 +00:00
|
|
|
log.Fatal("Error loading config: ", err)
|
2024-07-19 23:05:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-20 01:09:42 +00:00
|
|
|
// Initialize MongoDB client
|
|
|
|
client, err := db.InitDB(config.MongoDB.URI, config.MongoDB.Database)
|
2024-07-19 23:05:12 +00:00
|
|
|
if err != nil {
|
2024-07-20 01:09:42 +00:00
|
|
|
log.Fatal("Error initializing database: ", err)
|
2024-07-19 23:05:12 +00:00
|
|
|
}
|
2024-07-20 01:09:42 +00:00
|
|
|
defer db.DisconnectDB(client)
|
2024-07-19 23:05:12 +00:00
|
|
|
|
|
|
|
// Initialize collections
|
2024-07-20 01:09:42 +00:00
|
|
|
events.InitCollections(client, config.Collections.EventKind0, config.Collections.EventKind1)
|
2024-07-19 23:05:12 +00:00
|
|
|
|
|
|
|
// Start WebSocket server
|
2024-07-20 01:09:42 +00:00
|
|
|
http.Handle("/", websocket.Handler(requests.Handler))
|
|
|
|
fmt.Println("WebSocket server started on", config.Server.Address)
|
|
|
|
err = http.ListenAndServe(config.Server.Address, nil)
|
2024-07-19 23:05:12 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error starting server:", err)
|
|
|
|
}
|
2024-07-19 22:07:04 +00:00
|
|
|
}
|