aboutsummaryrefslogtreecommitdiff
path: root/cmd/monfront/create.go
blob: 43805a1d94bbac416a32b844b0f958cc7e904104 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"
	"strings"
)

func showCreate(con *Context) {
	if con.r.Method == "POST" {
		addCreate(con)
		return
	}
	if con.r.Method != "GET" {
		con.w.WriteHeader(http.StatusMethodNotAllowed)
		con.w.Write([]byte("method is not supported"))
		return
	}
	if !con.CanEdit {
		con.w.WriteHeader(http.StatusForbidden)
		con.w.Write([]byte("no permission to change data"))
		return
	}
	con.Content = map[string]any{}

	primitives := []struct {
		name  string
		query string
	}{
		{"commands", "select id, name, updated, command, message from commands order by name"},
		{"checkers", "select id, name, description from checkers order by name"},
		{"notifier", "select id, name, settings from notifier order by name"},
		{"nodes", "select id, name, updated, message from nodes order by name"},
	}
	for _, prim := range primitives {
		rows, err := DB.Query(prim.query)
		defer rows.Close()
		if err != nil {
			log.Printf("could not get commands: %s", err)
			con.Error = "could not get commands"
			returnError(http.StatusInternalServerError, con, con.w)
			return
		}
		result, err := rowsToResult(rows)
		if err != nil {
			log.Printf("could not get %s: %s", prim.name, err)
			con.Error = "could not get " + prim.name
			returnError(http.StatusInternalServerError, con, con.w)
			return
		}
		con.Content[prim.name] = result
	}

	con.w.Header()["Content-Type"] = []string{"text/html"}
	con.Render("create_index")
	return
}

type (
	sqlResult struct {
		Columns []string
		Rows    [][]sql.NullString
	}
)

func rowsToResult(rows *sql.Rows) (*sqlResult, error) {
	res := &sqlResult{}
	cols, err := rows.Columns()
	if err != nil {
		return res, fmt.Errorf("could not get columns: %w", err)
	}
	res.Columns = cols
	res.Rows = [][]sql.NullString{}
	colNum := len(cols)

	for rows.Next() {
		line := make([]sql.NullString, colNum)
		lineMap := make([]any, colNum)
		for i := 0; i < colNum; i++ {
			lineMap[i] = &(line[i])
		}
		if err := rows.Scan(lineMap...); err != nil {
			return res, fmt.Errorf("could not scan values: %w", err)
		}
		res.Rows = append(res.Rows, line)
	}

	return res, nil
}

func addCreate(con *Context) {
	if !con.CanEdit {
		con.w.WriteHeader(http.StatusForbidden)
		con.w.Write([]byte("no permission to change data"))
		return
	}

	if err := con.r.ParseForm(); err != nil {
		con.w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(con.w, "could not parse parameters: %s", err)
		return
	}

	con.w.Header()["Location"] = []string{"/create"}

	addType := con.r.PostForm.Get("type")
	types := map[string]struct {
		Fields []string
		Table  string
	}{
		"command":  {[]string{"name", "command", "message"}, "commands"},
		"node":     {[]string{"name", "message"}, "nodes"},
		"checker":  {[]string{"name", "description"}, "checkers"},
		"notifier": {[]string{"name", "settings"}, "notifier"},
		"check":    {[]string{"name", "message", "options", "intval", "node_id", "command_id", "checker_id"}, "checks"},
	}
	t, found := types[addType]
	if !found {
		con.Error = "undefined type '" + addType + "'"
		returnError(http.StatusBadRequest, con, con.w)
		return
	}

	fields := make([]any, len(t.Fields))
	vals := make([]string, len(t.Fields))
	for i := 0; i < len(fields); i++ {
		vals[i] = fmt.Sprintf(`$%d`, i+1)
		fields[i] = con.r.PostForm.Get(t.Fields[i])
		if fields[i] == "" {
			con.Error = "field " + t.Fields[i] + " must not be empty"
			returnError(http.StatusBadRequest, con, con.w)
			return
		}
	}
	stmt := `insert into ` + t.Table + `(` + strings.Join(t.Fields, ",") + `) values (` + strings.Join(vals, ",") + `)`
	_, err := DB.Exec(stmt, fields...)
	if err != nil {
		log.Printf("could not insert new %s: %s", addType, err)
		con.Error = "could not insert new " + addType
		returnError(http.StatusInternalServerError, con, con.w)
		return
	}

	con.w.WriteHeader(http.StatusSeeOther)
	return
}