From 6c93a9b710eef6cdd6263d65eb415ab38f1d6eab Mon Sep 17 00:00:00 2001 From: Jan Halfar Date: Thu, 22 Aug 2013 09:44:26 +0200 Subject: [PATCH] Hello go\! --- .gitignore | 6 +++ doc.go | 6 +++ main.go | 9 +++++ server/node/node.go | 41 +++++++++++++++++++ server/repo/repo.go | 68 +++++++++++++++++++++++++++++++ server/server.go | 98 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 228 insertions(+) create mode 100644 .gitignore create mode 100644 doc.go create mode 100644 main.go create mode 100644 server/node/node.go create mode 100644 server/repo/repo.go create mode 100644 server/server.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f48d33a --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.* +!.git* +/main +/ContentServer.sublime-project +/ContentServer.sublime-workspace + diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..1e4c5c0 --- /dev/null +++ b/doc.go @@ -0,0 +1,6 @@ +// ContentServer project doc.go + +/* +ContentServer document +*/ +package main diff --git a/main.go b/main.go new file mode 100644 index 0000000..7bb47be --- /dev/null +++ b/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "github.com/foomo/ContentServer/server" +) + +func main() { + server.Run() +} diff --git a/server/node/node.go b/server/node/node.go new file mode 100644 index 0000000..af99b33 --- /dev/null +++ b/server/node/node.go @@ -0,0 +1,41 @@ +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/repo.go b/server/repo/repo.go new file mode 100644 index 0000000..b7c3a55 --- /dev/null +++ b/server/repo/repo.go @@ -0,0 +1,68 @@ +package repo + +import ( + "encoding/json" + "fmt" + "github.com/foomo/ContentServer/server/node" + "io/ioutil" + "net/http" +) + +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 +} + +func NewRepo(server string) *Repo { + repo := new(Repo) + repo.server = server + return repo +} + +func (repo *Repo) GetContent(path string) *SiteContent { + content := NewSiteContent() + content.Path = path + return content +} + +func (repo *Repo) builDirectory(dirNode *node.Node) { + repo.Directory[dirNode.Path] = *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 + } +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..22705ec --- /dev/null +++ b/server/server.go @@ -0,0 +1,98 @@ +package server + +import ( + "encoding/json" + "fmt" + //"github.com/foomo/ContentServer/server/node" + "github.com/foomo/ContentServer/server/repo" + //"log" + "net/http" + "strings" +) + +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])) +} + +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") +} + +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 +} + +func commandHandler(w http.ResponseWriter, r *http.Request) { + parts := strings.Split(r.RequestURI, "/") + switch true { + case true == (len(parts) > 1): + switch parts[2] { + case "update": + jsonResponse(w, update()) + return + default: + help := make(map[string]interface{}) + help["input"] = parts[2] + help["commands"] = []string{"update", "help"} + 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) + http.HandleFunc("/cmd/", commandHandler) + http.HandleFunc("/", wtfHandler) + http.ListenAndServe(":8080", nil) +}