add sqlite server
This commit is contained in:
@@ -1,35 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/api"
|
||||
"acc-server-manager/local/utl/db"
|
||||
"acc-server-manager/local/utl/server"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/gofiber/swagger"
|
||||
"github.com/joho/godotenv"
|
||||
"go.uber.org/dig"
|
||||
|
||||
_ "acc-server-manager/docs"
|
||||
)
|
||||
|
||||
func main() {
|
||||
godotenv.Load()
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(cors.New())
|
||||
|
||||
app.Get("/swagger/*", swagger.HandlerDefault)
|
||||
|
||||
file, err := os.OpenFile("logs.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
log.Print("Cannot open file logs.log")
|
||||
}
|
||||
log.SetOutput(file)
|
||||
|
||||
api.Init(app)
|
||||
|
||||
server.Start(app)
|
||||
di := dig.New()
|
||||
db.Start(di)
|
||||
server.Start(di)
|
||||
}
|
||||
|
||||
5
go.mod
5
go.mod
@@ -15,6 +15,8 @@ require (
|
||||
github.com/gofiber/fiber/v2 v2.52.5 // indirect
|
||||
github.com/gofiber/swagger v1.1.0 // indirect
|
||||
github.com/google/uuid v1.5.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/joho/godotenv v1.5.1 // indirect
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/klauspost/compress v1.17.0 // indirect
|
||||
@@ -22,6 +24,7 @@ require (
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.22 // indirect
|
||||
github.com/rivo/uniseg v0.2.0 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||
@@ -39,5 +42,7 @@ require (
|
||||
golang.org/x/tools v0.23.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
gorm.io/driver/sqlite v1.5.6 // indirect
|
||||
gorm.io/gorm v1.25.11 // indirect
|
||||
sigs.k8s.io/yaml v1.4.0 // indirect
|
||||
)
|
||||
|
||||
10
go.sum
10
go.sum
@@ -38,6 +38,10 @@ github.com/gofiber/swagger v1.1.0/go.mod h1:pRZL0Np35sd+lTODTE5The0G+TMHfNY+oC4h
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
||||
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||
@@ -61,6 +65,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
@@ -125,5 +131,9 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
|
||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
|
||||
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
|
||||
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
|
||||
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
||||
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
|
||||
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
|
||||
|
||||
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -12,8 +13,8 @@ Initializes Web API Routes.
|
||||
Args:
|
||||
*fiber.App: Fiber Application.
|
||||
*/
|
||||
func Init(app *fiber.App) {
|
||||
Routes(app)
|
||||
func Init(di *dig.Container, app *fiber.App) {
|
||||
Routes(di, app)
|
||||
}
|
||||
|
||||
type API struct {
|
||||
|
||||
@@ -19,8 +19,7 @@ Initializes web api controllers and its corresponding routes.
|
||||
Args:
|
||||
*fiber.App: Fiber Application
|
||||
*/
|
||||
func Routes(app *fiber.App) {
|
||||
c := dig.New()
|
||||
func Routes(di *dig.Container, app *fiber.App) {
|
||||
groups := app.Group(configs.Prefix)
|
||||
|
||||
basicAuthConfig := basicauth.New(basicauth.Config{
|
||||
@@ -35,9 +34,9 @@ func Routes(app *fiber.App) {
|
||||
|
||||
routeGroups.Api.Use(basicAuthConfig)
|
||||
|
||||
c.Provide(func() *common.RouteGroups {
|
||||
di.Provide(func() *common.RouteGroups {
|
||||
return routeGroups
|
||||
})
|
||||
|
||||
controller.InitializeControllers(c)
|
||||
controller.InitializeControllers(di)
|
||||
}
|
||||
|
||||
35
local/repository/api.go
Normal file
35
local/repository/api.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/model"
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ApiRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewApiRepository(db *gorm.DB) *ApiRepository {
|
||||
return &ApiRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
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"}
|
||||
db.First(&apiModel)
|
||||
return apiModel
|
||||
}
|
||||
17
local/repository/repository.go
Normal file
17
local/repository/repository.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
InitializeRepositories
|
||||
|
||||
Initializes Dependency Injection modules for repositories
|
||||
|
||||
Args:
|
||||
*dig.Container: Dig Container
|
||||
*/
|
||||
func InitializeRepositories(c *dig.Container) {
|
||||
c.Provide(NewApiRepository)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/repository"
|
||||
"acc-server-manager/local/utl/configs"
|
||||
"os/exec"
|
||||
|
||||
@@ -8,10 +9,13 @@ import (
|
||||
)
|
||||
|
||||
type ApiService struct {
|
||||
Repository *repository.ApiRepository
|
||||
}
|
||||
|
||||
func NewApiService() *ApiService {
|
||||
return &ApiService{}
|
||||
func NewApiService(repository *repository.ApiRepository) *ApiService {
|
||||
return &ApiService{
|
||||
Repository: repository,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
17
local/utl/db/db.go
Normal file
17
local/utl/db/db.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Start(di *dig.Container) {
|
||||
db, err := gorm.Open(sqlite.Open("acc.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
di.Provide(func() *gorm.DB {
|
||||
return db
|
||||
})
|
||||
}
|
||||
@@ -1,22 +1,41 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/api"
|
||||
"acc-server-manager/local/utl/common"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/gofiber/swagger"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
func Start(r *fiber.App) *fiber.App {
|
||||
r.Get("/ping", func(c *fiber.Ctx) error {
|
||||
func Start(di *dig.Container) *fiber.App {
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(cors.New())
|
||||
|
||||
app.Get("/swagger/*", swagger.HandlerDefault)
|
||||
|
||||
file, err := os.OpenFile("logs.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
log.Print("Cannot open file logs.log")
|
||||
}
|
||||
log.SetOutput(file)
|
||||
|
||||
api.Init(di, app)
|
||||
|
||||
app.Get("/ping", func(c *fiber.Ctx) error {
|
||||
return c.SendString("pong")
|
||||
})
|
||||
port := os.Getenv("PORT")
|
||||
err := r.Listen(":" + port)
|
||||
err = app.Listen(":" + port)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("Running on %s:%s", common.GetIP(), port)
|
||||
println(msg)
|
||||
}
|
||||
return r
|
||||
return app
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user