-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add models and services middlewares
- Loading branch information
1 parent
fb41cd1
commit 88a19bf
Showing
8 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package controllers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
// Get all books | ||
func FindBooks(c *gin.Context) { | ||
var books []models.Book | ||
models.DB.Find(&books) | ||
|
||
c.JSON(http.StatusOK, gin.H{"data": books}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package middlewares | ||
|
||
import "github.com/gin-gonic/gin" | ||
|
||
func AuthMiddleware() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
apiKey := c.GetHeader("x-api-key") | ||
if apiKey == "" { | ||
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized to perform request. Please get a valid API key"}) | ||
return | ||
} | ||
c.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package middlewares | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// The`LoggerMiddleware` function | ||
// -------------------------------------------------------- | ||
// returns a Gin `Handler Function` that measures the request time, and Logs basic information about the request | ||
func LoggerMiddleware() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
start := time.Now() | ||
c.Next() | ||
duration := time.Since(start) | ||
log.Printf("Request - Method: %s | Status: %d | Duration: %v", c.Request.Method, c.Writer.Status(), duration) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package middlewares | ||
|
||
import "github.com/gin-gonic/gin" | ||
|
||
func RegisterMiddlewares(router *gin.Engine) { | ||
router.Use(AuthMiddleware()) | ||
router.Use(LoggerMiddleware()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package models | ||
|
||
import ( | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var DB *gorm.DB | ||
|
||
func OpenDatabaseConnection() { | ||
|
||
database, err := gorm.Open(sqlite.Open("database.sqlite"), &gorm.Config{}) | ||
if err != nil { | ||
panic("Failed to connect to database!") | ||
} | ||
err = database.AutoMigrate(&Company{}) | ||
if err != nil { | ||
return | ||
} | ||
|
||
DB = database | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package models | ||
|
||
type Startup struct { | ||
Name string `json:"name" bson:"name"` | ||
CEO string `json:"ceo" bson:"ceo"` | ||
ValueProposition string `json:"value_proposition" bson:"value_proposition"` | ||
Industry string `json:"industry" bson:"industry"` | ||
} | ||
|
||
type Founder struct { | ||
Name string `json:"name" bson:"name"` | ||
Profession string `json:"profession" bson:"profession"` | ||
Age int `json:"age" bson:"age"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package service | ||
package services | ||
|
||
import "github.com/baimamboukar/go-gin-docker-k8s/src/models" | ||
|
||
|