Nashboard/cmd.go

44 lines
962 B
Go
Raw Normal View History

2022-12-09 00:58:18 +03:00
package main
import (
"context"
"log"
"net/http"
"time"
2022-12-11 02:05:03 +03:00
"git.nefrace.ru/nefrace/nashboard/server"
"git.nefrace.ru/nefrace/nashboard/storage"
2022-12-09 00:58:18 +03:00
"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)
}
}()
2022-12-11 02:05:03 +03:00
s := storage.MongodbStorage{Db: db}
2022-12-09 00:58:18 +03:00
router := mux.NewRouter()
2022-12-11 02:05:03 +03:00
srv := server.Server{
Host: ":4000",
Mux: router,
Db: s,
2022-12-09 00:58:18 +03:00
}
2022-12-11 02:05:03 +03:00
srv.InitHandlers()
log.Println("Server starting at ", srv.Host)
log.Fatal(http.ListenAndServe(srv.Host, router))
2022-12-09 00:58:18 +03:00
}