Built-in PM

This commit is contained in:
2023-12-10 11:30:31 +08:00
parent ef0464234a
commit ec6d834aae
21 changed files with 187 additions and 82 deletions

View File

@ -1,18 +1,19 @@
package sign
import (
"encoding/json"
"io"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
var App *AppConfig
func ReadInConfig(root string) error {
cfg := &AppConfig{
Sites: []SiteConfig{},
Sites: []*SiteConfig{},
}
if err := filepath.Walk(root, func(fp string, info os.FileInfo, err error) error {
@ -23,13 +24,13 @@ func ReadInConfig(root string) error {
return err
} else if data, err := io.ReadAll(file); err != nil {
return err
} else if err := json.Unmarshal(data, &site); err != nil {
} else if err := yaml.Unmarshal(data, &site); err != nil {
return err
} else {
// Extract file name as site id
site.ID = strings.SplitN(filepath.Base(fp), ".", 2)[0]
cfg.Sites = append(cfg.Sites, site)
cfg.Sites = append(cfg.Sites, &site)
}
return nil
@ -44,7 +45,7 @@ func ReadInConfig(root string) error {
func SaveInConfig(root string, cfg *AppConfig) error {
for _, site := range cfg.Sites {
data, _ := json.Marshal(site)
data, _ := yaml.Marshal(site)
fp := filepath.Join(root, site.ID)
if file, err := os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755); err != nil {

72
pkg/sign/pm.go Normal file
View File

@ -0,0 +1,72 @@
package sign
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
type ProcessConfig struct {
ID string `json:"id" yaml:"id"`
Workdir string `json:"workdir" yaml:"workdir"`
Command []string `json:"command" yaml:"command"`
Prepares [][]string `json:"prepares" yaml:"prepares"`
Cmd *exec.Cmd `json:"-"`
}
func (v *ProcessConfig) BootProcess() error {
if v.Cmd != nil {
return nil
}
if err := v.PreapreProcess(); err != nil {
return err
}
if v.Cmd == nil {
return v.StartProcess()
}
if v.Cmd.Process == nil || v.Cmd.ProcessState == nil {
return v.StartProcess()
}
if v.Cmd.ProcessState.Exited() {
return v.StartProcess()
} else if v.Cmd.ProcessState.Exited() {
return fmt.Errorf("process already dead")
}
if v.Cmd.ProcessState.Exited() {
return fmt.Errorf("cannot start process")
} else {
return nil
}
}
func (v *ProcessConfig) PreapreProcess() error {
for _, script := range v.Prepares {
if len(script) <= 0 {
continue
}
cmd := exec.Command(script[0], script[1:]...)
cmd.Dir = filepath.Join(v.Workdir)
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
func (v *ProcessConfig) StartProcess() error {
if len(v.Command) <= 0 {
return fmt.Errorf("you need set the command for %s to enable process manager", v.ID)
}
v.Cmd = exec.Command(v.Command[0], v.Command[1:]...)
v.Cmd.Dir = filepath.Join(v.Workdir)
return v.Cmd.Start()
}
func (v *ProcessConfig) StopProcess() error {
return v.Cmd.Process.Signal(os.Interrupt)
}

View File

@ -5,19 +5,29 @@ import (
"math/rand"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
)
type AppConfig struct {
Sites []SiteConfig `json:"sites"`
Sites []*SiteConfig `json:"sites"`
}
func (v *AppConfig) Forward(ctx *fiber.Ctx, site SiteConfig) error {
func (v *AppConfig) Forward(ctx *fiber.Ctx, site *SiteConfig) error {
if len(site.Upstreams) == 0 {
return errors.New("invalid configuration")
}
// Boot processes
for _, process := range site.Processes {
if err := process.BootProcess(); err != nil {
log.Warn().Err(err).Msgf("An error occurred when booting process (%s) for %s", process.ID, site.ID)
return fiber.ErrBadGateway
}
}
// Do forward
idx := rand.Intn(len(site.Upstreams))
upstream := &site.Upstreams[idx]
upstream := site.Upstreams[idx]
switch upstream.GetType() {
case UpstreamTypeHypertext:
@ -30,15 +40,16 @@ func (v *AppConfig) Forward(ctx *fiber.Ctx, site SiteConfig) error {
}
type SiteConfig struct {
ID string `json:"id"`
Rules []RouterRuleConfig `json:"rules"`
Transformers []RequestTransformerConfig `json:"transformers"`
Upstreams []UpstreamConfig `json:"upstreams"`
ID string `json:"id"`
Rules []*RouterRuleConfig `json:"rules" yaml:"rules"`
Transformers []*RequestTransformerConfig `json:"transformers" yaml:"transformers"`
Upstreams []*UpstreamConfig `json:"upstreams" yaml:"upstreams"`
Processes []*ProcessConfig `json:"processes" yaml:"processes"`
}
type RouterRuleConfig struct {
Host []string `json:"host"`
Path []string `json:"path"`
Queries map[string]string `json:"query"`
Headers map[string][]string `json:"headers"`
Host []string `json:"host" yaml:"host"`
Path []string `json:"path" yaml:"path"`
Queries map[string]string `json:"queries" yaml:"queries"`
Headers map[string][]string `json:"headers" yaml:"headers"`
}

View File

@ -1,9 +1,10 @@
package sign
import (
"github.com/gofiber/fiber/v2"
"regexp"
"strings"
"github.com/gofiber/fiber/v2"
)
type RequestTransformer struct {
@ -12,8 +13,8 @@ type RequestTransformer struct {
}
type RequestTransformerConfig struct {
Type string `json:"type"`
Options any `json:"options"`
Type string `json:"type" yaml:"type"`
Options any `json:"options" yaml:"options"`
}
func (v *RequestTransformerConfig) TransformRequest(ctx *fiber.Ctx) {

View File

@ -2,10 +2,11 @@ package sign
import (
"fmt"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"net/url"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
)
const (
@ -15,18 +16,16 @@ const (
)
type UpstreamConfig struct {
ID string `json:"id"`
URI string `json:"uri"`
ID string `json:"id" yaml:"id"`
URI string `json:"uri" yaml:"uri"`
}
func (v *UpstreamConfig) GetType() string {
protocol := strings.SplitN(v.URI, "://", 2)[0]
switch protocol {
case "file":
case "files":
case "file", "files":
return UpstreamTypeFile
case "http":
case "https":
case "http", "https":
return UpstreamTypeHypertext
}