tb/main #1

Open
terrabytten wants to merge 17 commits from tb/main into main
11 changed files with 134 additions and 66 deletions
Showing only changes of commit c1930d6821 - Show all commits

View file

@ -14,3 +14,18 @@ The goal of this project is to be a crowd sourced resource to find out if a prod
- alergen ingredient database lookup?
- create mobile and desktop front ends
- moderation tooling?
Created database by
## How to start database
- Run `brew services start postgresql`
- Create database
- Setting environment variables:
- PGHOST=localhost
- DB_NAME=veganDB (Can be arbitrary)
- Run `createdb veganDB`
(To delete in future just run dropdb <name>)

View file

9
cmd/server/main.go Normal file
View file

@ -0,0 +1,9 @@
package main
import (
"vegan-barcode/internal/api"
)
func main() {
api.BindRoutes()
}

13
go.mod
View file

@ -1,7 +1,13 @@
module vegan-barcode/backend
module vegan-barcode
go 1.24.1
require (
github.com/gin-gonic/gin v1.10.0
github.com/labstack/gommon v0.4.2
github.com/sirupsen/logrus v1.9.3
)
require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
@ -10,7 +16,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.10.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
@ -27,15 +32,17 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.31.0 // indirect

16
go.sum
View file

@ -53,6 +53,8 @@ github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
@ -74,6 +76,7 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
@ -151,6 +154,8 @@ github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
@ -161,9 +166,12 @@ github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
@ -210,7 +218,6 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
@ -229,7 +236,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
@ -239,6 +245,10 @@ github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/vingarcia/ksql v1.12.3 h1:1LVRGW39XPaYltPHNQsvHms+bWHp8e99sxQx+aEXDMQ=
github.com/vingarcia/ksql v1.12.3/go.mod h1:DHp/nhVu1nHpBBXH/FRw6JLgIcvcM3+uo2+PfUNdo0g=
github.com/vingarcia/ksql/adapters/kpgx v1.12.3 h1:vzH0vMw1NTCang2DcZI9sV975BFUg5ONQjEZ27tuCng=
@ -341,6 +351,7 @@ golang.org/x/sys v0.0.0-20220405210540-1e041c57c461/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@ -390,6 +401,7 @@ golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=

65
internal/api/routes.go Normal file
View file

@ -0,0 +1,65 @@
package api
import (
"net/http"
"vegan-barcode/internal/database"
"vegan-barcode/internal/utils"
"github.com/gin-gonic/gin"
"github.com/labstack/gommon/log"
"github.com/sirupsen/logrus"
"github.com/vingarcia/ksql"
)
// TODO Figure out where this should be
// This exists so that you don't have to individually pass around the logger and database.
type ApiService struct {
db *ksql.DB
log *logrus.Logger
}
var s *ApiService
// TODO: Service should get moved somewhere else. Not sure on naming.
func BindRoutes() {
s = &ApiService{db: database.InitializeDatabase(), log: utils.InitializeLogger()}
// s = &ApiService{db: &ksql.DB{}, log: utils.InitializeLogger()}
router := gin.Default()
router.GET("/test/:id", s.runTest)
router.GET("/", s.runTest)
// // Search for item info
// router.GET("/claims/{barcode}", runTest)
// // by user ID or worker
// router.DELETE("/claims/{barcode}", deleteClaims)
// // Update item info
// router.GET("/test", runTest)
// // Add new item info
router.Run("localhost:8080")
}
func (s *ApiService) runTest(c *gin.Context) {
queryParam := c.Param("id")

what is the goal of this api folder, is it to:
a. bind API routes to functions
or b. parse out the parameters of our queries to then pass to the called service?

I think it would be fine to have both of these in the same folder but it might get to be a bit messy to have both of them in one place. Ideally a would live somewhere related to our swagger.yml file but that's just in the directory above us right now. Maybe we can move the swagger file into this folder, then take runTest (or its future non test equivalents) and create a new folder to host them. For Kotlin projects that I have worked on generally gets a controllers folder but I'm open to other names if you can thing of something that makes more sense to you

what is the goal of this `api` folder, is it to: a. bind API routes to functions or b. parse out the parameters of our queries to then pass to the called service? I think it would be fine to have both of these in the same folder but it might get to be a bit messy to have both of them in one place. Ideally a would live somewhere related to our `swagger.yml` file but that's just in the directory above us right now. Maybe we can move the swagger file into this folder, then take `runTest` (or its future non test equivalents) and create a new folder to host them. For Kotlin projects that I have worked on generally gets a `controllers` folder but I'm open to other names if you can thing of something that makes more sense to you
log.Debug("Test was successful.")
c.JSON(http.StatusOK, gin.H{"message": "Hello World!", "query_param": queryParam})
}
terrabytten marked this conversation as resolved Outdated

