init
This commit is contained in:
21
local/api/api.go
Normal file
21
local/api/api.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
/*
|
||||
Init
|
||||
|
||||
Initializes Web API Routes.
|
||||
|
||||
Args:
|
||||
*fiber.App: Fiber Application.
|
||||
*/
|
||||
func Init(app *fiber.App) {
|
||||
Routes(app)
|
||||
}
|
||||
|
||||
type API struct {
|
||||
Api string `json:"api"`
|
||||
}
|
||||
34
local/api/routes.go
Normal file
34
local/api/routes.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/controller"
|
||||
"acc-server-manager/local/utl/common"
|
||||
"acc-server-manager/local/utl/configs"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
Routes
|
||||
|
||||
Initializes web api controllers and its corresponding routes.
|
||||
|
||||
Args:
|
||||
*fiber.App: Fiber Application
|
||||
*/
|
||||
func Routes(app *fiber.App) {
|
||||
c := dig.New()
|
||||
groups := app.Group(configs.Prefix)
|
||||
|
||||
apiGroup := groups.Group("api")
|
||||
routeGroups := &common.RouteGroups{
|
||||
Api: apiGroup,
|
||||
}
|
||||
|
||||
c.Provide(func() *common.RouteGroups {
|
||||
return routeGroups
|
||||
})
|
||||
|
||||
controller.InitializeControllers(c)
|
||||
}
|
||||
44
local/controller/api.go
Normal file
44
local/controller/api.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/service"
|
||||
"acc-server-manager/local/utl/common"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type ApiController struct {
|
||||
service *service.ApiService
|
||||
}
|
||||
|
||||
/*
|
||||
NewApiController
|
||||
|
||||
Initializes ApiController.
|
||||
|
||||
Args:
|
||||
*services.ApiService: API service
|
||||
*Fiber.RouterGroup: Fiber Router Group
|
||||
Returns:
|
||||
*ApiController: Controller for "api" interactions
|
||||
*/
|
||||
func NewApiController(as *service.ApiService, routeGroups *common.RouteGroups) *ApiController {
|
||||
ac := &ApiController{
|
||||
service: as,
|
||||
}
|
||||
|
||||
routeGroups.Api.Get("", ac.getFirst)
|
||||
|
||||
return ac
|
||||
}
|
||||
|
||||
/*
|
||||
getFirst
|
||||
Args:
|
||||
*fiber.Ctx: Fiber Application Context
|
||||
*/
|
||||
// ROUTE (GET /api).
|
||||
func (ac *ApiController) getFirst(c *fiber.Ctx) error {
|
||||
apiModel := ac.service.GetFirst(c)
|
||||
return c.SendString(apiModel)
|
||||
}
|
||||
62
local/controller/controller.go
Normal file
62
local/controller/controller.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/model"
|
||||
"acc-server-manager/local/service"
|
||||
"acc-server-manager/local/utl/common"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
InitializeControllers
|
||||
|
||||
Initializes Dependency Injection modules and registers controllers
|
||||
|
||||
Args:
|
||||
*dig.Container: Dig Container
|
||||
*/
|
||||
func InitializeControllers(c *dig.Container) {
|
||||
service.InitializeServices(c)
|
||||
|
||||
c.Invoke(NewApiController)
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
FilteredResponse
|
||||
|
||||
Gets query parameters and populates FilteredResponse model.
|
||||
|
||||
Args:
|
||||
*gin.Context: Gin Application Context
|
||||
Returns:
|
||||
*model.FilteredResponse: Filtered response
|
||||
*/
|
||||
func FilteredResponse(c *fiber.Ctx) *model.FilteredResponse {
|
||||
filtered := new(model.FilteredResponse)
|
||||
page := c.Params("page")
|
||||
rpp := c.Params("rpp")
|
||||
sortBy := c.Params("sortBy")
|
||||
|
||||
dividers := [5]string{"|", " ", ".", "/", ","}
|
||||
|
||||
for _, div := range dividers {
|
||||
sortArr := strings.Split(sortBy, div)
|
||||
|
||||
if len(sortArr) >= 2 {
|
||||
sortBy = fmt.Sprintf("%s %s", common.ToSnakeCase(sortArr[0]), strings.ToUpper(sortArr[1]))
|
||||
}
|
||||
}
|
||||
|
||||
filtered.Embed = c.Params("embed")
|
||||
filtered.Page, _ = strconv.Atoi(page)
|
||||
filtered.Rpp, _ = strconv.Atoi(rpp)
|
||||
filtered.SortBy = sortBy
|
||||
|
||||
return filtered
|
||||
}
|
||||
5
local/model/api.go
Normal file
5
local/model/api.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package model
|
||||
|
||||
type ApiModel struct {
|
||||
Api string `json:"api"`
|
||||
}
|
||||
45
local/model/model.go
Normal file
45
local/model/model.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type FilteredResponse struct {
|
||||
Items interface{} `json:"items"`
|
||||
Params
|
||||
}
|
||||
|
||||
type ResponseFunc func(*fiber.Ctx) *[]interface{}
|
||||
|
||||
type MessageResponse struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Params struct {
|
||||
SortBy string `json:"sortBy"`
|
||||
Embed string `json:"embed"`
|
||||
Page int `json:"page"`
|
||||
Rpp int `json:"rpp"`
|
||||
TotalRecords int `json:"totalRecords"`
|
||||
}
|
||||
|
||||
type BaseModel struct {
|
||||
Id string `json:"id"`
|
||||
DateCreated time.Time `json:"dateCreated"`
|
||||
DateUpdated time.Time `json:"dateUpdated"`
|
||||
}
|
||||
|
||||
/*
|
||||
Init
|
||||
|
||||
Initializes base model with DateCreated, DateUpdated, and Id values.
|
||||
*/
|
||||
func (cm *BaseModel) Init() {
|
||||
date := time.Now()
|
||||
cm.Id = uuid.NewString()
|
||||
cm.DateCreated = date
|
||||
cm.DateUpdated = date
|
||||
}
|
||||
28
local/service/api.go
Normal file
28
local/service/api.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/utl/configs"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type ApiService struct {
|
||||
}
|
||||
|
||||
func NewApiService() *ApiService {
|
||||
return &ApiService{}
|
||||
}
|
||||
|
||||
/*
|
||||
GetFirst
|
||||
|
||||
Gets first row from API table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
Returns:
|
||||
string: Application version
|
||||
*/
|
||||
func (as ApiService) GetFirst(ctx *fiber.Ctx) string {
|
||||
return configs.Version
|
||||
}
|
||||
17
local/service/service.go
Normal file
17
local/service/service.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
InitializeServices
|
||||
|
||||
Initializes Dependency Injection modules for services
|
||||
|
||||
Args:
|
||||
*dig.Container: Dig Container
|
||||
*/
|
||||
func InitializeServices(c *dig.Container) {
|
||||
c.Provide(NewApiService)
|
||||
}
|
||||
56
local/utl/common/common.go
Normal file
56
local/utl/common/common.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type RouteGroups struct {
|
||||
Api fiber.Router
|
||||
}
|
||||
|
||||
func CheckError(err error) {
|
||||
if err != nil {
|
||||
log.Printf("Error occured. %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
|
||||
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
|
||||
|
||||
func ToSnakeCase(str string) string {
|
||||
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
|
||||
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
|
||||
return strings.ToLower(snake)
|
||||
}
|
||||
|
||||
func GetIP() string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
os.Stderr.WriteString("Oops: " + err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
for _, a := range addrs {
|
||||
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
return ipnet.IP.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func Find[T any](lst *[]T, callback func(item *T) bool) *T {
|
||||
for _, item := range *lst {
|
||||
if callback(&item) {
|
||||
return &item
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
8
local/utl/configs/configs.go
Normal file
8
local/utl/configs/configs.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package configs
|
||||
|
||||
const (
|
||||
Version = "0.0.1"
|
||||
Prefix = "v1"
|
||||
Secret = "Donde4sta"
|
||||
SecretCode = "brasno"
|
||||
)
|
||||
25
local/utl/server/server.go
Normal file
25
local/utl/server/server.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/utl/common"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func Start(r *fiber.App) *fiber.App {
|
||||
r.Get("/ping", func(c *fiber.Ctx) error {
|
||||
return c.SendString("pong")
|
||||
})
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "4000"
|
||||
}
|
||||
err := r.Listen(":" + port)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("Running on %s:%s", common.GetIP(), port)
|
||||
println(msg)
|
||||
}
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user