view pkg/controllers/system.go @ 722:815f5e2ed974

Added simple endpoint to view system logs. api/system/log/{service}/{file} allows to retrieve the raw log file content for a selected set of services (currently "apache2" and "postgresql"). The result is a JSON with two fields: "path:" contains the fully qualified path of the retrieved log file in the servers file system and "content:" provides the verbatim content of the requested log file.
author Sascha Wilde <wilde@intevation.de>
date Fri, 21 Sep 2018 18:01:39 +0200
parents
children 0f61bfc21041
line wrap: on
line source

package controllers

import (
	"database/sql"
	"io/ioutil"
	"net/http"
	"strings"

	"github.com/gorilla/mux"
)

func showSystemLog(
	_ interface{}, req *http.Request,
	_ *sql.Conn,
) (jr JSONResult, err error) {

	serviceName := mux.Vars(req)["service"]
	fileName := mux.Vars(req)["file"]

	// The following check is currently most likely unnecessary as I wasn't
	// able to inject a verbatim '/' via the middleware, but better be on
	// the safe site...
	if strings.Contains(fileName, "/") {
		err = JSONError{http.StatusBadRequest,
			"error: no slashes allowed in file name"}
		return
	}

	var path string

	switch serviceName {
	case "apache2", "postgresql":
		path = "/var/log/" + serviceName + "/" + fileName
	default:
		err = JSONError{http.StatusBadRequest,
			"error: invalid service: " + serviceName}
		return
	}

	var txt []byte

	if txt, err = ioutil.ReadFile(path); err != nil {
		return
	}

	jr = JSONResult{
		Result: struct {
			Path    string `json:"path"`
			Content string `json:"content"`
		}{path, string(txt)},
	}
	return
}