Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 0 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ server:
port: 8888
git:
url: https://github.com/ignatev/myac-config.git
username: trolley
password: strongpassword
localrepositorypath: .filesystem-repo
23 changes: 23 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -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
88 changes: 36 additions & 52 deletions myac.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"

Expand All @@ -28,44 +27,31 @@ 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)
}
}

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 {
log.Println(err)
}
}

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,
Expand All @@ -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) {
Expand All @@ -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
Expand Down
51 changes: 37 additions & 14 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"strings"
)

const (
Expand All @@ -17,26 +18,31 @@ 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 {
var current 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)
}
Expand All @@ -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 &current
}

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 {
Expand Down