✨ Static Files Hosting
This commit is contained in:
58
pkg/sign/configurator.go
Normal file
58
pkg/sign/configurator.go
Normal file
@ -0,0 +1,58 @@
|
||||
package sign
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var C *AppConfig
|
||||
|
||||
func ReadInConfig(root string) error {
|
||||
cfg := &AppConfig{
|
||||
Sites: []SiteConfig{},
|
||||
}
|
||||
|
||||
if err := filepath.Walk(root, func(fp string, info os.FileInfo, err error) error {
|
||||
var site SiteConfig
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
} else if file, err := os.OpenFile(fp, os.O_RDONLY, 0755); err != nil {
|
||||
return err
|
||||
} else if data, err := io.ReadAll(file); err != nil {
|
||||
return err
|
||||
} else if err := json.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)
|
||||
}
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
C = cfg
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SaveInConfig(root string, cfg *AppConfig) error {
|
||||
for _, site := range cfg.Sites {
|
||||
data, _ := json.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 {
|
||||
return err
|
||||
} else if _, err := file.Write(data); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
10
pkg/sign/deserializer.go
Normal file
10
pkg/sign/deserializer.go
Normal file
@ -0,0 +1,10 @@
|
||||
package sign
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func DeserializeOptions[T any](data any) T {
|
||||
var out T
|
||||
raw, _ := json.Marshal(data)
|
||||
_ = json.Unmarshal(raw, &out)
|
||||
return out
|
||||
}
|
78
pkg/sign/router.go
Normal file
78
pkg/sign/router.go
Normal file
@ -0,0 +1,78 @@
|
||||
package sign
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/filesystem"
|
||||
"github.com/gofiber/fiber/v2/middleware/proxy"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/valyala/fasthttp"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AppConfig struct {
|
||||
Sites []SiteConfig `json:"sites"`
|
||||
}
|
||||
|
||||
func (v *AppConfig) Forward(ctx *fiber.Ctx, site SiteConfig) error {
|
||||
if len(site.Upstreams) == 0 {
|
||||
return errors.New("invalid configuration")
|
||||
}
|
||||
|
||||
idx := rand.Intn(len(site.Upstreams))
|
||||
upstream := &site.Upstreams[idx]
|
||||
|
||||
switch upstream.GetType() {
|
||||
case UpstreamTypeHypertext:
|
||||
timeout := time.Duration(viper.GetInt64("performance.network_timeout")) * time.Millisecond
|
||||
return proxy.Do(ctx, upstream.MakeURI(ctx), &fasthttp.Client{
|
||||
ReadTimeout: timeout,
|
||||
WriteTimeout: timeout,
|
||||
})
|
||||
case UpstreamTypeFile:
|
||||
uri, queries := upstream.GetRawURI()
|
||||
fs := filesystem.New(filesystem.Config{
|
||||
Root: http.Dir(uri),
|
||||
ContentTypeCharset: queries.Get("charset"),
|
||||
Index: func() string {
|
||||
val := queries.Get("index")
|
||||
return lo.Ternary(len(val) > 0, val, "index.html")
|
||||
}(),
|
||||
NotFoundFile: func() string {
|
||||
val := queries.Get("fallback")
|
||||
return lo.Ternary(len(val) > 0, val, "404.html")
|
||||
}(),
|
||||
Browse: func() bool {
|
||||
browse, err := strconv.ParseBool(queries.Get("browse"))
|
||||
return lo.Ternary(err == nil, browse, false)
|
||||
}(),
|
||||
MaxAge: func() int {
|
||||
age, err := strconv.Atoi(queries.Get("maxAge"))
|
||||
return lo.Ternary(err == nil, age, 3600)
|
||||
}(),
|
||||
})
|
||||
return fs(ctx)
|
||||
default:
|
||||
return fiber.ErrBadGateway
|
||||
}
|
||||
}
|
||||
|
||||
type SiteConfig struct {
|
||||
ID string `json:"id"`
|
||||
|
||||
Name string `json:"name"`
|
||||
Rules []RouterRuleConfig `json:"rules"`
|
||||
Transformers []RequestTransformerConfig `json:"transformers"`
|
||||
Upstreams []UpstreamConfig `json:"upstreams"`
|
||||
}
|
||||
|
||||
type RouterRuleConfig struct {
|
||||
Host []string `json:"host"`
|
||||
Path []string `json:"path"`
|
||||
Queries map[string]string `json:"query"`
|
||||
Headers map[string][]string `json:"headers"`
|
||||
}
|
58
pkg/sign/transformer.go
Normal file
58
pkg/sign/transformer.go
Normal file
@ -0,0 +1,58 @@
|
||||
package sign
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RequestTransformer struct {
|
||||
ModifyRequest func(options any, ctx *fiber.Ctx)
|
||||
ModifyResponse func(options any, ctx *fiber.Ctx)
|
||||
}
|
||||
|
||||
type RequestTransformerConfig struct {
|
||||
Type string `json:"type"`
|
||||
Options any `json:"options"`
|
||||
}
|
||||
|
||||
func (v *RequestTransformerConfig) TransformRequest(ctx *fiber.Ctx) {
|
||||
for k, f := range Transformers {
|
||||
if k == v.Type {
|
||||
if f.ModifyRequest != nil {
|
||||
f.ModifyRequest(v.Options, ctx)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *RequestTransformerConfig) TransformResponse(ctx *fiber.Ctx) {
|
||||
for k, f := range Transformers {
|
||||
if k == v.Type {
|
||||
if f.ModifyResponse != nil {
|
||||
f.ModifyResponse(v.Options, ctx)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var Transformers = map[string]RequestTransformer{
|
||||
"replacePath": {
|
||||
ModifyRequest: func(options any, ctx *fiber.Ctx) {
|
||||
opts := DeserializeOptions[struct {
|
||||
Pattern string `json:"pattern"`
|
||||
Value string `json:"value"`
|
||||
Repl string `json:"repl"` // Use when complex mode(regexp) enabled
|
||||
Complex bool `json:"complex"`
|
||||
}](options)
|
||||
path := string(ctx.Request().URI().Path())
|
||||
if !opts.Complex {
|
||||
ctx.Path(strings.ReplaceAll(path, opts.Pattern, opts.Value))
|
||||
} else if ex := regexp.MustCompile(opts.Pattern); ex != nil {
|
||||
ctx.Path(ex.ReplaceAllString(path, opts.Repl))
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
55
pkg/sign/upstream.go
Normal file
55
pkg/sign/upstream.go
Normal file
@ -0,0 +1,55 @@
|
||||
package sign
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
UpstreamTypeFile = "file"
|
||||
UpstreamTypeHypertext = "hypertext"
|
||||
UpstreamTypeUnknown = "unknown"
|
||||
)
|
||||
|
||||
type UpstreamConfig struct {
|
||||
Name string `json:"name"`
|
||||
URI string `json:"uri"`
|
||||
}
|
||||
|
||||
func (v *UpstreamConfig) GetType() string {
|
||||
protocol := strings.SplitN(v.URI, "://", 2)[0]
|
||||
switch protocol {
|
||||
case "file":
|
||||
return UpstreamTypeFile
|
||||
case "http":
|
||||
case "https":
|
||||
return UpstreamTypeHypertext
|
||||
}
|
||||
|
||||
return UpstreamTypeUnknown
|
||||
}
|
||||
|
||||
func (v *UpstreamConfig) GetRawURI() (string, url.Values) {
|
||||
uri := strings.SplitN(v.URI, "://", 2)[1]
|
||||
data := strings.SplitN(uri, "?", 2)
|
||||
qs, _ := url.ParseQuery(uri)
|
||||
|
||||
return data[0], qs
|
||||
}
|
||||
|
||||
func (v *UpstreamConfig) MakeURI(ctx *fiber.Ctx) string {
|
||||
var queries []string
|
||||
for k, v := range ctx.Queries() {
|
||||
queries = append(queries, fmt.Sprintf("%s=%s", k, v))
|
||||
}
|
||||
|
||||
path := string(ctx.Request().URI().Path())
|
||||
hash := string(ctx.Request().URI().Hash())
|
||||
|
||||
return v.URI + path +
|
||||
lo.Ternary(len(queries) > 0, "?"+strings.Join(queries, "&"), "") +
|
||||
lo.Ternary(len(hash) > 0, "#"+hash, "")
|
||||
}
|
Reference in New Issue
Block a user