Skip to content

Commit

Permalink
refactor: default handlers 🏗️
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemmahlees committed Mar 18, 2024
1 parent ec4e825 commit a082e5f
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 22 deletions.
29 changes: 27 additions & 2 deletions docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"swagger": "2.0",
"info": {
"description": "A RESTFull and GraphQL API to manage your MySQL DB",
"description": "A RESTFull and GraphQL API to supercharge your database",
"title": "MetaX",
"contact": {
"name": "Kareem Ebrahim",
"email": "[email protected]"
},
"version": "1.0"
"version": "0.1.1"
},
"host": "localhost:5522",
"basePath": "/",
Expand Down Expand Up @@ -331,6 +331,31 @@
}
},
"definitions": {
"handlers.APIInfoResult": {
"type": "object",
"properties": {
"author": {
"type": "string"
},
"contact": {
"type": "string"
},
"repo": {
"type": "string"
},
"year": {
"type": "integer"
}
}
},
"handlers.HealthCheckResult": {
"type": "object",
"properties": {
"date": {
"type": "string"
}
}
},
"models.AddModifyColumnPayload": {
"type": "object",
"required": [
Expand Down
20 changes: 18 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
basePath: /
definitions:
handlers.APIInfoResult:
properties:
author:
type: string
contact:
type: string
repo:
type: string
year:
type: integer
type: object
handlers.HealthCheckResult:
properties:
date:
type: string
type: object
models.AddModifyColumnPayload:
properties:
column_name:
Expand Down Expand Up @@ -93,9 +109,9 @@ info:
contact:
email: [email protected]
name: Kareem Ebrahim
description: A RESTFull and GraphQL API to manage your MySQL DB
description: A RESTFull and GraphQL API to supercharge your database
title: MetaX
version: "1.0"
version: 0.1.1
paths:
/:
get:
Expand Down
26 changes: 26 additions & 0 deletions internal/db/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package db

import "github.com/kareemmahlees/meta-x/models"

// Interface that must be implemented by any
// storage driver dealing with database logic
type DatabaseExecuter interface {
ListDBs() ([]*string, error)
CreateDB(dbName string) error
}

// Interface that must be implemented by any
// storage driver dealing with table logic
type TableExecuter interface {
GetTable(tableName string) ([]*models.TableInfoResp, error)
ListTables() ([]*string, error)
CreateTable(tableName string, data []models.CreateTablePayload) error
DeleteTable(tableName string) error
AddColumn(tableName string, data models.AddModifyColumnPayload) error
UpdateColumn(tableName string, data models.AddModifyColumnPayload) error
}

type Storage interface {
DatabaseExecuter
TableExecuter
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package routes
package handlers

import (
"time"

_ "github.com/kareemmahlees/meta-x/docs"

"github.com/gofiber/fiber/v2"
"github.com/kareemmahlees/meta-x/internal/db"
)

func RegisterDefaultRoutes(app *fiber.App) {
app.Get("/health", healthCheck)
app.Get("/", apiInfo)
type defaultHandler struct {
storage *db.Storage
}

func NewDefaultHandler(storage *db.Storage) *defaultHandler {
return &defaultHandler{storage}
}

func (h *defaultHandler) RegisterRoutes(app *fiber.App) {
app.Get("/health", h.healthCheck)
app.Get("/", h.apiInfo)
}

type HealthCheckResult struct {
Expand All @@ -24,7 +31,7 @@ type HealthCheckResult struct {
// @tags default
// @router /health [get]
// @success 200 {object} HealthCheckResult
func healthCheck(c *fiber.Ctx) error {
func (h *defaultHandler) healthCheck(c *fiber.Ctx) error {
return c.JSON(fiber.Map{"date": time.Now()})
}

Expand All @@ -42,11 +49,11 @@ type APIInfoResult struct {
// @tags default
// @router / [get]
// @success 200 {object} APIInfoResult
func apiInfo(c *fiber.Ctx) error {
func (h *defaultHandler) apiInfo(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"author": "Kareem Ebrahim",
"year": 2023,
"contact": "[email protected]",
"repo": "https://github.com/kareemmahlees/mysql-meta",
"repo": "https://github.com/kareemmahlees/meta-x",
})
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package routes
package handlers

import (
"net/http/httptest"
"testing"

"github.com/kareemmahlees/meta-x/utils"

"github.com/gofiber/fiber/v2"
"github.com/kareemmahlees/meta-x/utils"
"github.com/stretchr/testify/assert"
)

func TestRegisterDefaultRoutes(t *testing.T) {

app := fiber.New()
handler := NewDefaultHandler(nil)

RegisterDefaultRoutes(app)
handler.RegisterRoutes(app)

var routes []utils.FiberRoute
for _, route := range app.GetRoutes() {
Expand All @@ -33,7 +33,8 @@ func TestRegisterDefaultRoutes(t *testing.T) {

func TestHealthCheck(t *testing.T) {
app := fiber.New()
RegisterDefaultRoutes(app)
handler := NewDefaultHandler(nil)
handler.RegisterRoutes(app)

req := httptest.NewRequest("GET", "http://localhost:4000/health", nil)

Expand All @@ -48,7 +49,8 @@ func TestHealthCheck(t *testing.T) {

func TestBaseUrl(t *testing.T) {
app := fiber.New()
RegisterDefaultRoutes(app)
handler := NewDefaultHandler(nil)
handler.RegisterRoutes(app)

req := httptest.NewRequest("GET", "http://localhost:4000", nil)

Expand Down
7 changes: 7 additions & 0 deletions internal/handlers/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package handlers

import "github.com/gofiber/fiber/v2"

type Handler interface {
RegisterRoutes(app *fiber.App)
}
1 change: 0 additions & 1 deletion internal/rest/setupRoutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

func Setup(app *fiber.App, db *sqlx.DB) {
RegisterDefaultRoutes(app)
RegisterDatabasesRoutes(app, db)
RegisterTablesRoutes(app, db)
}
5 changes: 5 additions & 0 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package internal

import (
"fmt"

"github.com/kareemmahlees/meta-x/internal/graph"
"github.com/kareemmahlees/meta-x/internal/handlers"
routes "github.com/kareemmahlees/meta-x/internal/rest"
"github.com/kareemmahlees/meta-x/utils"

Expand Down Expand Up @@ -50,6 +52,9 @@ func InitDBAndServer(app *fiber.App, provider, cfg string, port int, listenCh ch

routes.Setup(app, con)

defaultHandler := handlers.NewDefaultHandler(nil)
defaultHandler.RegisterRoutes(app)

fmt.Println(utils.NewStyle("REST", "#4B87FF"), fmt.Sprintf("http://localhost:%d", port))
fmt.Println(utils.NewStyle("Swagger", "#0EEBA1"), fmt.Sprintf("http://localhost:%d/swagger", port))
fmt.Println(utils.NewStyle("GraphQl", "#FF70FD"), fmt.Sprintf("http://localhost:%d/graphql", port))
Expand Down

0 comments on commit a082e5f

Please sign in to comment.