Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Club Vector Embeddings #180

Merged
merged 15 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ issues:
- path: tests/api/helpers/
linters:
- cyclop
- path: src/config/production.go
linters:
- cyclop
14 changes: 10 additions & 4 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/viper v1.18.2
github.com/swaggo/swag v1.16.3
golang.org/x/crypto v0.19.0
golang.org/x/text v0.14.0
gorm.io/driver/postgres v1.5.6
gorm.io/gorm v1.25.7
gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.6
)

require (
github.com/awnumar/memcall v0.2.0 // indirect
github.com/awnumar/memguard v0.22.4 // indirect
github.com/h2non/gock v1.2.0 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
)

require (
Expand All @@ -43,6 +48,7 @@ require (
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
Expand Down
9 changes: 9 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE=
github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/huandu/go-assert v1.1.6 h1:oaAfYxq9KNDi9qswn/6aE0EydfxSa+tWZC1KabNitYs=
Expand All @@ -67,6 +71,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
Expand Down Expand Up @@ -97,6 +103,7 @@ github.com/mcnijman/go-emailaddress v1.1.1 h1:AGhgVDG3tCDaL0/Vc6erlPQjDuDN3dAT7r
github.com/mcnijman/go-emailaddress v1.1.1/go.mod h1:5whZrhS8Xp5LxO8zOD35BC+b76kROtsh+dPomeRt/II=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
Expand Down Expand Up @@ -186,7 +193,9 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0=
gorm.io/driver/postgres v1.5.6 h1:ydr9xEd5YAM0vxVDY0X139dyzNz10spDiDlC7+ibLeU=
gorm.io/driver/postgres v1.5.6/go.mod h1:3e019WlBaYI5o5LIdNV+LyxCMNtLOQETBXL2h4chKpA=
gorm.io/gorm v1.25.6/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package auth

import (
"github.com/GenerateNU/sac/backend/src/errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package types
package auth

import "github.com/GenerateNU/sac/backend/src/models"

Expand Down Expand Up @@ -85,7 +85,6 @@ var rolePermissions = map[models.UserRole][]Permission{
},
}

// Returns the permissions for a given role
func GetPermissions(role models.UserRole) []Permission {
return rolePermissions[role]
}
11 changes: 5 additions & 6 deletions backend/src/auth/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/GenerateNU/sac/backend/src/config"
"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/types"

m "github.com/garrettladley/mattress"
"github.com/gofiber/fiber/v2"
Expand All @@ -32,7 +31,7 @@ func CreateAccessToken(id string, role string, accessExpiresAfter uint, accessTo
return nil, &errors.FailedToCreateAccessToken
}

accessTokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, &types.CustomClaims{
accessTokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, &CustomClaims{
StandardClaims: jwt.StandardClaims{
IssuedAt: time.Now().Unix(),
Issuer: id,
Expand Down Expand Up @@ -126,7 +125,7 @@ func RefreshAccessToken(refreshCookie string, role string, refreshKey *m.Secret[

// ParseAccessToken parses the access token
func ParseAccessToken(cookie string, accessKey *m.Secret[string]) (*jwt.Token, error) {
return jwt.ParseWithClaims(cookie, &types.CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return jwt.ParseWithClaims(cookie, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(accessKey.Expose()), nil
})
}
Expand All @@ -145,7 +144,7 @@ func GetRoleFromToken(tokenString string, accessKey *m.Secret[string]) (*string,
return nil, err
}

claims, ok := token.Claims.(*types.CustomClaims)
claims, ok := token.Claims.(*CustomClaims)
if !ok || !token.Valid {
return nil, &errors.FailedToValidateAccessToken
}
Expand All @@ -154,13 +153,13 @@ func GetRoleFromToken(tokenString string, accessKey *m.Secret[string]) (*string,
}

// ExtractClaims extracts the claims from the token
func ExtractAccessClaims(tokenString string, accessKey *m.Secret[string]) (*types.CustomClaims, *errors.Error) {
func ExtractAccessClaims(tokenString string, accessKey *m.Secret[string]) (*CustomClaims, *errors.Error) {
token, err := ParseAccessToken(tokenString, accessKey)
if err != nil {
return nil, &errors.FailedToParseAccessToken
}

claims, ok := token.Claims.(*types.CustomClaims)
claims, ok := token.Claims.(*CustomClaims)
if !ok || !token.Valid {
return nil, &errors.FailedToValidateAccessToken
}
Expand Down
12 changes: 7 additions & 5 deletions backend/src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
)

type Settings struct {
Application ApplicationSettings
Database DatabaseSettings
SuperUser SuperUserSettings
Auth AuthSettings
Application ApplicationSettings
Database DatabaseSettings
SuperUser SuperUserSettings
Auth AuthSettings
PineconeSettings PineconeSettings
OpenAISettings OpenAISettings
}

type intermediateSettings struct {
Expand Down Expand Up @@ -64,7 +66,7 @@ func GetConfiguration(path string) (*Settings, error) {
v.AddConfigPath(path)

if environment == EnvironmentLocal {
return readLocal(v)
return readLocal(v, path)
} else {
return readProd(v)
}
Expand Down
22 changes: 21 additions & 1 deletion backend/src/config/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package config
import (
"fmt"

"github.com/joho/godotenv"
"github.com/spf13/viper"
)

func readLocal(v *viper.Viper) (*Settings, error) {
func readLocal(v *viper.Viper, path string) (*Settings, error) {
var intermediateSettings intermediateSettings

env := string(EnvironmentLocal)
Expand All @@ -26,5 +27,24 @@ func readLocal(v *viper.Viper) (*Settings, error) {
return nil, fmt.Errorf("failed to convert intermediate settings into final settings: %w", err)
}

err = godotenv.Load(fmt.Sprintf("%s/.example_backend_env", path))
if err != nil {
return nil, fmt.Errorf("failed to load %s/.example_backend_env: %w", path, err)
}

pineconeSettings, err := readPineconeSettings()
if err != nil {
return nil, fmt.Errorf("failed to read Pinecone settings: %w", err)
}

settings.PineconeSettings = *pineconeSettings

openAISettings, err := readOpenAISettings()
if err != nil {
return nil, fmt.Errorf("failed to read OpenAI settings: %w", err)
}

settings.OpenAISettings = *openAISettings

return settings, nil
}
28 changes: 28 additions & 0 deletions backend/src/config/openai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package config

import (
"errors"
"os"

m "github.com/garrettladley/mattress"
)

type OpenAISettings struct {
APIKey *m.Secret[string]
}

func readOpenAISettings() (*OpenAISettings, error) {
apiKey := os.Getenv("SAC_OPENAI_API_KEY")
if apiKey == "" {
return nil, errors.New("SAC_OPENAI_API_KEY is not set")
}

secretAPIKey, err := m.NewSecret(apiKey)
if err != nil {
return nil, errors.New("failed to create secret from api key")
}

return &OpenAISettings{
APIKey: secretAPIKey,
}, nil
}
40 changes: 40 additions & 0 deletions backend/src/config/pinecone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

import (
"errors"
"os"

m "github.com/garrettladley/mattress"
)

type PineconeSettings struct {
IndexHost *m.Secret[string]
APIKey *m.Secret[string]
}

func readPineconeSettings() (*PineconeSettings, error) {
indexHost := os.Getenv("SAC_PINECONE_INDEX_HOST")
if indexHost == "" {
return nil, errors.New("SAC_PINECONE_INDEX_HOST is not set")
}

secretIndexHost, err := m.NewSecret(indexHost)
if err != nil {
return nil, errors.New("failed to create secret from index host")
}

apiKey := os.Getenv("SAC_PINECONE_API_KEY")
if apiKey == "" {
return nil, errors.New("SAC_PINECONE_API_KEY is not set")
}

secretAPIKey, err := m.NewSecret(apiKey)
if err != nil {
return nil, errors.New("failed to create secret from api key")
}

return &PineconeSettings{
IndexHost: secretIndexHost,
APIKey: secretAPIKey,
}, nil
}
18 changes: 18 additions & 0 deletions backend/src/config/production.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"

m "github.com/garrettladley/mattress"
"github.com/joho/godotenv"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -39,6 +40,11 @@ func readProd(v *viper.Viper) (*Settings, error) {
return nil, fmt.Errorf("failed to unmarshal configuration: %w", err)
}

err := godotenv.Load(".env")
if err != nil {
return nil, fmt.Errorf("failed to load .env: %w", err)
}

appPrefix := "APP_"
applicationPrefix := fmt.Sprintf("%sAPPLICATION__", appPrefix)
dbPrefix := fmt.Sprintf("%sDATABASE__", appPrefix)
Expand Down Expand Up @@ -84,6 +90,16 @@ func readProd(v *viper.Viper) (*Settings, error) {
return nil, errors.New("failed to create secret from refresh token")
}

pineconeSettings, err := readPineconeSettings()
if err != nil {
return nil, fmt.Errorf("failed to read Pinecone settings: %w", err)
}

openAISettings, err := readOpenAISettings()
if err != nil {
return nil, fmt.Errorf("failed to read OpenAI settings: %w", err)
}

return &Settings{
Application: ApplicationSettings{
Port: uint16(portInt),
Expand All @@ -107,5 +123,7 @@ func readProd(v *viper.Viper) (*Settings, error) {
AccessTokenExpiry: uint(authAccessExpiryInt),
RefreshTokenExpiry: uint(authRefreshExpiryInt),
},
PineconeSettings: *pineconeSettings,
OpenAISettings: *openAISettings,
}, nil
}
5 changes: 2 additions & 3 deletions backend/src/controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/GenerateNU/sac/backend/src/types"
"github.com/GenerateNU/sac/backend/src/utilities"
"github.com/gofiber/fiber/v2"
)
Expand All @@ -34,7 +33,7 @@ func NewAuthController(authService services.AuthServiceInterface, authSettings c
// @Failure 401 {string} string "failed to get current user"
// @Router /api/v1/auth/me [get]
func (a *AuthController) Me(c *fiber.Ctx) error {
claims, err := types.From(c)
claims, err := auth.From(c)
if err != nil {
return err.FiberError(c)
}
Expand Down Expand Up @@ -167,7 +166,7 @@ func (a *AuthController) UpdatePassword(c *fiber.Ctx) error {
return errors.FailedToParseRequestBody.FiberError(c)
}

claims, err := types.From(c)
claims, err := auth.From(c)
if err != nil {
return err.FiberError(c)
}
Expand Down
4 changes: 4 additions & 0 deletions backend/src/errors/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ var (
StatusCode: fiber.StatusInternalServerError,
Message: "failed to get admin ids",
}
FailedToVectorizeClub = Error{
StatusCode: fiber.StatusInternalServerError,
Message: "failed to vectorize club",
}
FailedToGetClubFollowers = Error{
StatusCode: fiber.StatusInternalServerError,
Message: "failed to get club followers",
Expand Down
22 changes: 22 additions & 0 deletions backend/src/errors/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package errors

import "github.com/gofiber/fiber/v2"

var (
FailedToCreateEmbedding = Error{
StatusCode: fiber.StatusInternalServerError,
Message: "failed to create embedding from string",
}
FailedToUpsertToPinecone = Error{
StatusCode: fiber.StatusInternalServerError,
Message: "failed to upsert to pinecone",
}
FailedToDeleteToPinecone = Error{
StatusCode: fiber.StatusInternalServerError,
Message: "failed to delete from pinecone",
}
FailedToSearchToPinecone = Error{
StatusCode: fiber.StatusInternalServerError,
Message: "failed to search on pinecone",
}
)
Loading
Loading