bazaar/cmd/server/main.go

49 lines
1.2 KiB
Go
Raw Normal View History

2022-01-02 22:11:00 +00:00
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/fmartingr/bazaar/pkg/manager"
"github.com/fmartingr/bazaar/pkg/shop/akiracomics"
"github.com/fmartingr/bazaar/pkg/shop/amazon"
2022-07-02 14:27:52 +00:00
"github.com/fmartingr/bazaar/pkg/shop/gtmstore"
"github.com/fmartingr/bazaar/pkg/shop/heroesdepapel"
"github.com/fmartingr/bazaar/pkg/shop/steam"
2022-01-02 22:11:00 +00:00
)
func main() {
m := manager.NewManager()
m.Register(akiracomics.Domains, akiracomics.NewAkiraShopFactory())
m.Register(steam.Domains, steam.NewSteamShopFactory())
m.Register(heroesdepapel.Domains, heroesdepapel.NewHeroesDePapelShopFactory())
m.Register(amazon.Domains, amazon.NewAmazonShopFactory())
2022-07-02 14:27:52 +00:00
m.Register(gtmstore.Domains, gtmstore.NewGTMStoreShopFactory())
2022-01-02 22:11:00 +00:00
http.HandleFunc("/item", func(rw http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
fmt.Fprintf(rw, "ParseForm() err: %v", err)
return
}
product, err := m.Retrieve(r.PostForm.Get("url"))
2022-01-02 22:11:00 +00:00
if err != nil {
rw.WriteHeader(500)
return
}
payload, _ := json.Marshal(product)
rw.Header().Add("Content-Type", "application/json")
rw.Write(payload)
})
log.Println("starting server")
2022-01-02 22:11:00 +00:00
if err := http.ListenAndServe(":5001", http.DefaultServeMux); err != nil {
log.Printf("Error: %s", err)
}
}