2016-02-17 14:30:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2016-02-23 03:23:53 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2016-02-17 14:30:43 +00:00
|
|
|
|
|
|
|
"github.com/br0xen/boltrest"
|
2016-02-23 03:23:53 +00:00
|
|
|
"github.com/nsf/termbox-go"
|
2016-02-17 14:30:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var adminDB *boltrest.DB
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
initialize()
|
|
|
|
adminDB = boltrest.Open("admin.db")
|
2016-02-23 03:23:53 +00:00
|
|
|
|
|
|
|
err := termbox.Init()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer termbox.Close()
|
|
|
|
stl := getDefaultStyle()
|
|
|
|
termbox.SetOutputMode(termbox.Output256)
|
|
|
|
mainLoop(stl)
|
2016-02-17 14:30:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 01:41:14 +00:00
|
|
|
func mainLoop() {
|
2016-02-23 03:23:53 +00:00
|
|
|
screens := defaultScreens()
|
|
|
|
displayScreen := screens[NasomiScreenIndex]
|
|
|
|
layoutAndDrawScreen(displayScreen, stl)
|
|
|
|
eventChan := make(chan termbox.Event)
|
|
|
|
go readUserInput(eventChan)
|
|
|
|
go sendNoneEvent(eventChan)
|
|
|
|
for {
|
|
|
|
event := <-eventChan
|
|
|
|
if event.Type == termbox.EventKey {
|
|
|
|
if event.Key == termbox.KeyCtrlZ {
|
|
|
|
process, _ := os.FindProcess(os.Getpid())
|
|
|
|
termbox.Close()
|
|
|
|
process.Signal(syscall.SIGSTOP)
|
|
|
|
termbox.Init()
|
|
|
|
} else if event.Key == termbox.KeyCtrlC {
|
|
|
|
termbox.Close()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
newScreenIndex := displayScreen.handleKeyEvent(event)
|
|
|
|
if newScreenIndex < len(screens) {
|
|
|
|
displayScreen = screens[newScreenIndex]
|
|
|
|
layoutAndDrawScreen(displayScreen, stl)
|
|
|
|
} else {
|
|
|
|
termbox.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if event.Type == termbox.EventResize || event.Type == termbox.EventNone {
|
|
|
|
layoutAndDrawScreen(displayScreen, stl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func readUserInput(e chan termbox.Event) {
|
|
|
|
for {
|
|
|
|
e <- termbox.PollEvent()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func sendNoneEvent(e chan termbox.Event) {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
e <- termbox.Event{Type: termbox.EventNone}
|
|
|
|
}
|
2016-02-21 01:41:14 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 14:30:43 +00:00
|
|
|
func initialize() error {
|
|
|
|
// Make sure that the necessary files/directories are in place
|
|
|
|
var tstDir *os.File
|
|
|
|
var tstDirInfo os.FileInfo
|
|
|
|
var err error
|
|
|
|
if tstDir, err = os.Open("dbs"); err != nil {
|
|
|
|
if err = os.Mkdir("dbs", 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if tstDir, err = os.Open("dbs"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tstDirInfo, err = tstDir.Stat(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !tstDirInfo.IsDir() {
|
|
|
|
return errors.New("'dbs' exists and is not a directory")
|
|
|
|
}
|
|
|
|
// We were able to open the db path and it was a directory
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-23 03:23:53 +00:00
|
|
|
const (
|
|
|
|
// ServerScreenIndex is the index
|
|
|
|
ServerScreenIndex = iota
|
|
|
|
// AboutScreenIndex The idx number for the 'About' Screen
|
|
|
|
AboutScreenIndex
|
|
|
|
// ExitScreenIndex The idx number for Exiting
|
|
|
|
ExitScreenIndex
|
|
|
|
)
|
|
|
|
|
|
|
|
func defaultScreens() []Screen {
|
|
|
|
serverScreen := ServerScreen{}
|
|
|
|
aboutScreen := AboutScreen{}
|
|
|
|
screens := []Screen{
|
|
|
|
&serverScreen,
|
|
|
|
&aboutScreen,
|
|
|
|
}
|
|
|
|
return screens
|
|
|
|
}
|
|
|
|
|
2016-02-17 14:30:43 +00:00
|
|
|
const keyChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
|
|
|
|
func generateAPIKey() string {
|
|
|
|
b := make([]byte, 34)
|
|
|
|
for i := range b {
|
|
|
|
if i%5 == 4 {
|
|
|
|
b[i] = '-'
|
|
|
|
} else {
|
|
|
|
b[i] = keyChars[rand.Int63()%int64(len(keyChars))]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(b)
|
2016-02-21 01:41:14 +00:00
|
|
|
}
|