We shouldnt really be making DB calls here generally best practices would be to mak some function in our database folder that for example is called createProduct that we would just call with our System, and Barcode value. Then we would also want to have that database::createProduct call done within our services folder (to keep a separation of our network layer and our business logic) but that ones not as big of a deal because this is just a test function that doesnt really have a defined goal in mind

We shouldnt really be making DB calls here generally best practices would be to mak some function in our database folder that for example is called `createProduct` that we would just call with our `System`, and `Barcode` value. Then we would also want to have that `database::createProduct` call done within our `services` folder (to keep a separation of our network layer and our business logic) but that ones not as big of a deal because this is just a test function that doesnt really have a defined goal in mind
// func claimsByBarcode(c *gin.Context) {
// system := c.DefaultQuery("system", "upc")
// barcode := c.Query("barcode")

c.JSON I believe pipes out the passed data as a stringify value on whatever network socket the gin context has but doesnt close that connection so when we get an error we end up getting something roughly like error: xyz... success xyz... we want to close that connection and return early before we get to the success portion of this method

c.JSON I believe pipes out the passed data as a stringify value on whatever network socket the gin context has but doesnt close that connection so when we get an error we end up getting something roughly like `error: xyz... success xyz...` we want to close that connection and return early before we get to the success portion of this method
// }
// // deleteClaims will delete
// func deleteClaims(c *gin.Context) {
// userID := c.Param("user")
// workerID := c.Param("worker")
// // TODO query database
// database.DB.Query()
// c.JSON(http.StatusOK, gin.H{"message": "Hello World!"})
// }

View file

@ -1,19 +0,0 @@
package main
import "github.com/gin-gonic/gin"
func main() {
router := gin.Default()
router.GET("/test", runTest)
// Search for item info
router.GET("/claims/{barcode}", runTest)
router.DELETE("/claims/{barcode}", runTest)
// Update item info
router.GET("/test", runTest)
// Add new item info
// Delete item (debug)
router.Run("localhost:8080")
}

View file

@ -1,11 +1,11 @@
package main
package database
import (
"context"
"fmt"
"log"
"os"
"github.com/sirupsen/logrus"
"github.com/vingarcia/ksql"
"github.com/vingarcia/ksql/adapters/kpgx"
)
@ -16,24 +16,8 @@ var UserClaimsTable = ksql.NewTable("user_claims", "user_claim_id")
var AutomatedClaimsTable = ksql.NewTable("automated_claims", "automated_claim_id")
// log is the global error logging interface.
var log *logrus.Logger
func main() {
setupLogger()
// Do we want to handle the error here instead?
db := initializeDatabase()
}
func setupLogger() {
log = logrus.New()
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true, // Include the full timestamp (with date and time)
})
}
// initializeDatabase creates the database and then calls createTables.
func initializeDatabase() ksql.DB {
// initializeDatabase creates the database and calls createTables.
func InitializeDatabase() *ksql.DB {
ctx := context.Background()
// urlExample := "postgres://username:password@localhost:5432/database_name"
@ -46,8 +30,8 @@ func initializeDatabase() ksql.DB {
if err != nil {
log.Fatalf("Unable to connect to database: %v\n", err)
}
createTables(ctx, db)
return &db
}
// createTables adds the product, automated_claims, and user_claims tables to the initialized database.

View file

@ -1,4 +1,4 @@
package main
package database
import (
"time"

View file

@ -1,19 +1 @@
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/labstack/gommon/log"
)
func testService(c *gin.Context) {
log.Debug("Test was successful.")
c.JSON(http.StatusOK, gin.H{"message": "Hello World!"})
}
func claimsByBarcode(c *gin.Context) {
system := c.DefaultQuery("system", "upc")
barcode := c.Query("barcode")
}
package services

13
internal/utils/logger.go Normal file
View file

@ -0,0 +1,13 @@
package utils
import (
"github.com/sirupsen/logrus"
)
func InitializeLogger() *logrus.Logger {
log := logrus.New()
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true, // Include the full timestamp (with date and time)
})
return log
}