func New() *Config { cfg := Config{ TemplDir: "templates", SchemaURI: "file://schemas", Bind: "0.0.0.0:8080", Envelope: false, Indent: true, } hostname, err := os.Hostname() if err != nil { log.Fatal(err.Error()) } cfg.ServerURI = "http://" + hostname + ":8080" cfg.Etcd = Etcd{ Peers: "http://127.0.0.1:4001,http://127.0.0.1:2379", Timeout: time.Second, CmdTimeout: 5 * time.Second, } cfg.Routes = []Route{} return &cfg }
// deleteDoc delete document. func (c *config) deleteDoc(endpoint, path string) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { var newPath bytes.Buffer err := templ.ExecuteTemplate(&newPath, endpoint, mux.Vars(r)) if err != nil { log.Fatal(err.Error()) } log.Infof("etcd path: %s", newPath.String()) data, code, err := c.session.Get(newPath.String(), false, "") if err != nil { c.writeError(w, r, err, code) return } if code, err := c.session.Delete(newPath.String()); err != nil { c.writeError(w, r, err, code) return } c.write(w, r, data) } }
// getDoc get document. func (c *config) getDoc(endpoint, path string, collection bool, dirName string) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { var newPath bytes.Buffer table := false if collection == true && strings.ToLower(r.URL.Query().Get("table")) == "true" { table = true } err := templ.ExecuteTemplate(&newPath, endpoint, mux.Vars(r)) if err != nil { log.Fatal(err.Error()) } log.Infof("etcd path: %s", newPath.String()) doc, code, err := c.session.Get(newPath.String(), table, dirName) if err != nil { c.writeError(w, r, err, code) return } c.write(w, r, doc) } }
func (cfg *Config) Print(f string) { switch strings.ToLower(f) { case "json": b, err := json.MarshalIndent(cfg, "", " ") if err != nil { log.Fatal(err.Error()) } fmt.Println(string(b)) case "yaml": b, err := yaml.Marshal(cfg) if err != nil { log.Fatal(err.Error()) } fmt.Println(string(b)) case "toml": b := new(bytes.Buffer) if err := toml.NewEncoder(b).Encode(cfg); err != nil { log.Fatal(err.Error()) } fmt.Println(b.String()) default: log.Fatal("unsupported data format") } }
func (cfg *Config) Load(c *cli.Context) { // Enable debug. if c.GlobalBool("debug") { log.SetDebug() } // Default path for config file. u, _ := user.Current() cfgs := []string{ u.HomeDir + "/.etcdrest.json", u.HomeDir + "/.etcdrest.yaml", u.HomeDir + "/.etcdrest.yml", u.HomeDir + "/.etcdrest.toml", u.HomeDir + "/.etcdrest.tml", "/etc/etcdrest.json", "/etc/etcdrest.yaml", "/etc/etcdrest.yml", "/etc/etcdrest.toml", "/etc/etcdrest.tml", "/app/etc/etcdrest.json", "/app/etc/etcdrest.yaml", "/app/etc/etcdrest.yml", "/app/etc/etcdrest.toml", "/app/etc/etcdrest.tml", } // Check if we have an arg. for config file and that it exist's. if c.GlobalString("config") != "" { if _, err := os.Stat(c.GlobalString("config")); os.IsNotExist(err) { log.Fatalf("Config file doesn't exist: %s", c.GlobalString("config")) } cfgs = append([]string{c.GlobalString("config")}, cfgs...) } for _, fn := range cfgs { if _, err := os.Stat(fn); os.IsNotExist(err) { continue } log.Infof("Using config file: %s", fn) // Load config file. b, err := ioutil.ReadFile(fn) if err != nil { log.Fatal(err.Error()) } switch filepath.Ext(fn) { case ".json": if err := json.Unmarshal(b, cfg); err != nil { log.Fatal(err.Error()) } case ".yaml", ".yml": if err := yaml.Unmarshal(b, cfg); err != nil { log.Fatal(err.Error()) } case ".toml", ".tml": if err := toml.Unmarshal(b, cfg); err != nil { log.Fatal(err.Error()) } default: log.Fatal("unsupported data format") } // Validate config using JSON schema. break } // Override configuration. if c.GlobalString("templ-dir") != "" { cfg.TemplDir = c.GlobalString("templ-dir") } if c.GlobalString("schema-uri") != "" { cfg.SchemaURI = c.GlobalString("schema-uri") } if c.GlobalString("bind") != "" { cfg.Bind = c.GlobalString("bind") } if c.GlobalString("server-uri") != "" { cfg.ServerURI = c.GlobalString("server-uri") } if c.GlobalBool("envelope") { cfg.Envelope = true } if c.GlobalBool("no-indent") { cfg.Indent = true } // Override etcd configuration. if c.GlobalString("peers") != "" { cfg.Etcd.Peers = c.GlobalString("peers") } if c.GlobalString("cert") != "" { cfg.Etcd.Cert = c.GlobalString("cert") } if c.GlobalString("key") != "" { cfg.Etcd.Key = c.GlobalString("key") } if c.GlobalString("ca") != "" { cfg.Etcd.CA = c.GlobalString("ca") } if c.GlobalString("user") != "" { cfg.Etcd.User = c.GlobalString("user") } if c.GlobalDuration("timeout") != 0 { cfg.Etcd.Timeout = c.GlobalDuration("timeout") } if c.GlobalDuration("command-timeout") != 0 { cfg.Etcd.CmdTimeout = c.GlobalDuration("command-timeout") } }
// putOrPatchDoc put or patch document. func (c *config) putOrPatchDoc(endpoint, path, schema string) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { var newPath bytes.Buffer err := templ.ExecuteTemplate(&newPath, endpoint, mux.Vars(r)) if err != nil { log.Fatal(err.Error()) } log.Infof("etcd path: %s", newPath.String()) // Get request body. body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576)) if err != nil { c.writeError(w, r, err, http.StatusInternalServerError) return } if err := r.Body.Close(); err != nil { c.writeError(w, r, err, http.StatusInternalServerError) return } // Patch document using JSON patch RFC 6902. var doc []byte if r.Method == "PATCH" { data, code, err := c.session.Get(newPath.String(), false, "") if err != nil { c.writeError(w, r, err, code) return } origDoc, err := json.Marshal(&data) if err != nil { c.writeError(w, r, err, http.StatusInternalServerError) return } doc, err = c.patchDoc(origDoc, body) if err != nil { c.writeError(w, r, err, code) return } } else { doc = body } // Validate document using JSON schema if code, errors := c.validateDoc(doc, newPath.String(), schema); errors != nil { c.writeErrors(w, r, errors, code) return } var data interface{} if err := json.Unmarshal(doc, &data); err != nil { c.writeError(w, r, err, http.StatusInternalServerError) return } // Create document. if code, err := c.session.Put(newPath.String(), data); err != nil { c.writeError(w, r, err, code) return } c.write(w, r, data) } }
func runServer(c *cli.Context, cfg *config.Config) { // Set debug. if c.GlobalBool("debug") { log.SetDebug() } cfg.Load(c) // Print configuration. if c.GlobalIsSet("print-config") { cfg.Print(c.GlobalString("print-config")) os.Exit(0) } // Create etcd config. ec := etcd.New() ec.Peers(cfg.Etcd.Peers) ec.Cert(cfg.Etcd.Cert) ec.Key(cfg.Etcd.Key) ec.CA(cfg.Etcd.CA) ec.Timeout(cfg.Etcd.Timeout) ec.CmdTimeout(cfg.Etcd.CmdTimeout) // If user is set ask for password. if cfg.Etcd.User != "" { ec.User(cfg.Etcd.User) pass, err := speakeasy.Ask("Password: "******"api": sc.RouteEtcd(route.Collection, route.CollectionPath, route.Resource, route.ResourcePath, route.Schema, route.DirName) case "template": sc.RouteTemplate(route.Endpoint, route.Template) case "static": sc.RouteStatic(route.Endpoint, route.Path) default: log.Fatalf("Unknown type: %s for endpoint: %s", route.Type, route.Endpoint) } } // Check routes. if len(cfg.Routes) < 1 { log.Fatal("No routes specified.") } // Start server. if err := sc.Run(); err != nil { log.Fatal(err.Error()) } }