Skip to content

Instantly share code, notes, and snippets.

@doorbash
Last active September 10, 2019 07:28
Show Gist options
  • Save doorbash/759e7656c99c2432f290ab5540ac75ee to your computer and use it in GitHub Desktop.
Save doorbash/759e7656c99c2432f290ab5540ac75ee to your computer and use it in GitHub Desktop.
github.com/doorbash/my-api/main.go
package main
import (
"context"
"github.com/doorbash/my-api/routes"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"net/http"
"time"
)
const DB_URL = "mongodb://localhost:27017"
func main() {
client, err := mongo.NewClient(options.Client().ApplyURI(DB_URL))
if err != nil {
panic(err)
}
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
err = client.Connect(ctx)
var db = client.Database("mydb")
routes.InitUserRouter(db)
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
ctx, _ = context.WithTimeout(context.Background(), 5*time.Second)
client.Disconnect(ctx)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment