diff --git a/README.md b/README.md index 85b3fb2..1d771cd 100644 --- a/README.md +++ b/README.md @@ -13,4 +13,19 @@ The goal of this project is to be a crowd sourced resource to find out if a prod - barnivore lookup - alergen ingredient database lookup? - create mobile and desktop front ends -- moderation tooling? \ No newline at end of file +- 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 ) + diff --git a/cmd/main.go b/cmd/main.go deleted file mode 100644 index e69de29..0000000 diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..426e8ff --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "vegan-barcode/internal/api" +) + +func main() { + api.BindRoutes() +} diff --git a/go.mod b/go.mod index 07191b7..8b19069 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e680649..3599f7d 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/api/routes.go b/internal/api/routes.go new file mode 100644 index 0000000..97eefc9 --- /dev/null +++ b/internal/api/routes.go @@ -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") + log.Debug("Test was successful.") + c.JSON(http.StatusOK, gin.H{"message": "Hello World!", "query_param": queryParam}) +} + +// func claimsByBarcode(c *gin.Context) { +// system := c.DefaultQuery("system", "upc") +// barcode := c.Query("barcode") + +// } + +// // 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!"}) +// } diff --git a/internal/controllers/routes.go b/internal/controllers/routes.go deleted file mode 100644 index 7e5e873..0000000 --- a/internal/controllers/routes.go +++ /dev/null @@ -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") -} diff --git a/internal/database/database.go b/internal/database/database.go index 46e2cfa..6bdf77d 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -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. diff --git a/internal/database/models.go b/internal/database/models.go index 86d4178..9a7ca59 100644 --- a/internal/database/models.go +++ b/internal/database/models.go @@ -1,4 +1,4 @@ -package main +package database import ( "time" diff --git a/internal/services/userService.go b/internal/services/userService.go index 3de7c0a..5e568ea 100644 --- a/internal/services/userService.go +++ b/internal/services/userService.go @@ -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 diff --git a/internal/utils/logger.go b/internal/utils/logger.go new file mode 100644 index 0000000..a83abc3 --- /dev/null +++ b/internal/utils/logger.go @@ -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 +}