initial commit
This commit is contained in:
33
internal/service/config.go
Normal file
33
internal/service/config.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DatabasePath string `json:"databasePath"`
|
||||
TemplateDirectoryPath string `json:"templateDirectoryPath"`
|
||||
HostName string `json:"hostName"`
|
||||
}
|
||||
|
||||
func ReadConfigFrom(reader io.Reader) (Config, error) {
|
||||
var config Config
|
||||
err := json.NewDecoder(reader).Decode(&config)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
config.DatabasePath, err = filepath.Abs(config.DatabasePath)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
config.TemplateDirectoryPath, err = filepath.Abs(config.TemplateDirectoryPath)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
79
internal/service/service.go
Normal file
79
internal/service/service.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/docker/docker/client"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/olahol/melody"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/sqlitedialect"
|
||||
"github.com/uptrace/bun/driver/sqliteshim"
|
||||
"github.com/uptrace/bun/extra/bundebug"
|
||||
_ "modernc.org/sqlite"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
keyHTTPClient = "httpClient"
|
||||
keyDockerClient = "dockerClient"
|
||||
keyDB = "db"
|
||||
keyConfig = "config"
|
||||
keyMelody = "melody"
|
||||
)
|
||||
|
||||
type Services struct {
|
||||
HTTPClient *http.Client
|
||||
DockerClient *client.Client
|
||||
Database *bun.DB
|
||||
Config Config
|
||||
Melody *melody.Melody
|
||||
}
|
||||
|
||||
func HTTPClient(c echo.Context) *http.Client {
|
||||
return c.Get(keyHTTPClient).(*http.Client)
|
||||
}
|
||||
|
||||
func DockerClient(c echo.Context) *client.Client {
|
||||
return c.Get(keyDockerClient).(*client.Client)
|
||||
}
|
||||
|
||||
func Database(c echo.Context) *bun.DB {
|
||||
return c.Get(keyDB).(*bun.DB)
|
||||
}
|
||||
|
||||
func Initialize(config Config) (Services, error) {
|
||||
hc := &http.Client{}
|
||||
|
||||
docker, err := client.NewClientWithOpts(client.FromEnv)
|
||||
if err != nil {
|
||||
return Services{}, err
|
||||
}
|
||||
|
||||
db, err := sql.Open(sqliteshim.ShimName, config.DatabasePath)
|
||||
if err != nil {
|
||||
return Services{}, err
|
||||
}
|
||||
bundb := bun.NewDB(db, sqlitedialect.New())
|
||||
bundb.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
|
||||
|
||||
return Services{
|
||||
HTTPClient: hc,
|
||||
DockerClient: docker,
|
||||
Database: bundb,
|
||||
Config: config,
|
||||
Melody: melody.New(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s Services) Middleware() echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
c.Set(keyHTTPClient, s.HTTPClient)
|
||||
c.Set(keyDockerClient, s.DockerClient)
|
||||
c.Set(keyDB, s.Database)
|
||||
c.Set(keyConfig, s.Config)
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user