Skip to content

Commit

Permalink
Merge pull request #13 from k-avy/kavya/api/test
Browse files Browse the repository at this point in the history
feat: updated project structure and added basic test cases to project
  • Loading branch information
GMishx authored Dec 4, 2023
2 parents 7c324a9 + e725b83 commit b324a4d
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 88 deletions.
50 changes: 7 additions & 43 deletions cmd/laas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"

"github.com/fossology/LicenseDb/pkg/api"
"github.com/fossology/LicenseDb/pkg/auth"
"github.com/fossology/LicenseDb/pkg/db"
"github.com/fossology/LicenseDb/pkg/models"
"github.com/fossology/LicenseDb/pkg/utils"
"github.com/gin-gonic/gin"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

// declare flags to input the basic requirement of database connection and the path of the data file
Expand All @@ -40,47 +33,18 @@ var (
func main() {
flag.Parse()

dburi := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s", *dbhost, *port, *user, *dbname, *password)
gormConfig := &gorm.Config{}
database, err := gorm.Open(postgres.Open(dburi), gormConfig)
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
db.Connect(dbhost, port, user, dbname, password)

if err := database.AutoMigrate(&models.LicenseDB{}); err != nil {
if err := db.DB.AutoMigrate(&models.LicenseDB{}); err != nil {
log.Fatalf("Failed to automigrate database: %v", err)
}

if err := database.AutoMigrate(&models.User{}); err != nil {
if err := db.DB.AutoMigrate(&models.User{}); err != nil {
log.Fatalf("Failed to automigrate database: %v", err)
}
if *populatedb {
var licenses []models.LicenseJson
// read the file of data
byteResult, _ := ioutil.ReadFile(*datafile)
// unmarshal the json file and it into the struct format
if err := json.Unmarshal(byteResult, &licenses); err != nil {
log.Fatalf("error reading from json file: %v", err)
}
for _, license := range licenses {
// populate the data in the database table
result := utils.Converter(license)
database.Create(&result)
}
}
api.DB = database

r := gin.Default()
r.NoRoute(api.HandleInvalidUrl)
authorized := r.Group("/")
authorized.Use(auth.AuthenticationMiddleware())
r.GET("/api/licenses/:shortname", api.GetLicense)
authorized.POST("/api/licenses", api.CreateLicense)
authorized.PATCH("/api/licenses/:shortname", api.UpdateLicense)
r.GET("/api/licenses", api.FilterLicense)
r.POST("/api/licenses/search", api.SearchInLicense)
authorized.POST("/api/users", auth.CreateUser)
authorized.GET("/api/users", auth.GetAllUser)
authorized.GET("/api/users/:id", auth.GetUser)
db.Populatedb(*populatedb, *datafile)

r := api.Router()
r.Run()
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ go 1.20

require (
github.com/gin-gonic/gin v1.9.1
github.com/stretchr/testify v1.8.3
gorm.io/driver/postgres v1.5.2
gorm.io/gorm v1.25.1
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand All @@ -30,6 +32,7 @@ require (
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.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
Expand Down
55 changes: 40 additions & 15 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,36 @@ import (
"net/http"
"time"

"github.com/fossology/LicenseDb/pkg/auth"
"github.com/fossology/LicenseDb/pkg/db"
"github.com/fossology/LicenseDb/pkg/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

var DB *gorm.DB
func Router() *gin.Engine {
// r is a default instance of gin engine
r := gin.Default()

// return error for invalid routes
r.NoRoute(HandleInvalidUrl)

// authorization not required for these routes
r.GET("/api/licenses/:shortname", GetLicense)
r.POST("/api/search", SearchInLicense)
r.GET("/api/licenses", FilterLicense)

// set up authentication
authorized := r.Group("/")
authorized.Use(auth.AuthenticationMiddleware())

authorized.POST("/api/licenses", CreateLicense)
authorized.PATCH("/api/licenses/:shortname", UpdateLicense)
authorized.POST("/api/users", auth.CreateUser)
authorized.GET("/api/users", auth.GetAllUser)
authorized.GET("/api/users/:id", auth.GetUser)

return r
}

func HandleInvalidUrl(c *gin.Context) {

Expand All @@ -27,9 +51,10 @@ func HandleInvalidUrl(c *gin.Context) {
c.JSON(http.StatusNotFound, er)
}
func GetAllLicense(c *gin.Context) {

var licenses []models.LicenseDB

err := DB.Find(&licenses).Error
err := db.DB.Find(&licenses).Error
if err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Expand All @@ -44,7 +69,7 @@ func GetAllLicense(c *gin.Context) {
res := models.LicenseResponse{
Data: licenses,
Status: http.StatusOK,
Meta: models.Meta{
Meta: models.PaginationMeta{
ResourceCount: len(licenses),
},
}
Expand All @@ -60,7 +85,7 @@ func GetLicense(c *gin.Context) {
return
}

err := DB.Where("shortname = ?", queryParam).First(&license).Error
err := db.DB.Where("shortname = ?", queryParam).First(&license).Error

if err != nil {
er := models.LicenseError{
Expand All @@ -77,7 +102,7 @@ func GetLicense(c *gin.Context) {
res := models.LicenseResponse{
Data: []models.LicenseDB{license},
Status: http.StatusOK,
Meta: models.Meta{
Meta: models.PaginationMeta{
ResourceCount: 1,
},
}
Expand Down Expand Up @@ -105,7 +130,7 @@ func CreateLicense(c *gin.Context) {
}
license := models.LicenseDB(input)

result := DB.FirstOrCreate(&license)
result := db.DB.FirstOrCreate(&license)
if result.RowsAffected == 0 {

er := models.LicenseError{
Expand All @@ -132,7 +157,7 @@ func CreateLicense(c *gin.Context) {
res := models.LicenseResponse{
Data: []models.LicenseDB{license},
Status: http.StatusCreated,
Meta: models.Meta{
Meta: models.PaginationMeta{
ResourceCount: 1,
},
}
Expand All @@ -144,7 +169,7 @@ func UpdateLicense(c *gin.Context) {
var update models.LicenseDB
var license models.LicenseDB
shortname := c.Param("shortname")
if err := DB.Where("shortname = ?", shortname).First(&license).Error; err != nil {
if err := db.DB.Where("shortname = ?", shortname).First(&license).Error; err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("license with shortname '%s' not found", shortname),
Expand All @@ -166,7 +191,7 @@ func UpdateLicense(c *gin.Context) {
c.JSON(http.StatusBadRequest, er)
return
}
if err := DB.Model(&license).Updates(update).Error; err != nil {
if err := db.DB.Model(&license).Updates(update).Error; err != nil {
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "Failed to update license",
Expand All @@ -180,7 +205,7 @@ func UpdateLicense(c *gin.Context) {
res := models.LicenseResponse{
Data: []models.LicenseDB{license},
Status: http.StatusOK,
Meta: models.Meta{
Meta: models.PaginationMeta{
ResourceCount: 1,
},
}
Expand All @@ -200,7 +225,7 @@ func FilterLicense(c *gin.Context) {
fsffree := c.Query("fsffree")
copyleft := c.Query("copyleft")
var license []models.LicenseDB
query := DB.Model(&license)
query := db.DB.Model(&license)

if SpdxId == "" && GPLv2compatible == "" && GPLv3compatible == "" && DetectorType == "" && marydone == "" && active == "" && fsffree == "" && OSIapproved == "" && copyleft == "" {
GetAllLicense(c)
Expand Down Expand Up @@ -258,7 +283,7 @@ func FilterLicense(c *gin.Context) {
res := models.LicenseResponse{
Data: license,
Status: http.StatusOK,
Meta: models.Meta{
Meta: models.PaginationMeta{
ResourceCount: len(license),
},
}
Expand All @@ -282,7 +307,7 @@ func SearchInLicense(c *gin.Context) {
}

var license []models.LicenseDB
query := DB.Model(&license)
query := db.DB.Model(&license)

if input.SearchType == "fuzzy" {
query = query.Where(fmt.Sprintf("%s ILIKE ?", input.Field), fmt.Sprintf("%%%s%%", input.SearchTerm))
Expand All @@ -305,7 +330,7 @@ func SearchInLicense(c *gin.Context) {
res := models.LicenseResponse{
Data: license,
Status: http.StatusOK,
Meta: models.Meta{
Meta: models.PaginationMeta{
ResourceCount: len(license),
},
}
Expand Down
Loading

0 comments on commit b324a4d

Please sign in to comment.