First version
This commit is contained in:
24
internals/conf/loader.go
Normal file
24
internals/conf/loader.go
Normal file
@ -0,0 +1,24 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"github.com/gookit/config/v2"
|
||||
"github.com/gookit/config/v2/yaml"
|
||||
"github.com/kataras/golog"
|
||||
)
|
||||
|
||||
var cfgPath string
|
||||
var App *config.Config
|
||||
|
||||
func LoadConfig(path string) *config.Config {
|
||||
App = config.New("appCfg")
|
||||
cfgPath = path
|
||||
App.AddDriver(yaml.Driver)
|
||||
|
||||
err := App.LoadFiles(path)
|
||||
|
||||
if err != nil {
|
||||
golog.Fatalf("Error loading config: %v", err)
|
||||
}
|
||||
|
||||
return App
|
||||
}
|
69
internals/helpers/findHostConfigs.go
Normal file
69
internals/helpers/findHostConfigs.go
Normal file
@ -0,0 +1,69 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"hoster/internals/types"
|
||||
"io/fs"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gookit/goutil/fsutil"
|
||||
)
|
||||
|
||||
func FindHostConfigs(path string) ([]types.Project, error) {
|
||||
projects := []types.Project{}
|
||||
|
||||
fsutil.FindInDir(path, func(filePath string, de fs.DirEntry) error {
|
||||
project := types.Project{}
|
||||
parseHostFile(filePath, &project)
|
||||
|
||||
if project.Domain != "" {
|
||||
projects = append(projects, project)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return projects, nil
|
||||
}
|
||||
|
||||
var portRegex = regexp.MustCompile(`listen\s+(\d+)`)
|
||||
var serverNameRegex = regexp.MustCompile(`server_name\s+(.+);`)
|
||||
|
||||
func parseHostFile(filePath string, project *types.Project) error {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
|
||||
if strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(line, "listen ") {
|
||||
port := portRegex.FindStringSubmatch(line)
|
||||
project.InternalPort, err = strconv.Atoi(port[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(line, "server_name ") {
|
||||
serverName := serverNameRegex.FindStringSubmatch(line)
|
||||
project.Domain = serverName[1]
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
7
internals/helpers/root.go
Normal file
7
internals/helpers/root.go
Normal file
@ -0,0 +1,7 @@
|
||||
package helpers
|
||||
|
||||
import "os"
|
||||
|
||||
func IsRootUser() bool {
|
||||
return os.Geteuid() == 0
|
||||
}
|
40
internals/templates/templates.go
Normal file
40
internals/templates/templates.go
Normal file
@ -0,0 +1,40 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"hoster/internals/conf"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
|
||||
"github.com/gookit/goutil/fsutil"
|
||||
)
|
||||
|
||||
func Execute(name string, outPath string, context any) error {
|
||||
tplPath := filepath.Join(conf.App.MustString(fmt.Sprintf("templates.%s", name)))
|
||||
|
||||
if !fsutil.IsFile(tplPath) {
|
||||
return fmt.Errorf("template file \"%s\" doesn't exist", tplPath)
|
||||
}
|
||||
|
||||
outFile, err := os.Create(outPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer outFile.Close()
|
||||
|
||||
tpl, err := os.ReadFile(tplPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = template.Must(template.New("tpl").Parse(string(tpl))).Execute(outFile, context)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
8
internals/types/project.go
Normal file
8
internals/types/project.go
Normal file
@ -0,0 +1,8 @@
|
||||
package types
|
||||
|
||||
type Project struct {
|
||||
Name string
|
||||
Domain string
|
||||
HostConfigFile string
|
||||
InternalPort int
|
||||
}
|
Reference in New Issue
Block a user