Exemplo n.º 1
0
// Handle handles federated wiki requests
// /system/plugins
// /system/factories
func (server *Server) Handle(r *http.Request) (code int, template string, data interface{}) {
	switch r.URL.Path {
	case "/system/plugins":
		server.mu.RLock()
		defer server.mu.RUnlock()

		plugins := make([]string, 0, 10)
		for _, plugin := range server.plugins {
			plugins = append(plugins, plugin.Name)
		}

		sort.Strings(plugins)

		return http.StatusOK, "plugins", plugins
	case "/system/factories":
		server.mu.RLock()
		defer server.mu.RUnlock()

		factories := make([]*Factory, 0, 10)
		for _, plugin := range server.plugins {
			if plugin.Factory != nil {
				factories = append(factories, plugin.Factory)
			}
		}

		return http.StatusOK, "factories", factories
	}

	return fedwiki.ErrorResponse(http.StatusNotFound, "Page not found.")
}
Exemplo n.º 2
0
func serve(r *http.Request) (code int, template string, data interface{}) {
	if r.Method != "GET" {
		return fedwiki.ErrorResponse(http.StatusForbidden, "Method %s is not allowed", r.Method)
	}

	switch r.URL.Path {
	case "/status":
		return http.StatusOK, "", fedwiki.Page{
			PageHeader: fedwiki.PageHeader{
				Slug:  "/status",
				Title: "Status",
				Date:  fedwiki.Now(),
			},
			Story: fedwiki.Story{
				item.Paragraph("Current time is: " + time.Now().String()),
			},
		}
	default:
		return fedwiki.ErrorResponse(http.StatusNotFound, "Page %s not found", r.URL.Path)
	}
}
Exemplo n.º 3
0
func (sitemap *Handler) Handle(r *http.Request) (code int, template string, data interface{}) {
	switch r.URL.Path {
	case "/system/sitemap":
		sitemap.mu.RLock()
		defer sitemap.mu.RUnlock()
		return http.StatusOK, "sitemap", sitemap.headers
	case "/system/slugs":
		sitemap.mu.RLock()
		defer sitemap.mu.RUnlock()
		slugs := make([]fedwiki.Slug, 0, len(sitemap.headers))
		for _, h := range sitemap.headers {
			slugs = append(slugs, h.Slug)
		}

		return http.StatusOK, "slugs", slugs
	}

	return fedwiki.ErrorResponse(http.StatusNotFound, "Page not found.")
}
Exemplo n.º 4
0
func (pages Handler) Handle(r *http.Request) (code int, template string, data interface{}) {
	slug := fedwiki.Slug(r.URL.Path)
	if err := fedwiki.ValidateSlug(slug); err != nil {
		return fedwiki.ErrorResponse(http.StatusBadRequest, err.Error())
	}

	switch r.Method {
	case "GET":
		page, err := pages.Load(slug)
		if err != nil {
			if err == fedwiki.ErrNotExist {
				return fedwiki.ErrorResponse(http.StatusNotFound, `Page "%s" does not exist.`, slug)
			}
			return fedwiki.ErrorResponse(http.StatusInternalServerError, err.Error())
		}
		page.Slug = slug
		return http.StatusOK, "", page
	case "PUT":
		var page *fedwiki.Page
		var err error

		switch r.Header.Get("Content-Type") {
		case "":
			fallthrough
		case "application/json":
			page, err = readJSONPage(r.Body)
			r.Body.Close()
			if err != nil {
				return fedwiki.ErrorResponse(http.StatusBadRequest, err.Error())
			}
		default:
			return fedwiki.ErrorResponse(http.StatusBadRequest, `Invalid request Content-Type "%s".`, r.Header.Get("Content-Type"))
		}

		page.Slug = slug
		page.Date = fedwiki.NewDate(time.Now())

		err = pages.Create(slug, page)
		if err != nil {
			return fedwiki.ErrorResponse(http.StatusInternalServerError, err.Error())
		}
		return http.StatusOK, "", page
	case "PATCH":
		var action fedwiki.Action
		var err error

		switch r.Header.Get("Content-Type") {
		case "":
			fallthrough
		case "application/json":
			action, err = readJSONAction(r.Body)
			r.Body.Close()
			if err != nil {
				return fedwiki.ErrorResponse(http.StatusBadRequest, err.Error())
			}
		default:
			return fedwiki.ErrorResponse(http.StatusBadRequest, `Invalid request Content-Type "%s".`, r.Header.Get("Content-Type"))
		}

		page, err := pages.Load(slug)
		if err != nil {
			if err == fedwiki.ErrNotExist {
				return fedwiki.ErrorResponse(http.StatusNotFound, `Page "%s" does not exist.`, slug)
			}
			return fedwiki.ErrorResponse(http.StatusInternalServerError, err.Error())
		}

		if err := page.Apply(action); err != nil {
			return fedwiki.ErrorResponse(http.StatusInternalServerError, err.Error())
		}

		if err := pages.Save(slug, page); err != nil {
			return fedwiki.ErrorResponse(http.StatusInternalServerError, err.Error())
		}

		return http.StatusOK, "", page
	default:
		return fedwiki.ErrorResponse(http.StatusNotAcceptable, `Unknown request Method "%s".`, r.Method)
	}
}