add config file support, not done yet

master
codeskyblue 8 years ago
parent db7a1e1d14
commit 86b45b56af

@ -0,0 +1,45 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/go-yaml/yaml"
)
type Configuration struct {
Server struct {
HttpAuth struct {
User string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"httpauth"`
Addr string `yaml:"addr"`
} `yaml:"server"`
Client struct {
ServerURL string `yaml:"server_url"`
}
}
func readConf(filename string) (c Configuration, err error) {
// initial default value
c.Server.Addr = ":11313" // in memory of 08-31 13:13
c.Client.ServerURL = "http://localhost:11313"
data, err := ioutil.ReadFile(filename)
if err != nil {
data = []byte("")
}
err = yaml.Unmarshal(data, &c)
if err != nil {
return
}
cfgDir := filepath.Dir(filename)
if !IsDir(cfgDir) {
os.MkdirAll(cfgDir, 0755)
}
data, _ = yaml.Marshal(c)
err = ioutil.WriteFile(filename, data, 0644)
return
}

@ -8,6 +8,7 @@ import (
"net/http"
"os"
"os/exec"
"path/filepath"
"github.com/equinox-io/equinox"
"github.com/urfave/cli"
@ -24,6 +25,7 @@ MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEY8xsSkcFs8XXUicw3n7E77qN/vqKUQ/6
TxI79zIvne9UT/rDsM0BxSydwtjG00MT
-----END ECDSA PUBLIC KEY-----
`)
cfg Configuration
)
func equinoxUpdate(channel string) error {
@ -129,10 +131,34 @@ func actionVersion(c *cli.Context) error {
}
func main() {
var defaultConfigPath = filepath.Join(defaultConfigDir, "config.yml")
app := cli.NewApp()
app.Name = "gosuv"
app.Version = Version
app.Usage = "golang port of python-supervisor"
app.Before = func(c *cli.Context) error {
var err error
cfgPath := c.GlobalString("conf")
cfg, err = readConf(cfgPath)
if err != nil {
log.Fatal(err)
}
return nil
}
app.Authors = []cli.Author{
cli.Author{
Name: "codeskyblue",
Email: "codeskyblue@gmail.com",
},
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "conf, c",
Usage: "config file",
Value: defaultConfigPath,
},
}
app.Commands = []cli.Command{
{
Name: "start-server",
@ -147,6 +173,11 @@ func main() {
Usage: "listen address",
Value: ":8000",
},
cli.StringFlag{
Name: "conf, c",
Usage: "config file",
Value: defaultConfigPath,
},
},
Action: actionStartServer,
},

@ -21,6 +21,15 @@ import (
"github.com/qiniu/log"
)
var defaultConfigDir string
func init() {
defaultConfigDir = os.Getenv("GOSUV_HOME_DIR")
if defaultConfigDir == "" {
defaultConfigDir = filepath.Join(UserHomeDir(), ".gosuv")
}
}
type Supervisor struct {
ConfigDir string
pgs []*Program
@ -161,11 +170,6 @@ func (s *Supervisor) loadDB() error {
}
func (s *Supervisor) saveDB() error {
dir := filepath.Dir(s.programPath())
if !IsDir(dir) {
os.MkdirAll(dir, 0755)
}
data, err := yaml.Marshal(s.pgs)
if err != nil {
return err
@ -174,11 +178,8 @@ func (s *Supervisor) saveDB() error {
}
func (s *Supervisor) renderHTML(w http.ResponseWriter, name string, data interface{}) {
w.Header().Set("Content-Type", "text/html")
executeTemplate(w, name, data)
// baseName := filepath.Base(name)
// t := template.Must(template.New("t").Delims("[[", "]]").ParseFiles(name))
// t.ExecuteTemplate(w, baseName, data)
}
type JSONResponse struct {
@ -398,8 +399,6 @@ func (s *Supervisor) catchExitSignal() {
}()
}
var defaultConfigDir = filepath.Join(UserHomeDir(), ".gosuv")
func registerHTTPHandlers() error {
suv := &Supervisor{
ConfigDir: defaultConfigDir,

Loading…
Cancel
Save