diff --git a/src/go.mod b/src/go.mod index ba31e42..6a57bea 100644 --- a/src/go.mod +++ b/src/go.mod @@ -2,7 +2,10 @@ module example/web-service-gin go 1.21.6 -require github.com/gin-gonic/gin v1.9.1 +require ( + github.com/gin-gonic/gin v1.9.1 + github.com/joho/godotenv v1.5.1 +) require ( github.com/bytedance/sonic v1.9.1 // indirect diff --git a/src/go.sum b/src/go.sum index e968ca4..f2890d0 100644 --- a/src/go.sum +++ b/src/go.sum @@ -23,6 +23,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= diff --git a/src/main.go b/src/main.go index 4302e44..ad5e021 100644 --- a/src/main.go +++ b/src/main.go @@ -1,14 +1,29 @@ package main import ( - "net/http" + "encoding/json" + "log" + "net/http" + "os" - "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin" + "github.com/joho/godotenv" ) +type ListingProps struct { + Refs []string `json:"refs"` + Price int `json:"price"` + Beds int `json:"beds"` + Baths int `json:"baths"` + Date string `json:"date"` + Notes string `json:"notes"` + IsFavorite bool `json:"isFavorite"` + IsDismissed bool `json:"isDismissed"` +} + func basicAuth(c *gin.Context) { user, password, hasAuth := c.Request.BasicAuth() - if hasAuth && user == "testuser" && password == "testpass" { + if hasAuth && user == os.Getenv("AUTH_USER") && password == os.Getenv("AUTH_PASS") { c.Next() } else { c.Abort() @@ -17,13 +32,31 @@ func basicAuth(c *gin.Context) { } } +func getListings(c *gin.Context) { + data, err := os.ReadFile("../data/listings.json") + if err != nil { + log.Print(err) + } + + m := make(map[string]ListingProps) + json.Unmarshal(data, &m) + + c.IndentedJSON(http.StatusOK, m) +} + func ping(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"pong": true}) } func main() { + err := godotenv.Load("../.env") + if err != nil { + log.Fatalf("Error loading .env: %s", err) + } + router := gin.Default() router.GET("/ping", basicAuth, ping) + router.GET("/listings", getListings) router.Run("0.0.0.0:8080") }