Skip to content

Commit

Permalink
add dummy backend
Browse files Browse the repository at this point in the history
  • Loading branch information
sechmann committed Jul 5, 2024
1 parent 89faad3 commit 28026f1
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
46 changes: 46 additions & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 53 additions & 0 deletions backend/products.go
Original file line number Diff line number Diff line change
@@ -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"},
},
},
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 28026f1

Please sign in to comment.