diff --git a/main.go b/main.go index 7bb47be..91e72a0 100644 --- a/main.go +++ b/main.go @@ -5,5 +5,5 @@ import ( ) func main() { - server.Run() + server.Run(":8080", "http://test.bestbytes/foomo/modules/Foomo.Page.Content/services/content.php") } diff --git a/server/node/node.go b/server/node/node.go deleted file mode 100644 index af99b33..0000000 --- a/server/node/node.go +++ /dev/null @@ -1,41 +0,0 @@ -package node - -import ( - "fmt" - "strings" -) - -type Node struct { - Id string `json:"id"` - Path string `json:"path"` - Names map[string]string `json:"names"` - Nodes map[string]*Node `json:"nodes"` - Index []string `json:"index"` -} - -const ( - INDENT string = "\t" -) - -func (node *Node) AddNode(name string, childNode *Node) (me *Node) { - node.Nodes[name] = childNode - return node -} - -func (node *Node) PrintNode(id string, level int) { - prefix := strings.Repeat(INDENT, level) - fmt.Printf("%s %s:\n", prefix, id) - for lang, name := range node.Names { - fmt.Printf("%s %s: %s\n", prefix+INDENT, lang, name) - } - for key, childNode := range node.Nodes { - childNode.PrintNode(key, level+1) - } -} - -func NewNode(id string, names map[string]string) *Node { - node := new(Node) - node.Id = id - node.Names = names - return node -} diff --git a/server/repo/content/constants.go b/server/repo/content/constants.go new file mode 100644 index 0000000..c63f6b7 --- /dev/null +++ b/server/repo/content/constants.go @@ -0,0 +1,5 @@ +package content + +const ( + INDENT string = "\t" +) diff --git a/server/repo/content/item.go b/server/repo/content/item.go new file mode 100644 index 0000000..0bcc6e6 --- /dev/null +++ b/server/repo/content/item.go @@ -0,0 +1,13 @@ +package content + +import () + +type Item struct { + Id string `json:"id"` + Name string `json:"name"` + URI string `json:"URI"` +} + +func NewItem() *Item { + return new(Item) +} diff --git a/server/repo/content/node.go b/server/repo/content/node.go new file mode 100644 index 0000000..b7d5862 --- /dev/null +++ b/server/repo/content/node.go @@ -0,0 +1,13 @@ +package content + +import () + +type Node struct { + Item *Item `json:"item"` + Nodes map[string]*Node `json:"nodes"` +} + +func NewNode() *Node { + node := new(Node) + return node +} diff --git a/server/repo/content/repoNode.go b/server/repo/content/repoNode.go new file mode 100644 index 0000000..422046f --- /dev/null +++ b/server/repo/content/repoNode.go @@ -0,0 +1,48 @@ +package content + +import ( + "fmt" + "strings" +) + +type RepoNode struct { + Id string `json:"id"` + MimeType string `json:"mimeType"` + Handler string `json:"handler"` + //Path string `json:"path"` + Regions map[string]string `json:"regions"` + URIs map[string]map[string]string `json:"uris"` + Names map[string]string `json:"names"` + Hidden bool `json:"hidden"` // hidden in tree + Groups []string `json:"groups"` + Data map[string]interface{} `json:"data"` + Index []string `json:"index"` + Nodes map[string]*RepoNode `json:"nodes"` + LinkIds []string `json:"linkIds"` // ids to link to + // published from - to +} + +func (node *RepoNode) AddNode(name string, childNode *RepoNode) *RepoNode { + node.Nodes[name] = childNode + return node +} + +func (node *RepoNode) GetName(language string) string { + return node.Names[language] +} + +func (node *RepoNode) PrintNode(id string, level int) { + prefix := strings.Repeat(INDENT, level) + fmt.Printf("%s %s:\n", prefix, id) + for lang, name := range node.Names { + fmt.Printf("%s %s: %s\n", prefix+INDENT, lang, name) + } + for key, childNode := range node.Nodes { + childNode.PrintNode(key, level+1) + } +} + +func NewRepoNode() *RepoNode { + node := new(RepoNode) + return node +} diff --git a/server/repo/content/siteContent-example.json b/server/repo/content/siteContent-example.json new file mode 100644 index 0000000..f3b1342 --- /dev/null +++ b/server/repo/content/siteContent-example.json @@ -0,0 +1,44 @@ +{ + "status": 200, + "nodes" : { + "main" : { + "id" : "xxx", + "name" : "home", + "nodes" : { + + } + }, + "footer" : { + + }, + "meta" : { + + } + }, + "content" : { + "item" : { + "id" : "xxx-content-id", + "name" : "foo", + "mimeType" : "" + }, + "handler" : "creation/full", + "data": { + "foo" : "bar" + }, + "uris" : { + "id-yyy" : "/de/...", + "" + } + }, + "linksToOtherRegionsAndLanguages": { + "ch" : { + "de" : "/ch/de/foo", + "it" : "/ch/it/va-fan-foo", + // no fr no have + }, + "si" : { + "si" : "/si/si/..." + } + // no other regions for this one + } +} \ No newline at end of file diff --git a/server/repo/content/siteContent.go b/server/repo/content/siteContent.go new file mode 100644 index 0000000..df9d683 --- /dev/null +++ b/server/repo/content/siteContent.go @@ -0,0 +1,24 @@ +package content + +const ( + STATUS_OK = 200 + STATUS_FORBIDDEN = 403 + STATUS_NOT_FOUND = 404 +) + +type SiteContent struct { + Status int `json:"status"` + NavigationTrees map[string]*Node `json:"navigationTrees"` + Content struct { + Item *Item `json:"item"` + Data interface{} `json:"data"` + } `json:"content"` + URIs map[string]string `json:"URIs"` +} + +func NewSiteContent() *SiteContent { + c := new(SiteContent) + c.NavigationTrees = make(map[string]*Node) + c.URIs = make(map[string]string) + return c +} diff --git a/server/repo/repo.go b/server/repo/repo.go index b7c3a55..7f4154b 100644 --- a/server/repo/repo.go +++ b/server/repo/repo.go @@ -1,25 +1,18 @@ package repo import ( - "encoding/json" "fmt" - "github.com/foomo/ContentServer/server/node" - "io/ioutil" - "net/http" + "github.com/foomo/ContentServer/server/repo/content" + "github.com/foomo/ContentServer/server/requests" + "github.com/foomo/ContentServer/server/utils" ) -type SiteContent struct { - Path string `json: path` -} - -func NewSiteContent() *SiteContent { - content := new(SiteContent) - return content -} - type Repo struct { server string - Directory map[string]node.Node + Regions []string + Languages []string + Directory map[string]content.RepoNode + Node *content.RepoNode } func NewRepo(server string) *Repo { @@ -28,41 +21,30 @@ func NewRepo(server string) *Repo { return repo } -func (repo *Repo) GetContent(path string) *SiteContent { - content := NewSiteContent() - content.Path = path - return content +func (repo *Repo) GetContent(r *requests.Content) *content.SiteContent { + c := content.NewSiteContent() + if node, ok := repo.Directory[r.URI]; ok { + c.Status = content.STATUS_OK + fmt.Println(node.Names, r.Env.Language) + c.Content.Item = content.NewItem() + c.Content.Item.Id = node.Id + c.Content.Item.Name = node.GetName(r.Env.Language) + } else { + c.Status = content.STATUS_NOT_FOUND + } + return c } -func (repo *Repo) builDirectory(dirNode *node.Node) { - repo.Directory[dirNode.Path] = *dirNode +func (repo *Repo) builDirectory(dirNode *content.RepoNode) { + repo.Directory[dirNode.Id] = *dirNode for _, childNode := range dirNode.Nodes { repo.builDirectory(childNode) } } -func (repo *Repo) Update() interface{} { - response, err := http.Get(repo.server) - if err != nil { - fmt.Printf("%s", err) - return "aua" - } else { - defer response.Body.Close() - contents, err := ioutil.ReadAll(response.Body) - if err != nil { - fmt.Printf("%s", err) - } - fmt.Printf("json string %s", string(contents)) - jsonNode := node.NewNode("/foo", map[string]string{"en": "foo"}) - jsonErr := json.Unmarshal(contents, &jsonNode) - if jsonErr != nil { - fmt.Println("wtf") - } - //fmt.Printf("obj %v", jsonNode) - jsonNode.PrintNode("root", 0) - - repo.Directory = make(map[string]node.Node) - repo.builDirectory(jsonNode) - return jsonNode - } +func (repo *Repo) Update() { + repo.Node = content.NewRepoNode() + utils.Get(repo.server, repo.Node) + repo.Directory = make(map[string]content.RepoNode) + repo.builDirectory(repo.Node) } diff --git a/server/requests/content.go b/server/requests/content.go new file mode 100644 index 0000000..04c9b9e --- /dev/null +++ b/server/requests/content.go @@ -0,0 +1,20 @@ +package requests + +type Content struct { + Env struct { + Region string `json:"region"` + Language string `json:"language"` + Groups []string `json:"groups"` + Data interface{} `json:"data"` + } `json:"env"` + URI string + Nodes map[string]struct { + Id string `json:"id"` + MimeTypes []string `json:"mimeTypes"` + Expand bool `json:"expand"` + } `json:"nodes"` +} + +func NewContent() *Content { + return new(Content) +} diff --git a/server/requests/content.json b/server/requests/content.json new file mode 100644 index 0000000..7a1ccd1 --- /dev/null +++ b/server/requests/content.json @@ -0,0 +1,26 @@ +{ + "env" : { + "region" : "de", + "language" : "de", + "groups" : ["admin", "www"], + "data" : { + "crazy": "stuff", + "data" : { + "crazy": "stuff" + } + } + }, + "URI" : "/home", + "nodes" : { + "main" : { + "id" : "id-xxx", + "mimeTypes" : ["text/html"], + "expand" : false + }, + "meta" : { + "id" : "id-yyy", + "mimeTypes" : ["application/x-radactFolder"], + "expand" : true + } + } +} \ No newline at end of file diff --git a/server/server.go b/server/server.go index 22705ec..b7cf5af 100644 --- a/server/server.go +++ b/server/server.go @@ -1,11 +1,10 @@ package server import ( - "encoding/json" "fmt" - //"github.com/foomo/ContentServer/server/node" "github.com/foomo/ContentServer/server/repo" - //"log" + "github.com/foomo/ContentServer/server/requests" + "github.com/foomo/ContentServer/server/utils" "net/http" "strings" ) @@ -13,60 +12,18 @@ import ( var i int = 0 var contentRepo *repo.Repo -func toJson(obj interface{}) (s string) { - b, err := json.MarshalIndent(obj, "", " ") - if err != nil { - s = "" - return - } - s = string(b) - return -} - -func jsonResponse(w http.ResponseWriter, obj interface{}) { - fmt.Fprint(w, toJson(obj)) -} - func contentHandler(w http.ResponseWriter, r *http.Request) { - /* - i++ - log.Println("request #", i, r) - childNode := node.NewNode("/foo", map[string]string{"en": "foo"}) - parentNode := node.NewNode("/", map[string]string{"en": "root"}).AddNode("foo", childNode) - jsonResponse(w, parentNode) - */ - uriParts := strings.Split(r.RequestURI, "/") - jsonResponse(w, contentRepo.GetContent(uriParts[2])) + request := requests.NewContent() + utils.PopulateRequest(r, request) + utils.JsonResponse(w, contentRepo.GetContent(request)) } func wtfHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, "tank you for your request, but i am totally lost with it\n", r.RequestURI, "\n") + fmt.Fprint(w, "thank you for your request, but i am totally lost with it\n", r.RequestURI, "\n") } func update() interface{} { contentRepo.Update() - /* - response, err := http.Get("http://test.bestbytes/foomo/modules/Foomo.Page.Content/services/content.php") - if err != nil { - fmt.Printf("%s", err) - return "aua" - } else { - defer response.Body.Close() - contents, err := ioutil.ReadAll(response.Body) - if err != nil { - fmt.Printf("%s", err) - } - fmt.Printf("json string %s", string(contents)) - jsonNode := node.NewNode("/foo", map[string]string{"en": "foo"}) - jsonErr := json.Unmarshal(contents, &jsonNode) - if jsonErr != nil { - fmt.Println("wtf") - } - //fmt.Printf("obj %v", jsonNode) - jsonNode.PrintNode("root", 0) - return jsonNode - } - */ return contentRepo.Directory } @@ -76,23 +33,28 @@ func commandHandler(w http.ResponseWriter, r *http.Request) { case true == (len(parts) > 1): switch parts[2] { case "update": - jsonResponse(w, update()) + utils.JsonResponse(w, update()) return default: help := make(map[string]interface{}) help["input"] = parts[2] - help["commands"] = []string{"update", "help"} - jsonResponse(w, help) + help["commands"] = []string{"update", "help", "content"} + utils.JsonResponse(w, help) } default: wtfHandler(w, r) } } -func Run() { - contentRepo = repo.NewRepo("http://test.bestbytes/foomo/modules/Foomo.Page.Content/services/content.php") - http.HandleFunc("/content/", contentHandler) +func Run(addr string, serverUrl string) { + fmt.Println("staring content server") + fmt.Printf(" loading content from %s\n", serverUrl) + contentRepo = repo.NewRepo(serverUrl) + contentRepo.Update() + fmt.Printf(" loaded %d items\n", len(contentRepo.Directory)) + http.HandleFunc("/content", contentHandler) http.HandleFunc("/cmd/", commandHandler) http.HandleFunc("/", wtfHandler) - http.ListenAndServe(":8080", nil) + fmt.Printf(" starting service on %s\n", addr) + http.ListenAndServe(addr, nil) } diff --git a/server/utils/utils.go b/server/utils/utils.go new file mode 100644 index 0000000..eb41a88 --- /dev/null +++ b/server/utils/utils.go @@ -0,0 +1,57 @@ +package utils + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" +) + +func JsonResponse(w http.ResponseWriter, obj interface{}) { + fmt.Fprint(w, toJson(obj)) +} + +func toJson(obj interface{}) string { + b, err := json.MarshalIndent(obj, "", "\t") + if err != nil { + return "" + } else { + return string(b) + } +} + +func extractJsonFromRequest(r *http.Request) []byte { + file, _, err := r.FormFile("request") + if err != nil { + fmt.Println(err) + } + data, err := ioutil.ReadAll(file) + if err != nil { + fmt.Println(err) + } + return data +} + +func PopulateRequest(r *http.Request, obj interface{}) { + json.Unmarshal(extractJsonFromRequest(r), obj) +} + +func Get(URL string, obj interface{}) { + // add proper error handling + response, err := http.Get(URL) + if err != nil { + fmt.Printf("%s", err) + } else { + defer response.Body.Close() + contents, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Printf("%s", err) + } + // fmt.Printf("json string %s", string(contents)) + jsonErr := json.Unmarshal(contents, &obj) + if jsonErr != nil { + fmt.Println("wtf", jsonErr) + } + } + +}