formatting
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -29,8 +29,8 @@ _testmain.go
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
*.log
|
||||
*.db
|
||||
|
||||
# .Dockerfile
|
||||
|
||||
logs.log
|
||||
querys.log
|
||||
@@ -1,22 +1,42 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/controller"
|
||||
"acc-server-manager/local/utl/common"
|
||||
"acc-server-manager/local/utl/configs"
|
||||
"os"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/basicauth"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
Init
|
||||
|
||||
Initializes Web API Routes.
|
||||
|
||||
Args:
|
||||
*fiber.App: Fiber Application.
|
||||
*/
|
||||
// Routes
|
||||
// Initializes web api controllers and its corresponding routes.
|
||||
//
|
||||
// Args:
|
||||
// *fiber.App: Fiber Application
|
||||
func Init(di *dig.Container, app *fiber.App) {
|
||||
Routes(di, app)
|
||||
}
|
||||
groups := app.Group(configs.Prefix)
|
||||
|
||||
type API struct {
|
||||
Api string `json:"api"`
|
||||
basicAuthConfig := basicauth.New(basicauth.Config{
|
||||
Users: map[string]string{
|
||||
"admin": os.Getenv("PASSWORD"),
|
||||
},
|
||||
})
|
||||
|
||||
routeGroups := &common.RouteGroups{
|
||||
Api: groups.Group("/api"),
|
||||
}
|
||||
|
||||
routeGroups.Api.Use(basicAuthConfig)
|
||||
|
||||
err := di.Provide(func() *common.RouteGroups {
|
||||
return routeGroups
|
||||
})
|
||||
if err != nil {
|
||||
panic("unable to bind routes")
|
||||
}
|
||||
|
||||
controller.InitializeControllers(di)
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/controller"
|
||||
"acc-server-manager/local/utl/common"
|
||||
"acc-server-manager/local/utl/configs"
|
||||
"os"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/basicauth"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
Routes
|
||||
|
||||
Initializes web api controllers and its corresponding routes.
|
||||
|
||||
Args:
|
||||
*fiber.App: Fiber Application
|
||||
*/
|
||||
func Routes(di *dig.Container, app *fiber.App) {
|
||||
groups := app.Group(configs.Prefix)
|
||||
|
||||
basicAuthConfig := basicauth.New(basicauth.Config{
|
||||
Users: map[string]string{
|
||||
"admin": os.Getenv("PASSWORD"),
|
||||
},
|
||||
})
|
||||
|
||||
routeGroups := &common.RouteGroups{
|
||||
Api: groups.Group("api"),
|
||||
}
|
||||
|
||||
routeGroups.Api.Use(basicAuthConfig)
|
||||
|
||||
di.Provide(func() *common.RouteGroups {
|
||||
return routeGroups
|
||||
})
|
||||
|
||||
controller.InitializeControllers(di)
|
||||
}
|
||||
@@ -11,24 +11,21 @@ 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
|
||||
*/
|
||||
// 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)
|
||||
routeGroups.Api.Post("", ac.startServer)
|
||||
routeGroups.Api.Get("/", ac.getFirst)
|
||||
routeGroups.Api.Post("/", ac.startServer)
|
||||
|
||||
return ac
|
||||
}
|
||||
@@ -42,7 +39,7 @@ func NewApiController(as *service.ApiService, routeGroups *common.RouteGroups) *
|
||||
// @Router /v1/api [get]
|
||||
func (ac *ApiController) getFirst(c *fiber.Ctx) error {
|
||||
apiModel := ac.service.GetFirst(c)
|
||||
return c.SendString(apiModel)
|
||||
return c.SendString(apiModel.Api)
|
||||
}
|
||||
|
||||
// startServer returns API
|
||||
|
||||
@@ -12,31 +12,27 @@ import (
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
InitializeControllers
|
||||
|
||||
Initializes Dependency Injection modules and registers controllers
|
||||
|
||||
Args:
|
||||
*dig.Container: Dig Container
|
||||
*/
|
||||
// InitializeControllers
|
||||
// Initializes Dependency Injection modules and registers controllers
|
||||
//
|
||||
// Args:
|
||||
// *dig.Container: Dig Container
|
||||
func InitializeControllers(c *dig.Container) {
|
||||
service.InitializeServices(c)
|
||||
|
||||
c.Invoke(NewApiController)
|
||||
|
||||
err := c.Invoke(NewApiController)
|
||||
if err != nil {
|
||||
panic("unable to initialize api controller")
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
FilteredResponse
|
||||
|
||||
Gets query parameters and populates FilteredResponse model.
|
||||
|
||||
Args:
|
||||
*gin.Context: Gin Application Context
|
||||
Returns:
|
||||
*model.FilteredResponse: Filtered response
|
||||
*/
|
||||
// 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")
|
||||
|
||||
@@ -32,11 +32,8 @@ type BaseModel struct {
|
||||
DateUpdated time.Time `json:"dateUpdated"`
|
||||
}
|
||||
|
||||
/*
|
||||
Init
|
||||
|
||||
Initializes base model with DateCreated, DateUpdated, and Id values.
|
||||
*/
|
||||
// Init
|
||||
// Initializes base model with DateCreated, DateUpdated, and Id values.
|
||||
func (cm *BaseModel) Init() {
|
||||
date := time.Now()
|
||||
cm.Id = uuid.NewString()
|
||||
|
||||
@@ -17,19 +17,16 @@ func NewApiRepository(db *gorm.DB) *ApiRepository {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
GetFirst
|
||||
|
||||
Gets first row from API table.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
Returns:
|
||||
model.ApiModel: Api object from database.
|
||||
*/
|
||||
func (as ApiRepository) GetFirst(ctx context.Context) model.ApiModel {
|
||||
// GetFirst
|
||||
// Gets first row from API table.
|
||||
//
|
||||
// Args:
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// model.ApiModel: Api object from database.
|
||||
func (as ApiRepository) GetFirst(ctx context.Context) *model.ApiModel {
|
||||
db := as.db.WithContext(ctx)
|
||||
apiModel := model.ApiModel{Api: "Works"}
|
||||
apiModel := new(model.ApiModel)
|
||||
db.First(&apiModel)
|
||||
return apiModel
|
||||
}
|
||||
|
||||
@@ -4,14 +4,11 @@ import (
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
InitializeRepositories
|
||||
|
||||
Initializes Dependency Injection modules for repositories
|
||||
|
||||
Args:
|
||||
*dig.Container: Dig Container
|
||||
*/
|
||||
// InitializeRepositories
|
||||
// Initializes Dependency Injection modules for repositories
|
||||
//
|
||||
// Args:
|
||||
// *dig.Container: Dig Container
|
||||
func InitializeRepositories(c *dig.Container) {
|
||||
c.Provide(NewApiRepository)
|
||||
}
|
||||
|
||||
15
local/scripts/run_sc.ps1
Normal file
15
local/scripts/run_sc.ps1
Normal file
@@ -0,0 +1,15 @@
|
||||
param (
|
||||
[string]$Action,
|
||||
[string]$ServiceName
|
||||
)
|
||||
|
||||
if ($Action -eq "start") {
|
||||
sc.exe start $ServiceName
|
||||
} elseif ($Action -eq "stop") {
|
||||
sc.exe stop $ServiceName
|
||||
} elseif ($Action -eq "restart") {
|
||||
sc.exe stop $ServiceName
|
||||
sc.exe start $ServiceName
|
||||
} else {
|
||||
Write-Error "Invalid action specified. Use 'start', 'stop', or 'restart'."
|
||||
}
|
||||
@@ -1,36 +1,33 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/model"
|
||||
"acc-server-manager/local/repository"
|
||||
"acc-server-manager/local/utl/common"
|
||||
"acc-server-manager/local/utl/configs"
|
||||
"errors"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type ApiService struct {
|
||||
Repository *repository.ApiRepository
|
||||
repository *repository.ApiRepository
|
||||
}
|
||||
|
||||
func NewApiService(repository *repository.ApiRepository) *ApiService {
|
||||
return &ApiService{
|
||||
Repository: repository,
|
||||
repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
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
|
||||
// GetFirst
|
||||
// Gets first row from API table.
|
||||
//
|
||||
// Args:
|
||||
// context.Context: Application context
|
||||
// Returns:
|
||||
// string: Application version
|
||||
func (as ApiService) GetFirst(ctx *fiber.Ctx) *model.ApiModel {
|
||||
return as.repository.GetFirst(ctx.UserContext())
|
||||
}
|
||||
|
||||
func (as ApiService) ApiStartServer(ctx *fiber.Ctx) (string, error) {
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/repository"
|
||||
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
InitializeServices
|
||||
|
||||
Initializes Dependency Injection modules for services
|
||||
|
||||
Args:
|
||||
*dig.Container: Dig Container
|
||||
*/
|
||||
// InitializeServices
|
||||
// Initializes Dependency Injection modules for services
|
||||
//
|
||||
// Args:
|
||||
// *dig.Container: Dig Container
|
||||
func InitializeServices(c *dig.Container) {
|
||||
repository.InitializeRepositories(c)
|
||||
|
||||
c.Provide(NewApiService)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/model"
|
||||
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
@@ -11,7 +13,19 @@ func Start(di *dig.Container) {
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
di.Provide(func() *gorm.DB {
|
||||
err = di.Provide(func() *gorm.DB {
|
||||
return db
|
||||
})
|
||||
if err != nil {
|
||||
panic("failed to bind database")
|
||||
}
|
||||
Migrate(db)
|
||||
}
|
||||
|
||||
func Migrate(db *gorm.DB) {
|
||||
err := db.AutoMigrate(&model.ApiModel{})
|
||||
if err != nil {
|
||||
panic("failed to migrate model.ApiModel")
|
||||
}
|
||||
db.FirstOrCreate(&model.ApiModel{Api: "Works"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user