package main import ( "context" "log" "net/http" "time" "git.nefrace.ru/nefrace/nashboard/server" "git.nefrace.ru/nefrace/nashboard/storage" "github.com/gorilla/mux" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func run() { ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) db, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://root:example@localhost:27017")) if err != nil { log.Fatal("Can not connect to db: ", err) } if err := db.Ping(ctx, nil); err != nil { log.Fatal("Can not connect to db: ", err) } cancel() // defer func() { if err := db.Disconnect(context.TODO()); err != nil { panic(err) } }() s := storage.MongodbStorage{Db: db} router := mux.NewRouter() srv := server.Server{ Host: ":4000", Mux: router, Db: s, } srv.InitHandlers() log.Println("Server starting at ", srv.Host) log.Fatal(http.ListenAndServe(srv.Host, router)) }