Skip to content

Commit

Permalink
chore: add models and services middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
baimamboukar committed Dec 26, 2023
1 parent fb41cd1 commit 88a19bf
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 1 deletion.
Binary file modified gin-bin
Binary file not shown.
14 changes: 14 additions & 0 deletions src/controllers/companies_controller.go
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})
}
14 changes: 14 additions & 0 deletions src/middlewares/auth_middleware.go
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()
}
}
20 changes: 20 additions & 0 deletions src/middlewares/logger_middleware.go
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)
}
}
8 changes: 8 additions & 0 deletions src/middlewares/middlewares.go
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())
}
22 changes: 22 additions & 0 deletions src/models/db.go
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
}
14 changes: 14 additions & 0 deletions src/models/startup.go
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"`
}
2 changes: 1 addition & 1 deletion src/services/companies_service.go
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"

Expand Down

0 comments on commit 88a19bf

Please sign in to comment.