first draft of config-based servlet creation
This commit is contained in:
parent
97a50a6a34
commit
85587cd2eb
92
servlet/config.go
Normal file
92
servlet/config.go
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
package servlet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"geniuscartel.xyz/vinegar/vinegarUtil"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
ConfigType string
|
||||||
|
ConfigEntry struct {
|
||||||
|
ConfigType ConfigType
|
||||||
|
UrlPattern string
|
||||||
|
FileLocation string
|
||||||
|
UseBuiltinCache bool
|
||||||
|
}
|
||||||
|
Config struct {
|
||||||
|
ListeningAddress string
|
||||||
|
Routes []ConfigEntry
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Text ConfigType = "Text"
|
||||||
|
Image = "Image"
|
||||||
|
SingleFile = "SingleFile"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateBlankConfig() *Config {
|
||||||
|
conf := Config{
|
||||||
|
ListeningAddress: ":8080",
|
||||||
|
Routes: make([]ConfigEntry, 0, 10),
|
||||||
|
}
|
||||||
|
|
||||||
|
dummyRoute := ConfigEntry{
|
||||||
|
ConfigType: Text,
|
||||||
|
UrlPattern: "/*",
|
||||||
|
FileLocation: "errors/",
|
||||||
|
UseBuiltinCache: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
conf.Routes = append(conf.Routes, dummyRoute)
|
||||||
|
|
||||||
|
return &conf
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadConfig(pathlike string) *VinegarServlet {
|
||||||
|
contents, exists := vinegarUtil.GetDiskContent(pathlike)
|
||||||
|
if exists {
|
||||||
|
conf := Config{}
|
||||||
|
err := json.Unmarshal(*contents, &conf)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
servlet := NewServlet(conf.ListeningAddress)
|
||||||
|
for _, v := range conf.Routes {
|
||||||
|
constructor, err := getConstructorFunction(v.ConfigType)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
//we don't have to invoke servlet.AddRoute because the static routes already do that for us
|
||||||
|
constructor(servlet, v.UrlPattern, v.FileLocation, v.UseBuiltinCache)
|
||||||
|
|
||||||
|
}
|
||||||
|
return servlet
|
||||||
|
}
|
||||||
|
panic("Could not find config file at" + pathlike)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e ConfigEntry) toRoute(serv *VinegarServlet) {
|
||||||
|
constructor, err := getConstructorFunction(e.ConfigType)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
constructor(serv, e.UrlPattern, e.FileLocation, e.UseBuiltinCache)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConstructorFunction(t ConfigType) (RouteConstructor, error) {
|
||||||
|
switch t {
|
||||||
|
case Text:
|
||||||
|
return NewTextRoute, nil
|
||||||
|
case Image:
|
||||||
|
return NewImageRoute, nil
|
||||||
|
case SingleFile:
|
||||||
|
return NewSingleFileRoute, nil
|
||||||
|
}
|
||||||
|
return nil, errors.New(fmt.Sprintf("could not determine constructor for invalid ConfigType: %s", t))
|
||||||
|
}
|
||||||
@ -67,7 +67,7 @@ func (s *VinegarServlet) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
path := req.URL.Path
|
path := req.URL.Path
|
||||||
for _, route := range s.Routes {
|
for _, route := range s.Routes {
|
||||||
if route.Pattern.MatchString(path) {
|
if route.Pattern.MatchString(path) {
|
||||||
fmt.Printf("SERVING: [%s]=>{%s}\n", path, route.Pattern.String())
|
//fmt.Printf("SERVING: [%s]=>{%s}\n", path, route.Pattern.String())
|
||||||
route.Handler(w, req)
|
route.Handler(w, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,19 +14,11 @@ type (
|
|||||||
fileRoot string
|
fileRoot string
|
||||||
UseCache bool
|
UseCache bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RouteConstructor func(servlet *VinegarServlet, urlPattern string, pathlike string, useCache bool) *FileRoute
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewImageRoute(servlet *VinegarServlet, urlPattern string, pathlike string, useCache bool) *FileRoute {
|
var NewTextRoute RouteConstructor = func(servlet *VinegarServlet, urlPattern string, pathlike string, useCache bool) *FileRoute {
|
||||||
defaultPrune := strings.Replace(urlPattern, ".*", "", -1)
|
|
||||||
route := FileRoute{srv: servlet, fileRoot: pathlike, UseCache: useCache}
|
|
||||||
rootRoute := NewServletRoute(urlPattern, createUncompressedFileServletFunction(&route, defaultPrune, pathlike))
|
|
||||||
route.VinegarRoute = rootRoute //i *kinda* don't like this pattern
|
|
||||||
|
|
||||||
servlet.AddRoute(route.VinegarRoute)
|
|
||||||
return &route
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTextRoute(servlet *VinegarServlet, urlPattern string, pathlike string, useCache bool) *FileRoute {
|
|
||||||
defaultPrune := strings.Replace(urlPattern, ".*", "", -1)
|
defaultPrune := strings.Replace(urlPattern, ".*", "", -1)
|
||||||
route := FileRoute{srv: servlet, fileRoot: pathlike, UseCache: useCache}
|
route := FileRoute{srv: servlet, fileRoot: pathlike, UseCache: useCache}
|
||||||
textRouteHandler := createCompressibleFileServletFunction(&route, defaultPrune, pathlike)
|
textRouteHandler := createCompressibleFileServletFunction(&route, defaultPrune, pathlike)
|
||||||
@ -38,7 +30,17 @@ func NewTextRoute(servlet *VinegarServlet, urlPattern string, pathlike string, u
|
|||||||
return &route
|
return &route
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSingleFileRoute(servlet *VinegarServlet, urlPattern string, pathlike string, useCache bool) *FileRoute {
|
var NewImageRoute RouteConstructor = func(servlet *VinegarServlet, urlPattern string, pathlike string, useCache bool) *FileRoute {
|
||||||
|
defaultPrune := strings.Replace(urlPattern, ".*", "", -1)
|
||||||
|
route := FileRoute{srv: servlet, fileRoot: pathlike, UseCache: useCache}
|
||||||
|
rootRoute := NewServletRoute(urlPattern, createUncompressedFileServletFunction(&route, defaultPrune, pathlike))
|
||||||
|
route.VinegarRoute = rootRoute //i *kinda* don't like this pattern
|
||||||
|
|
||||||
|
servlet.AddRoute(route.VinegarRoute)
|
||||||
|
return &route
|
||||||
|
}
|
||||||
|
|
||||||
|
var NewSingleFileRoute RouteConstructor = func(servlet *VinegarServlet, urlPattern string, pathlike string, useCache bool) *FileRoute {
|
||||||
route := FileRoute{
|
route := FileRoute{
|
||||||
srv: servlet,
|
srv: servlet,
|
||||||
fileRoot: pathlike,
|
fileRoot: pathlike,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user