dongnguyenltqb
3/13/2020 - 11:57 AM

docker compose medium go main file

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
	go func() {
		client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://docker:123456@mongo1:27017/bar"))
		if err != nil {
			fmt.Println(err)
			return
		}
		if err = client.Connect(context.Background()); err != nil {
			fmt.Println(err)
			return
		}
		collection := client.Database("bar").Collection("fool")
		for {
			time.Sleep(time.Second)
			if _, err = collection.InsertOne(context.Background(), bson.M{
				"time": time.Now().String(),
			}); err != nil {
				fmt.Println(err)
			}
		}
	}()
	fmt.Println("vim-go")
	app := gin.Default()
	app.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, time.Now().String())
	})
	app.Run(":8888")
}