mirror of
https://github.com/foomo/contentserver.git
synced 2025-10-16 12:25:44 +00:00
Hello go\!
This commit is contained in:
commit
6c93a9b710
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
.*
|
||||||
|
!.git*
|
||||||
|
/main
|
||||||
|
/ContentServer.sublime-project
|
||||||
|
/ContentServer.sublime-workspace
|
||||||
|
|
||||||
6
doc.go
Normal file
6
doc.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// ContentServer project doc.go
|
||||||
|
|
||||||
|
/*
|
||||||
|
ContentServer document
|
||||||
|
*/
|
||||||
|
package main
|
||||||
9
main.go
Normal file
9
main.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/foomo/ContentServer/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
server.Run()
|
||||||
|
}
|
||||||
41
server/node/node.go
Normal file
41
server/node/node.go
Normal file
@ -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
|
||||||
|
}
|
||||||
68
server/repo/repo.go
Normal file
68
server/repo/repo.go
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
98
server/server.go
Normal file
98
server/server.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user