mirror of
https://github.com/foomo/contentserver.git
synced 2025-10-16 12:25:44 +00:00
GOing crazy in a good way
This commit is contained in:
parent
6c93a9b710
commit
64e9c72bca
2
main.go
2
main.go
@ -5,5 +5,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
server.Run()
|
server.Run(":8080", "http://test.bestbytes/foomo/modules/Foomo.Page.Content/services/content.php")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
|
||||||
}
|
|
||||||
5
server/repo/content/constants.go
Normal file
5
server/repo/content/constants.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package content
|
||||||
|
|
||||||
|
const (
|
||||||
|
INDENT string = "\t"
|
||||||
|
)
|
||||||
13
server/repo/content/item.go
Normal file
13
server/repo/content/item.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
13
server/repo/content/node.go
Normal file
13
server/repo/content/node.go
Normal file
@ -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
|
||||||
|
}
|
||||||
48
server/repo/content/repoNode.go
Normal file
48
server/repo/content/repoNode.go
Normal file
@ -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
|
||||||
|
}
|
||||||
44
server/repo/content/siteContent-example.json
Normal file
44
server/repo/content/siteContent-example.json
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
24
server/repo/content/siteContent.go
Normal file
24
server/repo/content/siteContent.go
Normal file
@ -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
|
||||||
|
}
|
||||||
@ -1,25 +1,18 @@
|
|||||||
package repo
|
package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/foomo/ContentServer/server/node"
|
"github.com/foomo/ContentServer/server/repo/content"
|
||||||
"io/ioutil"
|
"github.com/foomo/ContentServer/server/requests"
|
||||||
"net/http"
|
"github.com/foomo/ContentServer/server/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SiteContent struct {
|
|
||||||
Path string `json: path`
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSiteContent() *SiteContent {
|
|
||||||
content := new(SiteContent)
|
|
||||||
return content
|
|
||||||
}
|
|
||||||
|
|
||||||
type Repo struct {
|
type Repo struct {
|
||||||
server string
|
server string
|
||||||
Directory map[string]node.Node
|
Regions []string
|
||||||
|
Languages []string
|
||||||
|
Directory map[string]content.RepoNode
|
||||||
|
Node *content.RepoNode
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRepo(server string) *Repo {
|
func NewRepo(server string) *Repo {
|
||||||
@ -28,41 +21,30 @@ func NewRepo(server string) *Repo {
|
|||||||
return repo
|
return repo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *Repo) GetContent(path string) *SiteContent {
|
func (repo *Repo) GetContent(r *requests.Content) *content.SiteContent {
|
||||||
content := NewSiteContent()
|
c := content.NewSiteContent()
|
||||||
content.Path = path
|
if node, ok := repo.Directory[r.URI]; ok {
|
||||||
return content
|
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) {
|
func (repo *Repo) builDirectory(dirNode *content.RepoNode) {
|
||||||
repo.Directory[dirNode.Path] = *dirNode
|
repo.Directory[dirNode.Id] = *dirNode
|
||||||
for _, childNode := range dirNode.Nodes {
|
for _, childNode := range dirNode.Nodes {
|
||||||
repo.builDirectory(childNode)
|
repo.builDirectory(childNode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *Repo) Update() interface{} {
|
func (repo *Repo) Update() {
|
||||||
response, err := http.Get(repo.server)
|
repo.Node = content.NewRepoNode()
|
||||||
if err != nil {
|
utils.Get(repo.server, repo.Node)
|
||||||
fmt.Printf("%s", err)
|
repo.Directory = make(map[string]content.RepoNode)
|
||||||
return "aua"
|
repo.builDirectory(repo.Node)
|
||||||
} 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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
20
server/requests/content.go
Normal file
20
server/requests/content.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
26
server/requests/content.json
Normal file
26
server/requests/content.json
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,11 +1,10 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
//"github.com/foomo/ContentServer/server/node"
|
|
||||||
"github.com/foomo/ContentServer/server/repo"
|
"github.com/foomo/ContentServer/server/repo"
|
||||||
//"log"
|
"github.com/foomo/ContentServer/server/requests"
|
||||||
|
"github.com/foomo/ContentServer/server/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -13,60 +12,18 @@ import (
|
|||||||
var i int = 0
|
var i int = 0
|
||||||
var contentRepo *repo.Repo
|
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) {
|
func contentHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
/*
|
request := requests.NewContent()
|
||||||
i++
|
utils.PopulateRequest(r, request)
|
||||||
log.Println("request #", i, r)
|
utils.JsonResponse(w, contentRepo.GetContent(request))
|
||||||
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) {
|
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{} {
|
func update() interface{} {
|
||||||
contentRepo.Update()
|
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
|
return contentRepo.Directory
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,23 +33,28 @@ func commandHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
case true == (len(parts) > 1):
|
case true == (len(parts) > 1):
|
||||||
switch parts[2] {
|
switch parts[2] {
|
||||||
case "update":
|
case "update":
|
||||||
jsonResponse(w, update())
|
utils.JsonResponse(w, update())
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
help := make(map[string]interface{})
|
help := make(map[string]interface{})
|
||||||
help["input"] = parts[2]
|
help["input"] = parts[2]
|
||||||
help["commands"] = []string{"update", "help"}
|
help["commands"] = []string{"update", "help", "content"}
|
||||||
jsonResponse(w, help)
|
utils.JsonResponse(w, help)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
wtfHandler(w, r)
|
wtfHandler(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run() {
|
func Run(addr string, serverUrl string) {
|
||||||
contentRepo = repo.NewRepo("http://test.bestbytes/foomo/modules/Foomo.Page.Content/services/content.php")
|
fmt.Println("staring content server")
|
||||||
http.HandleFunc("/content/", contentHandler)
|
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("/cmd/", commandHandler)
|
||||||
http.HandleFunc("/", wtfHandler)
|
http.HandleFunc("/", wtfHandler)
|
||||||
http.ListenAndServe(":8080", nil)
|
fmt.Printf(" starting service on %s\n", addr)
|
||||||
|
http.ListenAndServe(addr, nil)
|
||||||
}
|
}
|
||||||
|
|||||||
57
server/utils/utils.go
Normal file
57
server/utils/utils.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user