diff --git a/config.go b/config.go new file mode 100644 index 0000000..9438bab --- /dev/null +++ b/config.go @@ -0,0 +1,63 @@ +package main + +import ( + "os" + "flag" + "io/ioutil" + "gopkg.in/yaml.v2" + "log" +) + +type config struct { + port string + git struct { + url string + username string + password string + clonepath string + } +} + + +func setup() { + +} + +func flags() config { + var flags config + flags.port = *flag.String("port", "8888", "server port") + flags.git.url = *flag.String("git-url", "", "git repository url") + flags.git.username = *flag.String("git-username", "", "git repository username") + flags.git.password = *flag.String("git-password", "", "git repository password") + flags.git.clonepath = *flag.String("git-clonepath", ".filesystem-local","git clone directory path") + + return flags +} + +func envvars() config { + var envvars config + envvars.port = os.Getenv("MYAC_PORT") + envvars.git.url = os.Getenv("MYAC_GIT_URL") + envvars.git.username = os.Getenv("MYAC_GIT_USERNAME") + envvars.git.password = os.Getenv("MYAC_GIT_PASSWORD") + envvars.git.clonepath = os.Getenv("MYAC_GIT_CLONEPATH") + + return envvars +} + +func (c *config) yamlconfig() config { + configpath := flag.String("config", "config.yml", "path to service config") + flag.Parse() + + yamlFile, err := ioutil.ReadFile(*configpath) + if err != nil { + log.Printf("yamlFile.Get err #%v ", err) + } + + err = yaml.Unmarshal(yamlFile, &c) + if err != nil { + log.Fatalf("Unmarshal: %v", err) + } + + return *c +} diff --git a/config.yml b/config.yml index 3b61057..b67a563 100644 --- a/config.yml +++ b/config.yml @@ -2,6 +2,4 @@ server: port: 8888 git: url: https://github.com/ignatev/myac-config.git - username: trolley - password: strongpassword localrepositorypath: .filesystem-repo diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..78095c2 --- /dev/null +++ b/config_test.go @@ -0,0 +1,23 @@ +package main + +import ( + "os" + "testing" +) + +func TestConf(t *testing.T) { + + t.Run("serverConf.port should be filled from environment var MYAC_PORT", func(t *testing.T){ + os.Setenv("MYAC_PORT", "8877") + c := serverConf{} +// configure() + got := c.Server.Port + want := os.Getenv("MYAC_PORT") + if got != want { + t.Errorf("\ngot:\n%s \nwant:\n%s", got, want) + } + }) +} + + +//use cmp.Equals and cmp.Diff for tests \ No newline at end of file diff --git a/myac.go b/myac.go index cedf3c7..769a603 100644 --- a/myac.go +++ b/myac.go @@ -7,7 +7,6 @@ import ( "log" "net/http" "os" - "path/filepath" "strconv" "strings" @@ -28,15 +27,16 @@ type serverConf struct { } type configHandler struct { - configs map[string][]string + configs *map[string]string } func (ch *configHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { p := strings.TrimLeft(r.URL.Path, "/") log.Println(p) + log.Println(r.RemoteAddr) c := ch.configs - if val, ok := c[p]; ok { - serveConfigFile(w, r, val[0]) + if val, ok := (*c)[p]; ok { + serveConfigFile(w, r, val) } } @@ -44,7 +44,7 @@ func serveConfigFile(w http.ResponseWriter, r *http.Request, p string) { http.ServeFile(w, r, p) } -func runServer(port string, configs map[string][]string) { +func runServer(port string, configs *map[string]string) { err := http.ListenAndServe(port, &configHandler{configs}) log.Println("Listening...") if err != nil { @@ -52,20 +52,6 @@ func runServer(port string, configs map[string][]string) { } } -func (c *serverConf) getConf(configPath string) *serverConf { - yamlFile, err := ioutil.ReadFile(configPath) - if err != nil { - log.Printf("yamlFile.Get err #%v ", err) - } - - err = yaml.Unmarshal(yamlFile, &c) - if err != nil { - log.Fatalf("Unmarshal: %v", err) - } - - return c -} - func cloneConfigRepo(repopath, url string) { _, err := git.PlainClone(repopath, false, &git.CloneOptions{ URL: url, @@ -84,36 +70,11 @@ func main() { url := config.Server.Git.URL repopath := config.Server.Git.LocalRepositoryPath port := ":" + strconv.Itoa(config.Server.Port) + cloneConfigRepo(repopath, url) - repo, err := listRepo(repopath) - if err != nil { - log.Println(err) - } printServerStatus(port) - - treebuilder(repopath) - runServer(port, createSliceWithPaths(repo)) -} - -func listRepo(root string) ([]string, error) { - var files []string - err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { - if !info.IsDir() && !strings.HasPrefix(path, root+"/.git") && path != root { - files = append(files, path) - } - return nil - }) - return files, err -} - -func createSliceWithPaths(paths []string) map[string][]string { - m := make(map[string][]string) - for _, p := range paths { - segs := strings.Split(p, "/") - k := segs[len(segs)-2] - m[k] = append(m[k], p) - } - return m + tree := treebuilder(repopath) + runServer(port, tree.finalmapping) } func printServerStatus(port string) { @@ -123,17 +84,40 @@ func printServerStatus(port string) { /`) fmt.Println("Configuration server") fmt.Println("") - fmt.Println("Service running on port", port) + fmt.Println("Service running on port", port[1:]) } -func configurationpath(path string) []string { - segments := strings.Split(path, "/") - return []string{segments[len(segments)-1]} +func (c *serverConf) getConf(configPath string) *serverConf { + yamlFile, err := ioutil.ReadFile(configPath) + if err != nil { + log.Printf("yamlFile.Get err #%v ", err) + } + + err = yaml.Unmarshal(yamlFile, &c) + if err != nil { + log.Fatalf("Unmarshal: %v", err) + } + + return c } -func collectEnvConfigs() { +func environmentConfig(configpath string) error { +/* //filename is the path to the json config file +file, err := os.Open(filename) if err != nil { return err } +decoder := json.NewDecoder(file) +err = decoder.Decode(&configuration) +if err != nil { return err } +and from the environment variables +configuration.Connection_String = os.Getenv("Connection_String") */ +/* configPath := flag.String("config", "config.yml", "path to service config") + flag.Parse() + config := c.getConf(*configPath) { + + } */ + return nil } + //todo add method for mapping service:[multiple config files, e.g. dev, prod, test] //todo organize logs //todo add Dockerfile diff --git a/tree.go b/tree.go index 63ab4a1..4e5f48f 100644 --- a/tree.go +++ b/tree.go @@ -6,6 +6,7 @@ import ( "log" "os" "path/filepath" + "strings" ) const ( @@ -17,10 +18,13 @@ const ( ) type tree struct { - url, path, name string - children []*tree - parent *tree - mapping *map[string][]string + url, name string + relpath, abspath string + children []*tree + parent *tree + mapping *map[string][]string + isfile bool + finalmapping *map[string]string } func buildtree(path string, parent *tree) *tree { @@ -28,15 +32,17 @@ func buildtree(path string, parent *tree) *tree { var children []*tree current.name = path current.parent = parent - current.path = parent.path + "/" + current.name + current.relpath = parent.relpath + "/" + current.name + current.abspath = parent.abspath + "/" + current.name current.mapping = parent.mapping + current.finalmapping = parent.finalmapping - fileinfo, err := os.Stat(current.path) + fileinfo, err := os.Stat(current.abspath) if err != nil { log.Println(err) } if fileinfo.IsDir() && fileinfo.Name() != ".git" { - dir, err := ioutil.ReadDir(current.path) + dir, err := ioutil.ReadDir(current.abspath) if err != nil { log.Println(err) } @@ -48,34 +54,51 @@ func buildtree(path string, parent *tree) *tree { } if !fileinfo.IsDir() { current.url = parent.name + current.isfile = true c := current.mapping v := (*c)[parent.name] - v = append(v, current.path) + v = append(v, current.abspath) (*c)[parent.name] = v - } + return ¤t } -func treebuilder(path string) { +func treebuilder(path string) tree { var rootDir tree m := make(map[string][]string) - rootDir.path = filepath.Dir(path) + f := make(map[string]string) + rootDir.relpath = "" + rootDir.abspath = filepath.Dir(path) rootDir.mapping = &m - tree := buildtree(filepath.Base(path), &rootDir) + rootDir.finalmapping = &f - for _, d := range rendertree(tree) { + result := buildtree(filepath.Base(path), &rootDir) + for _, d := range rendertree(result) { fmt.Println(d) } + + return rootDir } func rendertree(tree *tree) []string { var result []string var mapping string - if tree.url != "" { + if tree.isfile { + m := (*tree.mapping)[tree.parent.name] + prefix := tree.parent.relpath + if len(m) == 1 { + tree.url = prefix + } else { + tree.url = prefix + "/" + strings.TrimSuffix(tree.name, filepath.Ext(tree.name)) + } + tree.url = strings.TrimLeft(tree.url, "/") mapping = " >>> " + "http://localhost:8888/" + tree.url + f := tree.finalmapping + (*f)[tree.url] = tree.abspath } + result = append(result, tree.name+mapping) for i, child := range tree.children {