Пример #1
0
func setup(c *module.ModuleContext) error {
	G := func(in string) string { return in }
	m := c.Session.Monsti()

	// Register a new node type
	nodeType := service.NodeType{
		Id:        "example.ExampleType",
		AddableTo: []string{"."},
		Name:      util.GenLanguageMap(G("Example node type"), availableLocales),
		Fields: []*service.NodeField{
			// core.Title and core.Body are already known to the system,
			// just specify their IDs to include them.
			{Id: "core.Title"},
			{Id: "core.Body"},
			{
				Id:   "example.Foo",
				Name: util.GenLanguageMap(G("Foo"), availableLocales),
				Type: "Text",
			},
			{
				Id:   "example.Bar",
				Name: util.GenLanguageMap(G("Bar"), availableLocales),
				Type: "DateTime",
			},
		},
	}
	if err := m.RegisterNodeType(&nodeType); err != nil {
		c.Logger.Fatalf("Could not register %q node type: %v", nodeType.Id, err)
	}

	// Add a signal handler
	handler := service.NewNodeContextHandler(
		func(id uint, nodeType string, embedNode *service.EmbedNode) map[string]string {
			session, err := c.Sessions.New()
			if err != nil {
				c.Logger.Fatalf("Could not get session: %v", err)
				return nil
			}
			defer c.Sessions.Free(session)
			if nodeType == "example.ExampleType" {
				req, err := session.Monsti().GetRequest(id)
				if err != nil || req == nil {
					c.Logger.Fatalf("Could not get request: %v", err)
				}
				return map[string]string{
					"SignalFoo": fmt.Sprintf("Hello Signal! Site name: %v", req.Site),
				}
			}
			return nil
		})
	if err := m.AddSignalHandler(handler); err != nil {
		c.Logger.Fatalf("Could not add signal handler: %v", err)
	}

	return nil
}
Пример #2
0
func initBlog(settings *settings, session *service.Session, logger *log.Logger,
	renderer *mtemplate.Renderer) error {
	G := func(in string) string { return in }

	nodeType := service.NodeType{
		Id:        "core.Blog",
		AddableTo: []string{"."},
		Name:      util.GenLanguageMap(G("Blog"), availableLocales),
		Fields: []*service.NodeField{
			{Id: "core.Title"},
		},
	}
	if err := session.Monsti().RegisterNodeType(&nodeType); err != nil {
		return fmt.Errorf("Could not register blog node type: %v", err)
	}

	nodeType = service.NodeType{
		Id:        "core.BlogPost",
		AddableTo: []string{"core.Blog"},
		Name:      util.GenLanguageMap(G("Blog Post"), availableLocales),
		Fields: []*service.NodeField{
			{Id: "core.Title"},
			{Id: "core.Body"},
		},
		Hide:       true,
		PathPrefix: "$year/$month",
	}
	if err := session.Monsti().RegisterNodeType(&nodeType); err != nil {
		return fmt.Errorf("Could not register blog post node type: %v", err)
	}

	// Add a signal handler
	handler := service.NewNodeContextHandler(
		func(req uint, nodeType string,
			embedNode *service.EmbedNode) map[string]string {
			switch nodeType {
			case "core.Blog":
				ctx, err := getBlogContext(req, embedNode, session, settings, renderer)
				if err != nil {
					logger.Printf("Could not get blog context: %v", err)
				}
				return ctx
			default:
				return nil
			}
		})
	if err := session.Monsti().AddSignalHandler(handler); err != nil {
		logger.Fatalf("Could not add signal handler: %v", err)
	}
	return nil
}
Пример #3
0
func initNodeTypes(settings *settings, session *service.Session, logger *log.Logger) error {
	G := func(in string) string { return in }
	pathType := service.NodeType{
		Id:   "core.Path",
		Hide: true,
		Name: util.GenLanguageMap(G("Path"), availableLocales),
	}
	if err := session.Monsti().RegisterNodeType(&pathType); err != nil {
		return fmt.Errorf("Could not register path node type: %v", err)
	}

	documentType := service.NodeType{
		Id:        "core.Document",
		AddableTo: []string{"."},
		Name:      util.GenLanguageMap(G("Document"), availableLocales),
		Fields: []*service.NodeField{
			{
				Id:       "core.Title",
				Required: true,
				Name:     util.GenLanguageMap(G("Title"), availableLocales),
				Type:     "Text",
			},
			{
				Id:       "core.Body",
				Required: true,
				Name:     util.GenLanguageMap(G("Body"), availableLocales),
				Type:     "HTMLArea",
			},
		},
	}
	if err := session.Monsti().RegisterNodeType(&documentType); err != nil {
		return fmt.Errorf("Could not register document node type: %v", err)
	}

	fileType := service.NodeType{
		Id:        "core.File",
		AddableTo: []string{"."},
		Name:      util.GenLanguageMap(G("File"), availableLocales),
		Fields: []*service.NodeField{
			{Id: "core.Title"},
			{Id: "core.Body"},
			{
				Id:       "core.File",
				Required: true,
				Name:     util.GenLanguageMap(G("File"), availableLocales),
				Type:     "File",
			},
		},
	}
	if err := session.Monsti().RegisterNodeType(&fileType); err != nil {
		return fmt.Errorf("Could not register file node type: %v", err)
	}

	imageType := service.NodeType{
		Id:        "core.Image",
		AddableTo: []string{"."},
		Name:      util.GenLanguageMap(G("Image"), availableLocales),
		Fields: []*service.NodeField{
			{Id: "core.Title"},
			{Id: "core.File"},
		},
	}
	if err := session.Monsti().RegisterNodeType(&imageType); err != nil {
		return fmt.Errorf("Could not register image node type: %v", err)
	}

	contactFormType := service.NodeType{
		Id:        "core.ContactForm",
		AddableTo: []string{"."},
		Name:      util.GenLanguageMap(G("Contact form"), availableLocales),
		Fields: []*service.NodeField{
			{Id: "core.Title"},
			{Id: "core.Body"},
		},
	}
	if err := session.Monsti().RegisterNodeType(&contactFormType); err != nil {
		return fmt.Errorf("Could not register contactform node type: %v", err)
	}
	return nil
}