grain/main.go

50 lines
1.2 KiB
Go
Raw Normal View History

2024-07-19 22:07:04 +00:00
package main
import (
"fmt"
"log"
"net/http"
2024-07-23 17:30:29 +00:00
"grain/relay"
"grain/relay/db"
"grain/relay/utils"
2024-07-23 20:40:39 +00:00
"grain/web"
2024-07-19 22:07:04 +00:00
"golang.org/x/net/websocket"
)
func main() {
// Load configuration
config, err := utils.LoadConfig("config.yml")
if err != nil {
log.Fatal("Error loading config: ", err)
}
// Initialize MongoDB client
_, err = db.InitDB(config.MongoDB.URI, config.MongoDB.Database)
if err != nil {
log.Fatal("Error initializing database: ", err)
}
defer db.DisconnectDB()
2024-07-20 12:41:47 +00:00
2024-07-23 20:40:39 +00:00
// Run the WebSocket server in a goroutine
go func() {
fmt.Printf("WebSocket server is running on ws://localhost%s\n", config.Relay.Port)
err := http.ListenAndServe(config.Relay.Port, websocket.Handler(relay.Listener))
if err != nil {
fmt.Println("Error starting WebSocket server:", err)
}
}()
// Run the HTTP server for serving static files and home page
mux := http.NewServeMux()
mux.HandleFunc("/", web.RootHandler)
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static"))))
fmt.Printf("Http server is running on http://localhost%s\n", config.Web.Port)
err = http.ListenAndServe(config.Web.Port, mux)
if err != nil {
2024-07-23 20:40:39 +00:00
fmt.Println("Error starting web server:", err)
}
2024-07-19 22:07:04 +00:00
}