Support for new dev env (#240)

This commit is contained in:
Dominic Wong
2021-10-22 13:38:01 +01:00
committed by GitHub
parent decefb678b
commit 2332224681
2 changed files with 18 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"flag"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@@ -29,7 +30,11 @@ type PublicAPI struct {
DisplayName string `json:"display_name,omitempty"` DisplayName string `json:"display_name,omitempty"`
} }
func publishAPI(apiSpec *PublicAPI) error { const (
prodAPIDomain = "api.m3o.com"
)
func publishAPI(apiSpec *PublicAPI, domain string) error {
client := &http.Client{} client := &http.Client{}
//Encode the data //Encode the data
@@ -40,7 +45,7 @@ func publishAPI(apiSpec *PublicAPI) error {
rbody := bytes.NewBuffer(postBody) rbody := bytes.NewBuffer(postBody)
//Leverage Go's HTTP Post function to make request //Leverage Go's HTTP Post function to make request
req, err := http.NewRequest("POST", "https://api.m3o.com/publicapi/Publish", rbody) req, err := http.NewRequest("POST", fmt.Sprintf("https://%s/publicapi/Publish", domain), rbody)
// Add auth headers here if needed // Add auth headers here if needed
req.Header.Add("Authorization", `Bearer `+os.Getenv("MICRO_ADMIN_TOKEN")) req.Header.Add("Authorization", `Bearer `+os.Getenv("MICRO_ADMIN_TOKEN"))
@@ -62,16 +67,22 @@ func publishAPI(apiSpec *PublicAPI) error {
} }
func main() { func main() {
files, err := ioutil.ReadDir(os.Args[1]) workDir, _ := os.Getwd()
domainFlag := flag.String("domain", prodAPIDomain, "domain to publish to e.g. api.m3o.com")
serviceFlag := flag.String("service", "", "individual service to publish e.g. helloworld")
flag.Parse()
files, err := ioutil.ReadDir(flag.Arg(0))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
workDir, _ := os.Getwd()
for _, f := range files { for _, f := range files {
if strings.Contains(f.Name(), "clients") || strings.Contains(f.Name(), "examples") { if strings.Contains(f.Name(), "clients") || strings.Contains(f.Name(), "examples") {
continue continue
} }
if len(*serviceFlag) > 0 && f.Name() != *serviceFlag {
continue
}
if f.IsDir() && !strings.HasPrefix(f.Name(), ".") { if f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
serviceDir := filepath.Join(workDir, f.Name()) serviceDir := filepath.Join(workDir, f.Name())
serviceFiles, err := ioutil.ReadDir(serviceDir) serviceFiles, err := ioutil.ReadDir(serviceDir)
@@ -180,7 +191,7 @@ func main() {
} }
// publish the api // publish the api
if err := publishAPI(publicApi); err != nil { if err := publishAPI(publicApi, *domainFlag); err != nil {
fmt.Println("Failed to save data to publicapi service", err) fmt.Println("Failed to save data to publicapi service", err)
os.Exit(1) os.Exit(1)
} }

View File

@@ -11,10 +11,10 @@ func CreateArecord() {
dbService := db.NewDbService(os.Getenv("MICRO_API_TOKEN")) dbService := db.NewDbService(os.Getenv("MICRO_API_TOKEN"))
rsp, err := dbService.Create(&db.CreateRequest{ rsp, err := dbService.Create(&db.CreateRequest{
Record: map[string]interface{}{ Record: map[string]interface{}{
"id": "1",
"name": "Jane", "name": "Jane",
"age": 42, "age": 42,
"isActive": true, "isActive": true,
"id": "1",
}, },
Table: "users", Table: "users",
}) })