🎉 Basic Web Sideload

This commit is contained in:
2024-01-01 18:07:21 +08:00
parent 9a5c5e9fca
commit 86b65cd21f
35 changed files with 3818 additions and 19 deletions

View File

@ -2,12 +2,25 @@ package sign
import (
"fmt"
"github.com/samber/lo"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
type ProcessConfig struct {
type ProcessStatus = int8
const (
ProcessCreated = ProcessStatus(iota)
ProcessStarting
ProcessStarted
ProcessExited
ProcessFailure
)
type ProcessInstance struct {
ID string `json:"id" yaml:"id"`
Workdir string `json:"workdir" yaml:"workdir"`
Command []string `json:"command" yaml:"command"`
@ -15,10 +28,14 @@ type ProcessConfig struct {
Prepares [][]string `json:"prepares" yaml:"prepares"`
Preheat bool `json:"preheat" yaml:"preheat"`
Cmd *exec.Cmd `json:"-"`
Cmd *exec.Cmd `json:"-"`
Logger strings.Builder `json:"-"`
Status ProcessStatus `json:"status"`
Logs string `json:"logs"`
}
func (v *ProcessConfig) BootProcess() error {
func (v *ProcessInstance) BootProcess() error {
if v.Cmd != nil {
return nil
}
@ -43,7 +60,7 @@ func (v *ProcessConfig) BootProcess() error {
}
}
func (v *ProcessConfig) PrepareProcess() error {
func (v *ProcessInstance) PrepareProcess() error {
for _, script := range v.Prepares {
if len(script) <= 0 {
continue
@ -57,7 +74,7 @@ func (v *ProcessConfig) PrepareProcess() error {
return nil
}
func (v *ProcessConfig) StartProcess() error {
func (v *ProcessInstance) StartProcess() error {
if len(v.Command) <= 0 {
return fmt.Errorf("you need set the command for %s to enable process manager", v.ID)
}
@ -65,11 +82,28 @@ func (v *ProcessConfig) StartProcess() error {
v.Cmd = exec.Command(v.Command[0], v.Command[1:]...)
v.Cmd.Dir = filepath.Join(v.Workdir)
v.Cmd.Env = append(v.Cmd.Env, v.Environment...)
v.Cmd.Stdout = &v.Logger
v.Cmd.Stderr = &v.Logger
// Monitor
go func() {
for {
if v.Cmd.Process == nil || v.Cmd.ProcessState == nil {
v.Status = ProcessStarting
} else if !v.Cmd.ProcessState.Exited() {
v.Status = ProcessStarted
} else {
v.Status = lo.Ternary(v.Cmd.ProcessState.Success(), ProcessExited, ProcessFailure)
return
}
time.Sleep(100 * time.Millisecond)
}
}()
return v.Cmd.Start()
}
func (v *ProcessConfig) StopProcess() error {
func (v *ProcessInstance) StopProcess() error {
if v.Cmd != nil && v.Cmd.Process != nil {
if err := v.Cmd.Process.Signal(os.Interrupt); err != nil {
v.Cmd.Process.Kill()
@ -82,8 +116,13 @@ func (v *ProcessConfig) StopProcess() error {
return nil
}
func (v *ProcessInstance) GetLogs() string {
v.Logs = v.Logger.String()
return v.Logs
}
func (v *RoadApp) PreheatProcesses(callbacks ...func(total int, success int)) {
var processes []*ProcessConfig
var processes []*ProcessInstance
for _, site := range v.Sites {
for _, process := range site.Processes {
if process.Preheat {

View File

@ -18,7 +18,7 @@ import (
"github.com/valyala/fasthttp"
)
func makeHypertextResponse(c *fiber.Ctx, upstream *UpstreamConfig) error {
func makeHypertextResponse(c *fiber.Ctx, upstream *UpstreamInstance) error {
timeout := time.Duration(viper.GetInt64("performance.network_timeout")) * time.Millisecond
return proxy.Do(c, upstream.MakeURI(c), &fasthttp.Client{
ReadTimeout: timeout,
@ -26,7 +26,7 @@ func makeHypertextResponse(c *fiber.Ctx, upstream *UpstreamConfig) error {
})
}
func makeFileResponse(c *fiber.Ctx, upstream *UpstreamConfig) error {
func makeFileResponse(c *fiber.Ctx, upstream *UpstreamInstance) error {
uri, queries := upstream.GetRawURI()
root := http.Dir(uri)

View File

@ -44,13 +44,13 @@ type RequestTransformerConfig = transformers.RequestTransformerConfig
type SiteConfig struct {
ID string `json:"id"`
Rules []*RouterRuleConfig `json:"rules" yaml:"rules"`
Rules []*RouterRule `json:"rules" yaml:"rules"`
Transformers []*RequestTransformerConfig `json:"transformers" yaml:"transformers"`
Upstreams []*UpstreamConfig `json:"upstreams" yaml:"upstreams"`
Processes []*ProcessConfig `json:"processes" yaml:"processes"`
Upstreams []*UpstreamInstance `json:"upstreams" yaml:"upstreams"`
Processes []*ProcessInstance `json:"processes" yaml:"processes"`
}
type RouterRuleConfig struct {
type RouterRule struct {
Host []string `json:"host" yaml:"host"`
Path []string `json:"path" yaml:"path"`
Queries map[string]string `json:"queries" yaml:"queries"`

View File

@ -15,12 +15,12 @@ const (
UpstreamTypeUnknown = "unknown"
)
type UpstreamConfig struct {
type UpstreamInstance struct {
ID string `json:"id" yaml:"id"`
URI string `json:"uri" yaml:"uri"`
}
func (v *UpstreamConfig) GetType() string {
func (v *UpstreamInstance) GetType() string {
protocol := strings.SplitN(v.URI, "://", 2)[0]
switch protocol {
case "file", "files":
@ -32,7 +32,7 @@ func (v *UpstreamConfig) GetType() string {
return UpstreamTypeUnknown
}
func (v *UpstreamConfig) GetRawURI() (string, url.Values) {
func (v *UpstreamInstance) GetRawURI() (string, url.Values) {
uri := strings.SplitN(v.URI, "://", 2)[1]
data := strings.SplitN(uri, "?", 2)
data = append(data, " ") // Make data array least have two element
@ -41,7 +41,7 @@ func (v *UpstreamConfig) GetRawURI() (string, url.Values) {
return data[0], qs
}
func (v *UpstreamConfig) MakeURI(ctx *fiber.Ctx) string {
func (v *UpstreamInstance) MakeURI(ctx *fiber.Ctx) string {
var queries []string
for k, v := range ctx.Queries() {
parsed, _ := url.QueryUnescape(v)