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