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
| func GracefullyListenAndServe(ctx context.Context, servePort string, rtr *mux.Router) { | |
| http.Handle("/", rtr) | |
| h := &http.Server{ | |
| Addr: fmt.Sprintf(":%v", servePort), | |
| Handler: handlers.CORS()(rtr), | |
| } | |
| sig := make(chan os.Signal, 1) |
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
| var ReadCollectionPayload = func(w http.ResponseWriter, r *http.Request) { | |
| (w).Header().Set("Content-Type", "application/json") | |
| var payload mongo.QueryInputs | |
| // Try to decode the request body into the struct. If there is an error, | |
| // respond to the client with the error message and a 400 status code. | |
| err := json.NewDecoder(r.Body).Decode(&payload) | |
| if err != nil { | |
| writeStatus(&w, http.StatusBadRequest, "invalid query payload") |
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 mongo | |
| import ( | |
| "fmt" | |
| "go.mongodb.org/mongo-driver/bson" | |
| "go.mongodb.org/mongo-driver/mongo/options" | |
| "log" | |
| ) | |
| // GetEvents executes a Find query with query parameters that don't include operations |
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 mongo | |
| import ( | |
| "context" | |
| mgo "go.mongodb.org/mongo-driver/mongo" | |
| ) | |
| // ExecuteParams defines the necessary values to execute a | |
| // mongo query | |
| type ExecuteParams struct { | |
| Context context.Context |
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 mongo | |
| import ( | |
| "go.mongodb.org/mongo-driver/bson" | |
| "go.mongodb.org/mongo-driver/bson/primitive" | |
| ) | |
| const ( | |
| eventVersionKey = "event.version" | |
| eventTimestampKey = "event.timestamp" |
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
| // Connection tests the mongodb connection health | |
| var Health = func(w http.ResponseWriter, r *http.Request) { | |
| (w).Header().Set("Content-Type", "application/json") | |
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | |
| defer cancel() | |
| client, err := mongo.Connect(ctx) | |
| if err != nil { | |
| writeStatus(&w, http.StatusBadRequest, err.Error()) |
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
| // Connect returns a successfully connected mongo client | |
| func Connect(ctx context.Context) (*mongo.Client, error) { | |
| url := "mongodb://admin:admin@localhost:27017" | |
| log.Printf("connect to mongodb at: %v", url) | |
| opts := options.Client() | |
| opts.ApplyURI(url) |
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
| version: '3.7' | |
| services: | |
| mongo: | |
| image: mongo | |
| restart: always | |
| ports: | |
| - 27017:27017 | |
| environment: |
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 services | |
| import ( | |
| "encoding/json" | |
| "log" | |
| "net/http" | |
| ) | |
| type status struct { | |
| Message string `json:"message"` |
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
| func main() { | |
| // create anew http router | |
| rtr := mux.NewRouter() | |
| // health endpoint | |
| rtr.HandleFunc("/health", services.Health).Methods(get) | |
| // use go routinue to serve endpoint | |
| ctx := context.Background() |