Created
July 8, 2023 06:31
-
-
Save winebarrel/39ff48f3deafbb4446248f163028eee5 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"fmt" | |
"net/http" | |
"os" | |
"strings" | |
"github.com/gin-gonic/gin" | |
"github.com/winebarrel/ddcost" | |
) | |
func newHandler(estimate bool) func(*gin.Context) { | |
client := ddcost.NewClient(&ddcost.ClientOptions{ | |
APIKey: os.Getenv("DD_API_KEY"), | |
APPKey: os.Getenv("DD_APP_KEY"), | |
}) | |
return func(c *gin.Context) { | |
var buf strings.Builder | |
err := client.PrintHistoricalCostByOrg(&buf, &ddcost.PrintHistoricalCostByOrgOptions{ | |
View: "sub-org", | |
Output: "csv", | |
Estimate: estimate, | |
}) | |
if err != nil { | |
c.String(http.StatusInternalServerError, err.Error()+"\n") | |
return | |
} | |
c.Writer.Header().Set("Content-Type", "text/csv") | |
c.String(http.StatusOK, buf.String()) | |
} | |
} | |
func main() { | |
r := gin.Default() | |
r.GET("/", newHandler(false)) | |
r.GET("/estimate", newHandler(true)) | |
addr := os.Getenv("LISTEN") | |
port := os.Getenv("PORT") | |
if addr == "" { | |
addr = "127.0.0.1" | |
} | |
if port == "" { | |
port = "8080" | |
} | |
r.Run(fmt.Sprintf("%s:%s", addr, port)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment