Skip to content

Commit

Permalink
Cleanup master
Browse files Browse the repository at this point in the history
  • Loading branch information
jpahm committed Nov 1, 2023
1 parent 797a2b0 commit 21bcdb5
Show file tree
Hide file tree
Showing 52 changed files with 160 additions and 41,040 deletions.
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

12 changes: 0 additions & 12 deletions .eslintrc.js

This file was deleted.

57 changes: 57 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
workflow_dispatch:

jobs:

build_ubuntu:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./api
steps:

- name: Checkout
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
cache-dependency-path: "**/go.sum"

- name: Setup
run: make setup

- name: Check
run: make check

- name: Build
run: make build

build_windows:
runs-on: windows-latest
defaults:
run:
working-directory: .\api
steps:

- name: Checkout
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
cache-dependency-path: "**\\go.sum"

- name: Check & Build
run: .\build.bat
38 changes: 0 additions & 38 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,54 +1,16 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.production
.env.development.local
.env.test.local
.env
/api/.env

# vercel
.vercel

# custom
misc/
deploy_log.sh
.gcloudignore
.idea/
.vscode/
.firebase/

# webscraper
/scraper/.env
/scraper/credentials.json
/scraper/scraper.log

# Golang

# Binaries for programs and plugins
*.exe
Expand Down
1 change: 0 additions & 1 deletion .husky/.gitignore

This file was deleted.

4 changes: 0 additions & 4 deletions .husky/pre-commit

This file was deleted.

4 changes: 0 additions & 4 deletions .husky/prepare-commit-msg

This file was deleted.

2 changes: 0 additions & 2 deletions .prettierignore

This file was deleted.

7 changes: 0 additions & 7 deletions .prettierrc.js

This file was deleted.

5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ See the [docs](docs/index.md) for more information about the project.

Clone the repository.

- The `scraper` directory contains scripts to obtain data.
- The `api` directory contains infrastructure for endpoints to query data.

#### Scraper

[TBD]

#### API (Under construction)

The API uses Golang with Gin and the MongoDB Golang Driver.
Expand Down
27 changes: 27 additions & 0 deletions api/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

EXEC_NAME?=go-api
DOCKER_RUNNER?=docker
REGISTRY?=localhost:5000
RELEASE_TAG=$(shell git rev-parse --short HEAD)
BASE_TAG=$(REGISTRY)/utdnebula/api/go-api

setup:
go install honnef.co/go/tools/cmd/staticcheck@latest
go install golang.org/x/tools/cmd/goimports@latest

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

build: server.go
go build -o $(EXEC_NAME) server.go

clean: $(EXEC_NAME)
rm $(EXEC_NAME)

docker: Dockerfile configs/ controllers/ models/ responses/ routes/ server.go
$(DOCKER_RUNNER) build -t $(BASE_TAG):$(RELEASE_TAG) .
$(DOCKER_RUNNER) tag $(BASE_TAG):$(RELEASE_TAG) $(BASE_TAG):latest
29 changes: 29 additions & 0 deletions api/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@echo off

::vars
set EXEC_NAME=go-api.exe

