Last active
September 10, 2019 07:28
-
-
Save doorbash/759e7656c99c2432f290ab5540ac75ee to your computer and use it in GitHub Desktop.
github.com/doorbash/my-api/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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