Skip to content
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
Binary file added .github/.DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ go.work.sum
semesters.json

# API executables
rest-api
rest-api

# mac
**/.DS_Store
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ setup:
go install honnef.co/go/tools/cmd/staticcheck@latest
go install golang.org/x/tools/cmd/goimports@latest
go install github.com/swaggo/swag/cmd/swag@latest
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.15.0

check:
go mod tidy
go vet ./...
staticcheck ./...
gofmt -w .
goimports -w .
golangci-lint run ./...

test:
go test ./... -count=1
Expand Down
4 changes: 4 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ go install golang.org/x/tools/cmd/goimports@latest
if ERRORLEVEL 1 exit /b %ERRORLEVEL%
go install github.com/swaggo/swag/cmd/swag@latest
if ERRORLEVEL 1 exit /b %ERRORLEVEL%
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.15.0
if ERRORLEVEL 1 exit /b %ERRORLEVEL%
echo Setup done!
echo.
exit /b 0
Expand All @@ -66,6 +68,8 @@ gofmt -w .
if ERRORLEVEL 1 exit /b %ERRORLEVEL%
goimports -w .
if ERRORLEVEL 1 exit /b %ERRORLEVEL%
golangci-lint run ./...
if ERRORLEVEL 1 exit /b %ERRORLEVEL%
echo Checks done!
echo.
exit /b 0
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
cloud.google.com/go/compute/metadata v0.9.0 // indirect
cloud.google.com/go/iam v1.13.0 // indirect
cloud.google.com/go/monitoring v1.30.0 // indirect
cloud.google.com/go/pubsub/v2 v2.6.2 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.36.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.60.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.60.0 // indirect
Expand All @@ -35,6 +36,7 @@ require (
github.com/envoyproxy/go-control-plane/envoy v1.39.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.3.3 // indirect
github.com/felixge/httpsnoop v1.1.0 // indirect
github.com/fsouza/fake-gcs-server v1.56.1 // indirect
github.com/go-jose/go-jose/v4 v4.1.6 // indirect
github.com/go-logr/logr v1.4.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -43,17 +45,22 @@ require (
github.com/go-openapi/spec v0.22.9 // indirect
github.com/go-openapi/swag/pools v0.29.1 // indirect
github.com/goccy/go-yaml v1.19.2 // indirect
github.com/google/renameio/v2 v2.0.0 // indirect
github.com/google/s2a-go v0.1.9 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.21 // indirect
github.com/googleapis/gax-go/v2 v2.24.0 // indirect
github.com/gorilla/handlers v1.5.2 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/pkg/xattr v0.4.12 // indirect
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
github.com/quic-go/qpack v0.6.0 // indirect
github.com/quic-go/quic-go v0.61.0 // indirect
github.com/spiffe/go-spiffe/v2 v2.8.1 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/detectors/gcp v1.45.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.70.0 // indirect
Expand Down
89 changes: 89 additions & 0 deletions go.sum

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions rest/configs/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ var fatalCases = map[string]func(){
"mongo_uri": func() { GetEnvMongoURI() },
"login_netid": func() { GetEnvLogin() },
"login_password": func() {
os.Setenv("LOGIN_NETID", "abc123456")
if err := os.Setenv("LOGIN_NETID", "abc123456"); err != nil {
log.Fatalf("Error setting 'LOGIN_NETID'")
}
GetEnvLogin()
},
}
Expand All @@ -33,7 +35,10 @@ func unsetEnv(t *testing.T, key string) {
// t.Setenv records the original value so it is restored on cleanup; the
// unset immediately afterwards is what the test actually wants.
t.Setenv(key, "")
os.Unsetenv(key)
if err := os.Unsetenv(key); err != nil {
t.Fatalf("Error unsetting key")
}

}

// TestEnvFatalSubprocess is the entry point for the subprocesses spawned by
Expand Down
5 changes: 3 additions & 2 deletions rest/configs/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func GetOptionLimit(query *bson.M, c *gin.Context) (*options.FindOptionsBuilder,
var offset int64
var err error

var limit int64 = GetEnvLimit()
var limit = GetEnvLimit()

if c.Query("offset") == "" {
offset = 0 // default value for offset
Expand Down Expand Up @@ -137,7 +137,8 @@ func ConnectClubsDB() *sql.DB {
// ping the database
err = db.PingContext(ctx)
if err != nil {
log.Panic("Unable to ping database.")
log.Panic("Unable to ping database")
return
}

log.Println("Connected to Clubs DB.")
Expand Down
5 changes: 2 additions & 3 deletions rest/controllers/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controllers

import (
"context"
"database/sql"
"net/http"
"time"

Expand All @@ -26,7 +25,7 @@ func ClubDirectoryInfo(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
defer cancel()

var clubsDatabase *sql.DB = configs.ConnectClubsDB()
var clubsDatabase = configs.ConnectClubsDB()
id := c.Param("id")

var raw []byte
Expand Down Expand Up @@ -90,7 +89,7 @@ func ClubSearch(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
defer cancel()

var clubsDatabase *sql.DB = configs.ConnectClubsDB()
var clubsDatabase = configs.ConnectClubsDB()
search := c.Query("q")

var raw []byte
Expand Down
2 changes: 1 addition & 1 deletion rest/controllers/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func getQuery[T any](flag string, c *gin.Context) (bson.M, error) {
func respondWithInternalError(c *gin.Context, err error) {
// Note that we use log.Output here to be able to set the stack depth to the frame above this one (2),
// which allows us to log the location this function was called from
log.Output(2, fmt.Sprintf("INTERNAL SERVER ERROR: %s", err.Error()))
_ = log.Output(2, fmt.Sprintf("INTERNAL SERVER ERROR: %s", err.Error()))
// Capture error with Sentry
if hub := sentrygin.GetHubFromContext(c); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
Expand Down
6 changes: 3 additions & 3 deletions rest/controllers/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func CourseSearch(c *gin.Context) {
respondWithInternalError(c, err)
return
}
defer cursor.Close(ctx)
defer func() { _ = cursor.Close(ctx) }()

// Retrieve and parse all valid documents
if err = cursor.All(ctx, &courses); err != nil {
Expand Down Expand Up @@ -129,7 +129,7 @@ func CourseAll(c *gin.Context) {
respondWithInternalError(c, err)
return
}
defer cursor.Close(ctx)
defer func() { _ = cursor.Close(ctx) }()

// retrieve and parse all valid documents
if err = cursor.All(ctx, &courses); err != nil {
Expand Down Expand Up @@ -262,7 +262,7 @@ func courseAggregate[T any](flag string, c *gin.Context) {
respondWithInternalError(c, err)
return
}
defer cursor.Close(ctx)
defer func() { _ = cursor.Close(ctx) }()

if err = cursor.All(ctx, &queryResults); err != nil {
respondWithInternalError(c, err)
Expand Down
2 changes: 1 addition & 1 deletion rest/controllers/degrees.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Degrees(c *gin.Context) {
respondWithInternalError(c, err)
return
}
defer cursor.Close(ctx)
defer func() { _ = cursor.Close(ctx) }()

degrees := make([]schema.AcademicProgram, 0)
if err = cursor.All(ctx, &degrees); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion rest/controllers/discounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func DiscountSearch(c *gin.Context) {
return
}
}
defer cursor.Close(ctx)
defer func() { _ = cursor.Close(ctx) }()

discounts := make([]schema.DiscountProgram, 0)
if err = cursor.All(ctx, &discounts); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion rest/controllers/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func SectionsByRoomDetailed(c *gin.Context) {
respondWithInternalError(c, err)
return
}
defer cursor.Close(ctx)
defer func() { _ = cursor.Close(ctx) }()

if err = cursor.All(ctx, &sectionsByRoom.Events); err != nil {
respondWithInternalError(c, err)
Expand Down
27 changes: 19 additions & 8 deletions rest/controllers/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,41 @@ import (
"strings"
"testing"

"cloud.google.com/go/storage"
"github.com/fsouza/fake-gcs-server/fakestorage"
"github.com/gin-gonic/gin"
)

func TestMaxUploadSize(t *testing.T) {
// Set the environment variable for max upload size (e.g., 100 bytes)
os.Setenv("MAX_UPLOAD_SIZE", "100")
defer os.Unsetenv("MAX_UPLOAD_SIZE")

if err := os.Setenv("MAX_UPLOAD_SIZE", "100"); err != nil {
t.Fatalf("Error setting 'MAX_UPLOAD_SIZE'")
}

defer func() {
if err := os.Unsetenv("MAX_UPLOAD_SIZE"); err != nil {
t.Fatalf("Error unsetting 'MAX_UPLOAD_SIZE'")
}
}()

server := fakestorage.NewServer(nil)
defer server.Stop()
client := server.Client()

// Setup Gin
gin.SetMode(gin.TestMode)
router := gin.New()

router.POST("/storage/:bucket/:objectID", func(c *gin.Context) {

c.Set("gcsClient", &storage.Client{})
c.Set("gcsClient", client)
PostObject(c)
})

t.Run("Upload within limit", func(t *testing.T) {

defer func() {
if r := recover(); r != nil {

t.Fatalf("Recover panicked unexpectedly: %v", r)
}
}()

Expand Down Expand Up @@ -67,8 +78,8 @@ func TestMaxUploadSize(t *testing.T) {
t.Run("Upload exceeding limit via Stream", func(t *testing.T) {
pr, pw := io.Pipe()
go func() {
pw.Write(make([]byte, 150))
pw.Close()
_, _ = pw.Write(make([]byte, 150))
_ = pw.Close()
}()

req, _ := http.NewRequest("POST", "/storage/test-bucket/stream-file", pr)
Expand Down
2 changes: 1 addition & 1 deletion rest/controllers/trends.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func trendsSectionSearch(flag string, c *gin.Context) {
respondWithInternalError(c, err)
return
}
defer cursor.Close(ctx)
defer func() { _ = cursor.Close(ctx) }()

if err := cursor.All(ctx, &detailedSections); err != nil {
respondWithInternalError(c, err)
Expand Down
5 changes: 3 additions & 2 deletions rest/schema/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ func recBuild(t reflect.Type, prefix string, queryableMap map[string]bool, visit
// Pointer types are allowed to recur once, i.e., only considered a loop when
// the same pointer type appears twice in a row.
func willCreateLoop(visited []reflect.Type, value reflect.Type) bool {
if value.Kind() != reflect.Ptr {
if value.Kind() !=
reflect.Pointer {
return false
}

Expand All @@ -155,7 +156,7 @@ func willCreateLoop(visited []reflect.Type, value reflect.Type) bool {

// drillType gets the base of a type, removing pointers and slices/arrays
func drillType(t reflect.Type) reflect.Type {
for t.Kind() == reflect.Ptr || t.Kind() == reflect.Slice || t.Kind() == reflect.Array {
for t.Kind() == reflect.Pointer || t.Kind() == reflect.Slice || t.Kind() == reflect.Array {
t = t.Elem()
}
return t
Expand Down
48 changes: 36 additions & 12 deletions rest/schema/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,51 +113,75 @@ func (cr *CollectionRequirement) UnmarshalBSON(data []byte) error {
switch optionType {
case "course":
var t CourseRequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
case "section":
var t SectionRequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
case "major":
var t MajorRequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
case "minor":
var t MinorRequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
case "gpa":
var t GPARequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
case "consent":
var t ConsentRequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
case "collection":
var t CollectionRequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
case "hours":
var t HoursRequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
case "other":
var t OtherRequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
case "choice":
var t ChoiceRequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
case "limit":
var t LimitRequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
case "core":
var t CoreRequirement
bson.Unmarshal(bytes, &t)
if err := bson.Unmarshal(bytes, &t); err != nil {
return err
}
out = append(out, t)
default:
return fmt.Errorf("unknown option type: %v", err)
Expand Down
Loading
Loading