Add dockerfile, add data directory flag

This commit is contained in:
Ken-Håvard Lieng 2015-05-29 01:59:08 +02:00
parent 1f5dd7a5e0
commit 284a35ab09
9 changed files with 81 additions and 62 deletions

3
.dockerignore Normal file
View File

@ -0,0 +1,3 @@
.git
client/dist
client/node_modules

4
.gitignore vendored
View File

@ -1,2 +1,2 @@
client/dist/ client/dist
client/node_modules/ client/node_modules

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM golang
ADD . /go/src/github.com/khlieng/name_pending
RUN go install github.com/khlieng/name_pending
VOLUME ["/data"]
ENTRYPOINT ["/go/bin/name_pending"]
CMD ["-p=8080", "--dir=/data"]

View File

@ -10,6 +10,6 @@ var clearCmd = &cobra.Command{
Use: "clear", Use: "clear",
Short: "Clear all application data", Short: "Clear all application data",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
storage.Clear() storage.Clear(appDir)
}, },
} }

View File

@ -7,19 +7,15 @@ import (
"path" "path"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/khlieng/name_pending/storage"
) )
var ( var (
editors = []string{"nano", "notepad", "vi", "emacs"}
configCmd = &cobra.Command{ configCmd = &cobra.Command{
Use: "config", Use: "config",
Short: "Edit config file", Short: "Edit config file",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if editor := findEditor(); editor != "" { if editor := findEditor(); editor != "" {
process := exec.Command(editor, path.Join(storage.AppDir, "config.toml")) process := exec.Command(editor, path.Join(appDir, "config.toml"))
process.Stdin = os.Stdin process.Stdin = os.Stdin
process.Stdout = os.Stdout process.Stdout = os.Stdout
process.Stderr = os.Stderr process.Stderr = os.Stderr
@ -29,6 +25,8 @@ var (
} }
}, },
} }
editors = []string{"nano", "notepad", "vi", "emacs"}
) )
func findEditor() string { func findEditor() string {

View File

@ -1,9 +1,16 @@
package commands package commands
import ( import (
"io/ioutil"
"log"
"os"
"os/user"
"path"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra" "github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/viper" "github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/viper"
"github.com/khlieng/name_pending/assets"
"github.com/khlieng/name_pending/server" "github.com/khlieng/name_pending/server"
"github.com/khlieng/name_pending/storage" "github.com/khlieng/name_pending/storage"
) )
@ -13,10 +20,25 @@ var (
Use: "name_pending", Use: "name_pending",
Short: "Web-based IRC client in Go.", Short: "Web-based IRC client in Go.",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
storage.Initialize() storage.Initialize(appDir)
server.Run(viper.GetInt("port")) server.Run(viper.GetInt("port"))
}, },
PersistentPreRun: func(cmd *cobra.Command, args []string) {
appDir = viper.GetString("dir")
os.Mkdir(appDir, 0777)
os.Mkdir(path.Join(appDir, "logs"), 0777)
initConfig()
viper.SetConfigName("config")
viper.AddConfigPath(appDir)
viper.ReadInConfig()
},
} }
appDir string
) )
func init() { func init() {
@ -24,14 +46,38 @@ func init() {
rootCmd.AddCommand(configCmd) rootCmd.AddCommand(configCmd)
rootCmd.Flags().IntP("port", "p", 1337, "port to listen on") rootCmd.Flags().IntP("port", "p", 1337, "port to listen on")
rootCmd.PersistentFlags().String("dir", defaultDir(), "directory to store config and data in")
viper.SetConfigName("config")
viper.AddConfigPath(storage.AppDir)
viper.ReadInConfig()
viper.BindPFlag("port", rootCmd.Flags().Lookup("port")) viper.BindPFlag("port", rootCmd.Flags().Lookup("port"))
viper.BindPFlag("dir", rootCmd.PersistentFlags().Lookup("dir"))
} }
func Execute() { func Execute() {
rootCmd.Execute() rootCmd.Execute()
} }
func initConfig() {
configPath := path.Join(appDir, "config.toml")
if _, err := os.Stat(configPath); os.IsNotExist(err) {
config, err := assets.Asset("config.default.toml")
if err != nil {
log.Println(err)
return
}
err = ioutil.WriteFile(configPath, config, 0600)
if err != nil {
log.Println(err)
}
}
}
func defaultDir() string {
currentUser, err := user.Current()
if err != nil {
log.Fatal(err)
}
return path.Join(currentUser.HomeDir, ".name_pending")
}

25
main.go
View File

@ -1,34 +1,9 @@
package main package main
import ( import (
"io/ioutil"
"log"
"os"
"path"
"github.com/khlieng/name_pending/assets"
"github.com/khlieng/name_pending/commands" "github.com/khlieng/name_pending/commands"
"github.com/khlieng/name_pending/storage"
) )
func main() { func main() {
initConfig()
commands.Execute() commands.Execute()
} }
func initConfig() {
configPath := path.Join(storage.AppDir, "config.toml")
if _, err := os.Stat(configPath); os.IsNotExist(err) {
config, err := assets.Asset("config.default.toml")
if err != nil {
log.Println(err)
return
}
err = ioutil.WriteFile(configPath, config, 0600)
if err != nil {
log.Println(err)
}
}
}

View File

@ -3,16 +3,14 @@ package storage
import ( import (
"log" "log"
"os" "os"
"os/user"
"path" "path"
"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/boltdb/bolt" "github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/boltdb/bolt"
) )
var ( var (
AppDir string appDir string
db *bolt.DB
db *bolt.DB
bucketUsers = []byte("Users") bucketUsers = []byte("Users")
bucketServers = []byte("Servers") bucketServers = []byte("Servers")
@ -20,24 +18,13 @@ var (
bucketMessages = []byte("Messages") bucketMessages = []byte("Messages")
) )
func init() { func Initialize(dir string) {
currentUser, err := user.Current()
if err != nil {
log.Fatal(err)
}
AppDir = path.Join(currentUser.HomeDir, ".name_pending")
os.Mkdir(AppDir, 0777)
os.Mkdir(path.Join(AppDir, "logs"), 0777)
}
func Initialize() {
var err error var err error
appDir = dir
log.Println("Storing data at", AppDir) log.Println("Storing data at", dir)
db, err = bolt.Open(path.Join(AppDir, "data.db"), 0600, nil) db, err = bolt.Open(path.Join(dir, "data.db"), 0600, nil)
if err != nil { if err != nil {
log.Fatal("Could not open database file") log.Fatal("Could not open database file")
} }
@ -55,7 +42,7 @@ func Close() {
db.Close() db.Close()
} }
func Clear() { func Clear(dir string) {
os.RemoveAll(path.Join(AppDir, "logs")) os.RemoveAll(path.Join(dir, "logs"))
os.Remove(path.Join(AppDir, "data.db")) os.Remove(path.Join(dir, "data.db"))
} }

View File

@ -304,7 +304,7 @@ func (u *User) SearchMessages(server, channel, phrase string) ([]Message, error)
func (u *User) openMessageLog() { func (u *User) openMessageLog() {
var err error var err error
u.messageLog, err = bolt.Open(path.Join(AppDir, "logs", u.UUID+"_log"), 0600, nil) u.messageLog, err = bolt.Open(path.Join(appDir, "logs", u.UUID+"_log"), 0600, nil)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -315,7 +315,7 @@ func (u *User) openMessageLog() {
return nil return nil
}) })
indexPath := path.Join(AppDir, "logs", u.UUID+"_index") indexPath := path.Join(appDir, "logs", u.UUID+"_index")
u.messageIndex, err = bleve.Open(indexPath) u.messageIndex, err = bleve.Open(indexPath)
if err == bleve.ErrorIndexPathDoesNotExist { if err == bleve.ErrorIndexPathDoesNotExist {
mapping := bleve.NewIndexMapping() mapping := bleve.NewIndexMapping()