added config file loading, fixed bug with search shortcuts

remotes/origin/persistence
Adam Veldhousen 5 years ago
parent a57f276257
commit 4e74fd955b
Signed by: adam
GPG Key ID: 6DB29003C6DD1E4B

@ -0,0 +1,44 @@
package internal
import (
"os"
"encoding/json"
)
type Config struct {
ListenAddress string `json:"listenAddress"`
Shortcuts map[string]string `json:"shortcuts"`
}
func LoadShortcutsFromConfig(configPath string) (Config, error) {
var configuration Config
file, err := os.Open(configPath)
if err != nil {
return configuration, err
}
defer file.Close()
decoder := json.NewDecoder(file)
if err := decoder.Decode(&configuration); err != nil {
return configuration, err
}
if configuration.Shortcuts == nil {
configuration.Shortcuts = make(map[string]string)
}
if _, ok := configuration.Shortcuts["*"]; !ok {
configuration.Shortcuts["*"] = DefaultSearchProvider
}
configuration.Shortcuts["help"] = "/"
if configuration.ListenAddress == "" {
configuration.ListenAddress = "127.0.0.1"
}
return configuration, nil
}

@ -8,15 +8,14 @@ import (
"sync"
)
func NewServer(tp TemplateRenderer, accessLogging bool) http.Handler {
type Shortcuts map[string]string
func NewServer(tp TemplateRenderer, shorts Shortcuts, accessLogging bool) http.Handler {
mux := http.NewServeMux()
ss := &CommandHandler{
Mutex: &sync.Mutex{},
Shortcuts: map[string]string{
"*": DefaultSearchProvider,
"help": "/",
},
Shortcuts: shorts,
}
mux.HandleFunc("/", tp.RenderHandler("index.html", ss, nil))

@ -34,6 +34,7 @@ func (c *CommandHandler) Handle(input string) (Command, error) {
if fragmentCount > 1 {
shortcut = rawArgs[1]
parameters = strings.Join(rawArgs[1:], " ")
}
var updateShortcutParams string
@ -46,7 +47,7 @@ func (c *CommandHandler) Handle(input string) (Command, error) {
}
}
return c.getShortcut(farg, parameters), nil
return c.getShortcut(farg, parameters, input), nil
}
func (c *CommandHandler) updateShortcut(action, shortcut, location string) (Command, error) {
@ -84,11 +85,12 @@ func (c *CommandHandler) updateShortcut(action, shortcut, location string) (Comm
return command, nil
}
func (c *CommandHandler) getShortcut(key string, parameter string) Command {
func (c *CommandHandler) getShortcut(key, parameter, rawInput string) Command {
location, ok := c.Shortcuts[key]
if !ok {
location = DefaultSearchProvider
key = "*"
parameter = rawInput
}
if strings.Contains(location, "%s") {

@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"testing"
"sync"
)
func Test_Handle(t *testing.T) {
@ -36,6 +37,15 @@ func Test_Handle(t *testing.T) {
input: "add gh",
wantErr: true,
},
{
name: "add search shortcut: 'add so https://stackvoerflow.com?q=%s'",
input: "add so https://stackoverflow.com?q=%s",
want: Command{
Action: "add",
Name: "so",
Location: "https://stackoverflow.com?q=%s",
},
},
{
name: "remove shortcut: 'remove gh'",
input: "remove gh",
@ -67,13 +77,24 @@ func Test_Handle(t *testing.T) {
Location: "https://facebook.com",
},
},
{
name: "visit a search shortcut: 'go net/http'",
input: "go net/http",
want: Command{
Action: "lookup",
Name: "go",
Location: "https://godoc.org/net/http",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cm := &CommandHandler{
Mutex: &sync.Mutex{},
Shortcuts: map[string]string{
"fb": "https://facebook.com",
"go": "https://godoc.org/%s",
},
}

@ -12,17 +12,23 @@ import (
func main() {
port := flag.Int("port", 80, "port to listen on")
cfgPath := flag.String("config", "./config.json", "path to config file")
enableAccessLogging := flag.Bool("accesslog", true, "Enable access logging")
flag.Parse()
box := packr.NewBox("./internal/templates")
tp := internal.TemplateRenderer{FS: box}
server := internal.NewServer(tp, *enableAccessLogging)
config, err := internal.LoadShortcutsFromConfig(*cfgPath)
if err != nil {
log.Fatalf("could not load config from file: %v", err)
}
server := internal.NewServer(tp, config.Shortcuts, *enableAccessLogging)
log.SetPrefix("[INFO] ")
addr := fmt.Sprintf("0.0.0.0:%d", *port)
addr := fmt.Sprintf("%s:%d", config.ListenAddress, *port)
log.Printf("Listening @ %s", addr)
log.Fatal(http.ListenAndServe(addr, server))
}

@ -3,6 +3,9 @@ APP := riffraff
dev: clean .bin/$(APP)-dev
./.bin/$(APP)-dev -port 8080 -accesslog=true
test:
go test -v ./...
package:
docker build --build-arg VERSION=$${VERSION:-dev} -t vdhsn/$(APP):$${VERSION:-dev} .

Loading…
Cancel
Save