diff --git a/backend/main.go b/backend/main.go new file mode 100644 index 0000000..48d7be4 --- /dev/null +++ b/backend/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "context" + "encoding/json" + "net" + "net/http" + "os" + "os/signal" + + "github.com/sirupsen/logrus" +) + +func main() { + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) + defer cancel() + + log := logrus.New().WithField("component", "main") + if err := run(ctx, log); err != nil { + log.WithError(err).Error("running main program") + os.Exit(1) + } +} + +func run(ctx context.Context, log logrus.FieldLogger) error { + l, err := net.Listen("tcp", ":8080") + if err != nil { + return err + } + s := &http.Server{ + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if err := json.NewEncoder(w).Encode(products); err != nil { + log.WithError(err).Error("encoding products") + w.Write([]byte("internal server error")) + w.WriteHeader(http.StatusInternalServerError) + } + }), + } + + log.WithField("addr", l.Addr()).Info("listening") + go s.Serve(l) + + <-ctx.Done() + + return nil +} diff --git a/backend/products.go b/backend/products.go new file mode 100644 index 0000000..b0de729 --- /dev/null +++ b/backend/products.go @@ -0,0 +1,53 @@ +package main + +type Product struct { + Name string `json:"name"` + Price string `json:"price"` + Description string `json:"description"` +} + +type Category struct { + Name string `json:"name"` + Products []Product `json:"products"` +} + +var products = []Category{ + { + Name: "Skinnprodukter", + Products: []Product{ + {Name: "Sengefell", Price: "7000,- pr kvm + tekstil", Description: "Fell til enkeltseng. Du kan velge pelsfarge (rase) og om du vil ha åkle eller trykk på fellen. Størrelse ca 1x1,8 meter. Syr også til dobbeltseng. Ring for bestilling! Syr gjerne til gamle åkler også."}, + }, + }, + { + Name: "Saueskinn", + Products: []Product{ + {Name: "Saueskinn", Price: "2000,- pr stk", Description: "Lorem Ipsum etc mer tekst kan komme her"}, + }, + }, + { + Name: "Toving", + Products: []Product{ + {Name: "Ull", Price: "200,-", Description: "Lorem Ipsum etc mer tekst kan komme her"}, + }, + }, + { + Name: "Alpakkagarn", + Products: []Product{ + {Name: "Alpakkagarn 1", Price: "10,- pr g", Description: "Lorem Ipsum etc mer tekst kan komme her"}, + {Name: "Alpakkagarn 2", Price: "12,- pr g", Description: "Lorem Ipsum etc mer tekst kan komme her"}, + }, + }, + { + Name: "Ullundertøy", + Products: []Product{ + {Name: "Bokser", Price: "400,-", Description: "Lorem Ipsum etc mer tekst kan komme her"}, + {Name: "Sokker", Price: "400,- pr par", Description: "Lorem Ipsum etc mer tekst kan komme her"}, + }, + }, + { + Name: "Skinn", + Products: []Product{ + {Name: "Kuskinn", Price: "3000,-", Description: "Lorem Ipsum etc mer tekst kan komme her"}, + }, + }, +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..287a8c5 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module github.com/sechmann/googvarm + +go 1.22.5 + +require github.com/sirupsen/logrus v1.9.3 + +require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2cbdecd --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=