view pkg/controllers/system.go @ 904:e4b72a199258

New default bottleneck colors Mainly to make the stroke color one actually selectable in the ui. In addition the pink does better match the collors used on the ECDIS layer.
author Sascha Wilde <wilde@intevation.de>
date Tue, 02 Oct 2018 13:34:59 +0200
parents 83c271cb2344
children a244b18cb916
line wrap: on
line source

package controllers

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

	"gemma.intevation.de/gemma/pkg/models"
	"github.com/gorilla/mux"
)

const (
	getFeatureColourSQL = `SELECT r,g,b,a
FROM systemconf.feature_colours
WHERE feature_name = $1 AND style_attr = $2`
	setFeatureColourSQL = `UPDATE systemconf.feature_colours
SET (r, g, b, a) = ($3, $4, $5, $6)
WHERE feature_name = $1 AND style_attr = $2`
)

// System status end points

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
}

// Map/Feature style end points

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

	feature := mux.Vars(req)["feature"]
	attr := mux.Vars(req)["attr"]

	c := models.Colour{}
	err = db.QueryRowContext(
		req.Context(),
		getFeatureColourSQL,
		feature, attr,
	).Scan(&c.R, &c.G, &c.B, &c.A)

	switch {
	case err == sql.ErrNoRows:
		err = JSONError{
			Code:    http.StatusNotFound,
			Message: "Requestes style not found.",
		}
		return
	case err != nil:
		return
	}

	jr.Result = &struct {
		Colour models.Colour `json:"colour"`
		Code   string        `json:"code"`
	}{c, fmt.Sprintf("rgba(%d, %d, %d, %g)", c.R, c.G, c.B, c.A)}
	return
}

func setFeatureStyle(
	input interface{},
	req *http.Request,
	db *sql.Conn,
) (jr JSONResult, err error) {

	feature := mux.Vars(req)["feature"]
	attr := mux.Vars(req)["attr"]

	c := input.(*models.Colour)
	if !c.IsValid() {
		err = JSONError{http.StatusBadRequest, "error: invalid colours"}
		return
	}

	var res sql.Result
	res, err = db.ExecContext(
		req.Context(),
		setFeatureColourSQL,
		feature, attr,
		c.R, c.G, c.B, c.A)

	if err != nil {
		return
	}

	if n, err2 := res.RowsAffected(); err2 == nil && n == 0 {
		err = JSONError{
			Code:    http.StatusNotFound,
			Message: "Requestes style not found.",
		}
		return
	}

	jr = JSONResult{
		Code: http.StatusCreated,
		Result: struct {
			Result string `json:"result"`
		}{"success"},
	}
	return
}