package main import ( "errors" "math/rand" "os" "syscall" "time" "github.com/br0xen/boltrest" "github.com/nsf/termbox-go" ) var adminDB *boltrest.DB func main() { initialize() adminDB = boltrest.Open("admin.db") err := termbox.Init() if err != nil { panic(err) } defer termbox.Close() stl := getDefaultStyle() termbox.SetOutputMode(termbox.Output256) mainLoop(stl) } func mainLoop() { 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} } } 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 } 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 } 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) }