::setup
echo Performing setup...
go install honnef.co/go/tools/cmd/staticcheck@latest && ^
go install golang.org/x/tools/cmd/goimports@latest
if ERRORLEVEL 1 exit /b %ERRORLEVEL% :: fail if error occurred
echo Setup done!
echo[

::checks
echo Performing checks...
go mod tidy && ^
go vet ./... && ^
staticcheck ./... && ^
gofmt -w ./.. && ^
goimports -w ./..
if ERRORLEVEL 1 exit /b %ERRORLEVEL% :: fail if error occurred
echo Checks done!
echo[

::build
echo Building...
go build -o %EXEC_NAME% server.go
if ERRORLEVEL 1 exit /b %ERRORLEVEL% :: fail if error occurred
echo Build complete!
44 changes: 24 additions & 20 deletions api/configs/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,53 @@ import (
func ConnectDB() *mongo.Client {
client, err := mongo.NewClient(options.Client().ApplyURI(EnvMongoURI()))
if err != nil {
log.Fatal("Unable to create MongoDB client: %v", err)
log.Fatalf("Unable to create MongoDB client: %v", err)
}

ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancelFnc := context.WithTimeout(context.Background(), 10*time.Second)

// TODO: Actually use cancelFnc in the rewrite
_ = cancelFnc

err = client.Connect(ctx)
if err != nil {
log.Fatal("Unable to connect to database: %v", err)
log.Fatalf("Unable to connect to database: %v", err)
}

//ping the database
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal("Unable to ping database: %v", err)
log.Fatalf("Unable to ping database: %v", err)
}
fmt.Println("Connected to MongoDB")
return client
}

//Client instance
// Client instance
var DB *mongo.Client = ConnectDB()

//getting database collections
// getting database collections
func GetCollection(client *mongo.Client, collectionName string) *mongo.Collection {
collection := client.Database("combinedDB").Collection(collectionName)
return collection
}

// Returns *options.FindOptions with a limit and offset applied. Returns error if any
func GetOptionLimit(query *bson.M, c *gin.Context) (*options.FindOptions, error){
delete(*query, "offset") // removes offset (if present) in query --offset is not field in collections
func GetOptionLimit(query *bson.M, c *gin.Context) (*options.FindOptions, error) {
delete(*query, "offset") // removes offset (if present) in query --offset is not field in collections

// parses offset if included in the query
var offset int64;
var err error

if c.Query("offset") == "" {
offset = 0 // default value for offset
} else {
offset, err = strconv.ParseInt(c.Query("offset"), 10, 64)
if err != nil {
return options.Find().SetSkip(0).SetLimit(Limit), err // default value for offset
}
// parses offset if included in the query
var offset int64
var err error

if c.Query("offset") == "" {
offset = 0 // default value for offset
} else {
offset, err = strconv.ParseInt(c.Query("offset"), 10, 64)
if err != nil {
return options.Find().SetSkip(0).SetLimit(Limit), err // default value for offset
}
}

return options.Find().SetSkip(offset).SetLimit(Limit), err
return options.Find().SetSkip(offset).SetLimit(Limit), err
}
7 changes: 4 additions & 3 deletions api/controllers/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ func CourseSearch() gin.HandlerFunc {

// build query key value pairs (only one value per key)
query := bson.M{}
for key, _ := range queryParams {
for key := range queryParams {
query[key] = c.Query(key)
}

optionLimit, err := configs.GetOptionLimit(&query, c); if err != nil {
optionLimit, err := configs.GetOptionLimit(&query, c)
if err != nil {
c.JSON(http.StatusConflict, responses.CourseResponse{Status: http.StatusConflict, Message: "Error offset is not type integer", Data: err.Error()})
return
}
Expand Down Expand Up @@ -70,7 +71,7 @@ func CourseById() gin.HandlerFunc {

// parse object id from id parameter
objId, err := primitive.ObjectIDFromHex(courseId)
if err != nil{
if err != nil {
c.JSON(http.StatusBadRequest, responses.CourseResponse{Status: http.StatusBadRequest, Message: "error", Data: err.Error()})
return
}
Expand Down
7 changes: 4 additions & 3 deletions api/controllers/degree.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ func DegreeSearch() gin.HandlerFunc {

// build query key value pairs (only one value per key)
query := bson.M{}
for key, _ := range queryParams {
for key := range queryParams {
query[key] = c.Query(key)
}

optionLimit, err := configs.GetOptionLimit(&query, c); if err != nil {
optionLimit, err := configs.GetOptionLimit(&query, c)
if err != nil {
c.JSON(http.StatusConflict, responses.DegreeResponse{Status: http.StatusConflict, Message: "Error offset is not type integer", Data: err.Error()})
return
}
Expand Down Expand Up @@ -72,7 +73,7 @@ func DegreeById() gin.HandlerFunc {

// parse object id from id parameter
objId, err := primitive.ObjectIDFromHex(degreeId)
if err != nil{
if err != nil {
c.JSON(http.StatusBadRequest, responses.CourseResponse{Status: http.StatusBadRequest, Message: "error", Data: err.Error()})
return
}
Expand Down
15 changes: 9 additions & 6 deletions api/controllers/exam.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ func ExamSearch() gin.HandlerFunc {

// build query key value pairs (only one value per key)
query := bson.M{}
for key, _ := range queryParams {
for key := range queryParams {
query[key] = c.Query(key)
}

optionLimit, err := configs.GetOptionLimit(&query, c); if err != nil {
optionLimit, err := configs.GetOptionLimit(&query, c)
if err != nil {
c.JSON(http.StatusConflict, responses.ExamResponse{Status: http.StatusConflict, Message: "Error offset is not type integer", Data: err.Error()})
return
}
Expand Down Expand Up @@ -72,7 +73,7 @@ func ExamById() gin.HandlerFunc {

// parse object id from id parameter
objId, err := primitive.ObjectIDFromHex(examId)
if err != nil{
if err != nil {
c.JSON(http.StatusBadRequest, responses.ExamResponse{Status: http.StatusBadRequest, Message: "error", Data: err.Error()})
return
}
Expand All @@ -96,9 +97,10 @@ func ExamAll() gin.HandlerFunc {
// @TODO: Fix with model - There is NO typechecking!
var courses []map[string]interface{}

defer cancel();
defer cancel()

optionLimit, err := configs.GetOptionLimit(&bson.M{}, c); if err != nil {
optionLimit, err := configs.GetOptionLimit(&bson.M{}, c)
if err != nil {
c.JSON(http.StatusConflict, responses.ExamResponse{Status: http.StatusConflict, Message: "Error offset is not type integer", Data: err.Error()})
return
}
Expand All @@ -111,7 +113,8 @@ func ExamAll() gin.HandlerFunc {
}

// retrieve and parse all valid documents
err = cursor.All(ctx, &courses); if err != nil {
err = cursor.All(ctx, &courses)
if err != nil {
panic(err)
}

Expand Down
Loading

0 comments on commit 21bcdb5

Please sign in to comment.