Skip to content

Commit

Permalink
- remove configuration feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkarnez committed Jul 27, 2020
1 parent ff43efa commit b716ed0
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 117 deletions.
26 changes: 2 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,10 @@ staticserver
staticserver
[--port={port | 80 default}] \
[--root={root (default to working directory)}] \
[--mode={spa | fs default}] \
[--config={config JSON file path (optional)}]
[--mode={spa | upload | fs default}]
```

### How config JSON file looks like
```
[
{
"path": "endpoint",
"source": "{ url for json }",
"template": "{ jsonquery on {source}, in golang array-templated string }",
"arguments": [
"//nodeName",
"/internalEndpoint/ports/*[port=8080]/nodePort"
]
}
]
```

### jsonquery
- [github.com/antchfx/jsonquery](github.com/antchfx/jsonquery)

### Golang array-templated string format
- ```{{index . 0}}{{index . 1}}{{index . 2}}....```

### Warning(s)
- The mime-type in Windows registry may be modified by other software causing wrong `Content-Type` in response, see issue(s)
- https://github.com/golang/go/issues/32350
- How to fix: `regedit` -> go to `Computer\HKEY_CLASSES_ROOT\.js` -> set `Content Type` to `application/javascript`
- How to fix: `regedit` -> go to `Computer\HKEY_CLASSES_ROOT\.js` -> set `Content Type` to `application/javascript`
184 changes: 91 additions & 93 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,46 @@
package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"github.com/antchfx/jsonquery"
"github.com/gin-gonic/gin"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
textTemplate "text/template"

"github.com/gin-gonic/gin"
)

type Config struct {
Path string `json:"path"`
Source string `json:"source"`
Template string `json:"template"`
Arguments []string `json:"arguments"`
}
// type Config struct {
// Path string `json:"path"`
// Source string `json:"source"`
// Template string `json:"template"`
// Arguments []string `json:"arguments"`
// }

var (
port string
root string
mode string
configFilePath string
configMap map[string]Config
router *gin.Engine
port uint64
root string
mode string
// configFilePath string
// configMap map[string]Config
router *gin.Engine
)

func init() {
configMap = make(map[string]Config)
// configMap = make(map[string]Config)

gin.SetMode(gin.ReleaseMode)
router = gin.Default()
}

func main() {
flag.StringVar(&root, "root", "", "Absolute path for root directory")
flag.StringVar(&port, "port", "", "Port, default is 80")
flag.StringVar(&mode, "mode", "", "Mode: fs, spa, upload. Default fs mode")
flag.StringVar(&configFilePath, "config", "", "Config file path")
flag.Uint64Var(&port, "port", 80, "Port, default is 80")
flag.StringVar(&mode, "mode", "fs", "Mode: fs, spa, upload. Default fs mode")
// flag.StringVar(&configFilePath, "config", "", "Config file path")
flag.Parse()

if len(root) < 1 {
Expand All @@ -55,31 +51,27 @@ func main() {
root = dir
}

if len(port) < 1 {
port = "80"
if port > 1<<16-1 {
log.Fatal("Port number too large")
}

if len(mode) < 1 {
mode = "fs"
}
// if len(configFilePath) > 0 {
// raw, err := ioutil.ReadFile(configFilePath)
// if err != nil {
// fmt.Println("Config file not found. No configuration is loaded.")
// }

if len(configFilePath) > 0 {
raw, err := ioutil.ReadFile(configFilePath)
if err != nil {
fmt.Println("Config file not found. No configuration is loaded.")
}
// var configArr []Config
// if err := json.Unmarshal(raw, &configArr); err != nil {
// fmt.Println("Cannot parse config file. No configuration is loaded.")
// }

var configArr []Config
if err := json.Unmarshal(raw, &configArr); err != nil {
fmt.Println("Cannot parse config file. No configuration is loaded.")
}

for _, config := range configArr {
configPath := config.Path
configMap[configPath] = config
router.POST(configPath, handlerCreator(configPath))
}
}
// for _, config := range configArr {
// configPath := config.Path
// configMap[configPath] = config
// router.POST(configPath, handlerCreator(configPath))
// }
// }

switch mode {
case "spa":
Expand Down Expand Up @@ -138,7 +130,7 @@ func main() {
router.SetHTMLTemplate(template.Must(template.New("index").Parse(tpl)))

router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK,"index", gin.H{})
c.HTML(http.StatusOK, "index", gin.H{})
})

router.POST("/upload", func(c *gin.Context) {
Expand All @@ -158,59 +150,65 @@ func main() {
})
case "fs":
router.StaticFS("/", gin.Dir(root, true))
default:
log.Fatalf("%s mode is not supported\n", mode)
}

log.Println(fmt.Sprintf("Listening on %s, serving %s, in %s mode", port, root, mode))
err := router.Run(fmt.Sprintf(":%s", port))
log.Println(fmt.Sprintf("Listening on %d, serving %s, in %s mode", port, root, mode))
err := router.Run(fmt.Sprintf(":%d", port))
if err != nil {
log.Fatal(err)
}
}

func handlerCreator(key string) func(c *gin.Context) {
return func(c *gin.Context) {
value, ok := configMap[key]
if !ok {
c.AbortWithStatus(404)
} else {
source := value.Source
template := value.Template
arguments := value.Arguments

if len(template) > 1 {
doc, err := jsonquery.LoadURL(source)
if err != nil {
c.AbortWithStatus(404)
}

tmpl, err := textTemplate.New("template").Parse(template)
if err != nil {
c.AbortWithStatus(404)
return
}

buf := new(bytes.Buffer)

queries := make([]string, len(arguments))
for i, argument := range arguments {
nodeNameNode := jsonquery.FindOne(doc, argument)
if nodeNameNode != nil {
queries[i] = nodeNameNode.InnerText()
} else {
queries[i] = ""
}
}

err = tmpl.Execute(buf, queries)
if err != nil {
c.AbortWithStatus(404)
return
}

c.JSON(200, buf.String())
} else {
c.JSON(200, source)
}
}
}
}
// func handlerCreator(key string) func(c *gin.Context) {
// return func(c *gin.Context) {
// value, ok := configMap[key]
// if !ok {
// c.AbortWithStatus(404)
// } else {
// source := value.Source
// template := value.Template
// arguments := value.Arguments

// doc, err := jsonquery.LoadURL(source)
// if err != nil {
// c.AbortWithStatus(404)
// }

// if len(template) > 1 {
// argumentsLen := len(arguments)

// if argumentsLen > 0 {
// tmpl, err := textTemplate.New("template").Parse(template)
// if err != nil {
// c.AbortWithStatus(404)
// return
// }

// buf := new(bytes.Buffer)
// queries := make([]string, argumentsLen)
// for i, argument := range arguments {
// nodeNameNode := jsonquery.FindOne(doc, argument)
// if nodeNameNode != nil {
// queries[i] = nodeNameNode.InnerText()
// } else {
// queries[i] = ""
// }
// }

// err = tmpl.Execute(buf, queries)
// if err != nil {
// c.AbortWithStatus(404)
// return
// }

// c.JSON(200, buf.String())
// }

// } else {
// c.JSON(200, source)
// }
// }
// }
// }

0 comments on commit b716ed0

Please sign in to comment.