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
文件: blog.go 项目: Ali-Kabiri/monsti
func initBlog(settings *settings, session *service.Session,
	sessions *service.SessionPool, logger *log.Logger,
	renderer *mtemplate.Renderer) error {
	G := func(in string) string { return in }

	nodeType := service.NodeType{
		Id:        "core.Blog",
		AddableTo: []string{"."},
		Name:      i18n.GenLanguageMap(G("Blog"), availableLocales),
		Fields: []*service.FieldConfig{
			{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:      i18n.GenLanguageMap(G("Blog Post"), availableLocales),
		Fields: []*service.FieldConfig{
			{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(sessions,
		func(req uint, session *service.Session, nodeType string,
			embedNode *service.EmbedNode) (
			map[string][]byte, *service.CacheMods, error) {
			switch nodeType {
			case "core.Blog":
				ctx, mods, err := getBlogContext(req, embedNode, session, settings, renderer)
				if err != nil {
					return nil, nil, fmt.Errorf("Could not get blog context: %v", err)
				}
				return ctx, mods, nil
			default:
				return nil, nil, nil
			}
		})
	if err := session.Monsti().AddSignalHandler(handler); err != nil {
		logger.Fatalf("Could not add signal handler: %v", err)
	}
	return nil
}
示例#3
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",
		Name: i18n.GenLanguageMap(G("Example node type"), availableLocales),
		Fields: []*service.FieldConfig{
			// 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: i18n.GenLanguageMap(G("Foo"), availableLocales),
				Type: new(service.TextFieldType),
			},
			{
				Id:   "example.Bar",
				Name: i18n.GenLanguageMap(G("Bar"), availableLocales),
				Type: new(service.DateTimeFieldType),
			},
		},
	}
	if err := m.RegisterNodeType(&nodeType); err != nil {
		c.Logger.Fatalf("Could not register %q node type: %v", nodeType.Id, err)
	}

	nodeType = service.NodeType{
		Id:   "example.Embed",
		Name: i18n.GenLanguageMap(G("Embed example"), availableLocales),
		Fields: []*service.FieldConfig{
			// core.Title and core.Body are already known to the system,
			// just specify their IDs to include them.
			{Id: "core.Title"},
			{Id: "core.Body"},
		},
	}
	if err := m.RegisterNodeType(&nodeType); err != nil {
		c.Logger.Fatalf("Could not register %q node type: %v", nodeType.Id, err)
	}

	nodeType = service.NodeType{
		Id:   "example.Fields",
		Name: i18n.GenLanguageMap(G("Fields example"), availableLocales),
		Fields: []*service.FieldConfig{
			// 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.Bool",
				Name: i18n.GenLanguageMap(G("Bool"), availableLocales),
				Type: new(service.BoolFieldType),
			},
			{
				Id:   "example.DateTime",
				Name: i18n.GenLanguageMap(G("DateTime"), availableLocales),
				Type: new(service.DateTimeFieldType),
			},
			{
				Id:   "example.HTMLArea",
				Name: i18n.GenLanguageMap(G("HTML"), availableLocales),
				Type: new(service.HTMLFieldType),
			},
			{
				Id:     "example.Hidden",
				Name:   i18n.GenLanguageMap(G("Hidden"), availableLocales),
				Hidden: true,
				Type:   new(service.TextFieldType),
			},
			{
				Id:   "example.Text",
				Name: i18n.GenLanguageMap(G("Text"), availableLocales),
				Type: new(service.TextFieldType),
			},
			{
				Id:   "example.TextList",
				Name: i18n.GenLanguageMap(G("Text list"), availableLocales),
				Type: &service.ListFieldType{new(service.TextFieldType)},
			},
			{
				Id:   "example.Map",
				Name: i18n.GenLanguageMap(G("Map"), availableLocales),
				Type: &service.MapFieldType{new(service.TextFieldType)},
			},
			{
				Id:   "example.Combined",
				Name: i18n.GenLanguageMap(G("Combined"), availableLocales),
				Type: &service.CombinedFieldType{
					map[string]service.FieldConfig{
						"Text": {
							Name: i18n.GenLanguageMap(G("Text"), availableLocales),
							Type: new(service.TextFieldType),
						},
						"Bool": {
							Name: i18n.GenLanguageMap(G("Bool"), availableLocales),
							Type: new(service.BoolFieldType),
						},
					}},
			},
		},
	}
	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(c.Sessions,
		func(id uint, session *service.Session, nodeType string,
			embedNode *service.EmbedNode) (
			map[string][]byte, *service.CacheMods, error) {
			if nodeType == "example.ExampleType" {
				req, err := session.Monsti().GetRequest(id)
				if err != nil || req == nil {
					return nil, nil, fmt.Errorf("Could not get request: %v", err)
				}
				return map[string][]byte{
					"SignalFoo": []byte(fmt.Sprintf("Hello Signal! Site name: %v", req.Site)),
				}, nil, nil
			}
			return nil, nil, nil
		})
	if err := m.AddSignalHandler(handler); err != nil {
		c.Logger.Fatalf("Could not add signal handler: %v", err)
	}

	return nil
}