view pkg/controllers/system.go @ 812:98a7776100fb

Added end point lo list available surveys for a given bottleneck.
author Sascha Wilde <wilde@intevation.de>
date Thu, 27 Sep 2018 17:25:03 +0200
parents 815f5e2ed974
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
}