Skip to content

Commit

Permalink
fetch data from database
Browse files Browse the repository at this point in the history
  • Loading branch information
VikashChauhan51 committed Jul 27, 2024
1 parent b2de866 commit a0787ee
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 67 deletions.
14 changes: 8 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
"mode": "debug",
"program": "${workspaceFolder}/cmd/api/main.go",
"cwd": "${workspaceFolder}",
"args": [],
"buildFlags": "",
"env": {},
"port": 0
}
]
}
}
16 changes: 10 additions & 6 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/VikashChauhan51/go-sample-api/cmd/api/routes"
docs "github.com/VikashChauhan51/go-sample-api/docs"
"github.com/VikashChauhan51/go-sample-api/internal/core/entities"
"github.com/VikashChauhan51/go-sample-api/internal/infra/databases"
"github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files"
Expand All @@ -15,11 +16,18 @@ import (
func main() {

// Connect to the database
dsn := "sqlserver://username:password@localhost:1433?database=bookstore"
dsn := "sqlserver://Sa:Welcome@123@localhost:1433?database=bookstore"
db, err := databases.NewDBConnection(dsn)
if err != nil {
fmt.Printf("failed to connect to database: %v \n", err)
}

// Auto migrate database (create tables)
err = db.AutoMigrate(&entities.Book{})
if err != nil {
fmt.Printf("failed to migrate database: %v", err)
}

r := gin.New()
// LoggerWithFormatter middleware will write the logs to gin.DefaultWriter
// By default gin.DefaultWriter = os.Stdout
Expand All @@ -43,11 +51,7 @@ func main() {
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
v1 := r.Group("/api/v1")
{
allRoutes, err := routes.ScanRoutes(db)
if err != nil {
fmt.Printf("failed to load routes: %v\n", err)
return
}
allRoutes := routes.ScanRoutes(db)
routes.RegisterRoutes(v1, allRoutes)
}

Expand Down
52 changes: 4 additions & 48 deletions cmd/api/routes/loader.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,12 @@
package routes

import (
"fmt"
"os"
"path/filepath"
"plugin"
"strings"

"github.com/VikashChauhan51/go-sample-api/internal/core/interfaces"
)

// ScanRoutes scans all route files in the current directory and collects routes.
func ScanRoutes(db interfaces.Database) ([]Route, error) {
func ScanRoutes(db interfaces.Database) []Route {
var allRoutes []Route

// Find all *_routes.go files in the current directory
err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if strings.HasSuffix(info.Name(), "_routes.go") {
routes, err := loadRoutesFromPlugin(path, db)
if err != nil {
return err
}
allRoutes = append(allRoutes, routes...)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to scan route files: %v", err)
}

return allRoutes, nil
}

// loadRoutesFromPlugin loads routes from a plugin file.
func loadRoutesFromPlugin(path string, db interfaces.Database) ([]Route, error) {
p, err := plugin.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open plugin %s: %v", path, err)
}

sym, err := p.Lookup("Routes")
if err != nil {
return nil, fmt.Errorf("failed to find GetRoutes in plugin %s: %v", path, err)
}

getRoutesFunc, ok := sym.(func(interfaces.Database) []Route)
if !ok {
return nil, fmt.Errorf("GetRoutes function has incorrect signature in plugin %s", path)
}

return getRoutesFunc(db), nil
booksRoutes := GetBookRoutes(db)
allRoutes = append(allRoutes, booksRoutes...)
return allRoutes
}
4 changes: 2 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const docTemplate = `{
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/models.Book"
"$ref": "#/definitions/dto.Book"
}
}
}
Expand All @@ -40,7 +40,7 @@ const docTemplate = `{
}
},
"definitions": {
"models.Book": {
"dto.Book": {
"type": "object",
"properties": {
"author": {
Expand Down
4 changes: 2 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/models.Book"
"$ref": "#/definitions/dto.Book"
}
}
}
Expand All @@ -29,7 +29,7 @@
}
},
"definitions": {
"models.Book": {
"dto.Book": {
"type": "object",
"properties": {
"author": {
Expand Down
4 changes: 2 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
definitions:
models.Book:
dto.Book:
properties:
author:
type: string
Expand All @@ -21,7 +21,7 @@ paths:
description: OK
schema:
items:
$ref: '#/definitions/models.Book'
$ref: '#/definitions/dto.Book'
type: array
summary: Get books
tags:
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/books_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewBookController(service svc.BookService) *BookController {
// @Description Retrieves a list of books
// @Tags books
// @Produce json
// @Success 200 {array} models.Book
// @Success 200 {array} dto.Book
// @Router /books [get]
func (b *BookController) GetBooks(c *gin.Context) {
books, err := b.bookService.FetchBooksAsync()
Expand Down

0 comments on commit a0787ee

Please sign in to comment.