grouping - http proxy

pull/119/head
Hunter Long 2019-01-03 13:09:11 -08:00
parent 0b7687aa85
commit df2eb5c50a
12 changed files with 64 additions and 44 deletions

View File

@ -41,15 +41,29 @@ func (g *Group) Services() []*Service {
}
// SelectGroups returns all groups
func SelectGroups() []*Group {
func SelectGroups(includeAll bool, auth bool) []*Group {
var groups []*Group
var validGroups []*Group
groupsDb().Find(&groups).Order("id desc")
return groups
if includeAll {
emptyGroup := &Group{&types.Group{Id: 0, Public: types.NewNullBool(true)}}
groups = append(groups, emptyGroup)
}
for _, g := range groups {
if !g.Public.Bool {
if auth {
validGroups = append(validGroups, g)
}
} else {
validGroups = append(validGroups, g)
}
}
return validGroups
}
// SelectGroup returns a *core.Group
func SelectGroup(id int64) *Group {
for _, g := range SelectGroups() {
for _, g := range SelectGroups(false, false) {
if g.Id == id {
return g
}

View File

@ -103,11 +103,13 @@ func InsertSampleData() error {
func insertSampleGroups() error {
group1 := &Group{&types.Group{
Name: "Main Services",
Name: "Main Services",
Public: types.NewNullBool(true),
}}
_, err := group1.Create()
group2 := &Group{&types.Group{
Name: "Linked Services",
Name: "Linked Services",
Public: types.NewNullBool(false),
}}
_, err = group2.Create()
return err

View File

@ -24,16 +24,19 @@ import (
"net/http"
)
// apiAllGroupHandler will show all the groups
func apiAllGroupHandler(w http.ResponseWriter, r *http.Request) {
if !IsReadAuthenticated(r) {
sendUnauthorizedJson(w, r)
return
}
groups := core.SelectGroups()
auth := IsUser(r)
groups := core.SelectGroups(false, auth)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(groups)
}
// apiGroupHandler will show a single group
func apiGroupHandler(w http.ResponseWriter, r *http.Request) {
if !IsReadAuthenticated(r) {
sendUnauthorizedJson(w, r)
@ -49,6 +52,7 @@ func apiGroupHandler(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(group)
}
// apiCreateGroupHandler accepts a POST method to create new groups
func apiCreateGroupHandler(w http.ResponseWriter, r *http.Request) {
if !IsFullAuthenticated(r) {
sendUnauthorizedJson(w, r)
@ -69,6 +73,7 @@ func apiCreateGroupHandler(w http.ResponseWriter, r *http.Request) {
sendJsonAction(group, "create", w, r)
}
// apiGroupDeleteHandler accepts a DELETE method to delete groups
func apiGroupDeleteHandler(w http.ResponseWriter, r *http.Request) {
if !IsFullAuthenticated(r) {
sendUnauthorizedJson(w, r)

View File

@ -192,8 +192,9 @@ var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap
"Services": func() []types.ServiceInterface {
return core.CoreApp.Services
},
"Groups": func() []*core.Group {
return core.SelectGroups()
"Groups": func(includeAll bool) []*core.Group {
auth := IsUser(r)
return core.SelectGroups(includeAll, auth)
},
"len": func(g interface{}) int {
val := reflect.ValueOf(g)

View File

@ -53,7 +53,6 @@ func servicesHandler(w http.ResponseWriter, r *http.Request) {
}
data := map[string]interface{}{
"Services": core.CoreApp.Services,
"Groups": core.SelectGroups(),
}
ExecuteResponse(w, r, "services.gohtml", data, nil)
}

View File

@ -13,7 +13,15 @@
<input type="text" name="name" class="form-control" value="{{.Name}}" id="title" placeholder="Group Name" required>
</div>
</div>
<div class="form-group row">
<label for="order" class="col-sm-4 col-form-label">Public Group</label>
<div class="col-8 mt-1">
<span class="switch float-left">
<input type="checkbox" name="public" class="switch" id="switch-group-public" checked>
<label for="switch-group-public">Show group services to the public</label>
</span>
</div>
</div>
<div class="form-group row">
<div class="col-sm-12">
<button type="submit" class="btn btn-primary btn-block">{{if ne .Id 0}}Update Group{{else}}Create Group{{end}}</button>

View File

@ -107,7 +107,7 @@
<div class="col-sm-8">
<select name="group_id" class="form-control" id="group_id">
<option value="0" {{if eq $s.GroupId 0}}selected{{end}}>None</option>
{{range Groups}}
{{range Groups false}}
<option value="{{.Id}}" {{if eq $s.GroupId .Id}}selected{{end}}>{{.Name}}</option>
{{end}}
</select>

View File

@ -8,39 +8,23 @@
<h5 class="col-12 text-center mb-5 header-desc">{{ .Description }}</h5>
{{ end }}
{{if ne (len Groups) 0}}
{{ range Groups }}
<div class="col-12 full-col-12">
<h4>{{.Name}}</h4>
<div class="list-group online_list mb-3">
{{ range .Services }}
<a href="#" class="service_li list-group-item list-group-item-action {{if not .Online}}bg-danger text-white{{ end }}" data-id="{{.Id}}">
{{ .Name }}
{{if .Online}}
<span class="badge bg-success float-right pulse-glow">ONLINE</span>
{{ else }}
<span class="badge bg-white text-black-50 float-right pulse">OFFLINE</span>
{{end}}
</a>
{{ end }}
</div>
</div>
{{end}}
{{else}}
{{ range Groups true }}
<div class="col-12 full-col-12">
<h4>{{.Name}}</h4>
<div class="list-group online_list mb-3">
{{ range Services }}
<a href="#" class="service_li list-group-item list-group-item-action {{if not .Online}}bg-danger text-white{{ end }}" data-id="{{.Id}}">
{{ .Name }}
{{if .Online}}
<span class="badge bg-success float-right pulse-glow">ONLINE</span>
{{ else }}
<span class="badge bg-white text-black-50 float-right pulse">OFFLINE</span>
{{end}}
</a>
{{ range .Services }}
<a href="#" class="service_li list-group-item list-group-item-action {{if not .Online}}bg-danger text-white{{ end }}" data-id="{{.Id}}">
{{ .Name }}
{{if .Online}}
<span class="badge bg-success float-right pulse-glow">ONLINE</span>
{{ else }}
<span class="badge bg-white text-black-50 float-right pulse">OFFLINE</span>
{{end}}
</a>
{{ end }}
</div>
</div>
{{end}}
</div>
{{end}}
{{ if .Messages }}
<div class="col-12">

View File

@ -11,6 +11,7 @@
<tr>
<th scope="col">Name</th>
<th scope="col" class="d-none d-md-table-cell">Status</th>
<th scope="col">Visibility</th>
<th scope="col"></th>
</tr>
</thead>
@ -18,7 +19,8 @@
{{range .Services}}
<tr id="service_{{.Id}}" data-id="{{.Id}}">
<td><span class="drag_icon d-none d-md-inline"><i class="fas fa-bars"></i></span> {{.Name}}</td>
<td class="d-none d-md-table-cell">{{if .Online}}<span class="badge badge-success">ONLINE</span>{{else}}<span class="badge badge-danger">OFFLINE</span>{{end}} </td>
<td class="d-none d-md-table-cell">{{if .Online}}<span class="badge badge-success">ONLINE</span>{{else}}<span class="badge badge-danger">OFFLINE</span>{{end}}</td>
<td class="d-none d-md-table-cell">{{if .Public.Bool}}<span class="badge badge-primary">PUBLIC</span>{{else}}<span class="badge badge-secondary">PRIVATE</span>{{end}}</td>
<td class="text-right">
<div class="btn-group">
<a href="/service/{{.Id}}" class="btn btn-outline-secondary"><i class="fas fa-chart-area"></i> View</a>
@ -43,14 +45,16 @@
<tr>
<th scope="col">Name</th>
<th scope="col">Services</th>
<th scope="col">Visibility</th>
<th scope="col"></th>
</tr>
</thead>
<tbody id="groups_table">
{{range .Groups}}
{{range Groups false}}
<tr id="group_{{.Id}}" data-id="{{.Id}}">
<td>{{.Name}}</td>
<td>{{len .Services}}</td>
<td>{{if .Public.Bool}}<span class="badge badge-primary">PUBLIC</span>{{else}}<span class="badge badge-secondary">PRIVATE</span>{{end}}</td>
<td class="text-right">
<div class="btn-group">
{{if Auth}}<a href="/api/groups/{{.Id}}" class="ajax_delete btn btn-danger" data-method="DELETE" data-obj="group_{{.Id}}" data-id="{{.Id}}"><i class="fas fa-times"></i></a>{{end}}

View File

@ -1,6 +1,6 @@
// Code generated by go generate; DO NOT EDIT.
// This file was generated by robots at
// 2019-01-03 11:12:03.7014 -0800 PST m=+0.913709962
// 2019-01-03 13:06:31.051097 -0800 PST m=+1.051596230
//
// This contains the most recently Markdown source for the Statping Wiki.
package source

View File

@ -6,6 +6,7 @@ import "time"
type Group struct {
Id int64 `gorm:"primary_key;column:id" json:"id"`
Name string `gorm:"column:name" json:"name"`
Public NullBool `gorm:"default:false;column:public" json:"public"`
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
}

View File

@ -231,6 +231,7 @@ func SaveFile(filename string, data []byte) error {
// // headers - An array of Headers to be sent (KEY=VALUE) []string{"Authentication=12345", ...}
// // body - The body or form data to send with HTTP request
// // timeout - Specific duration to timeout on. time.Duration(30 * time.Seconds)
// // You can use a HTTP Proxy if you HTTP_PROXY environment variable
func HttpRequest(url, method string, content interface{}, headers []string, body io.Reader, timeout time.Duration) ([]byte, *http.Response, error) {
var err error
transport := &http.Transport{
@ -240,6 +241,7 @@ func HttpRequest(url, method string, content interface{}, headers []string, body
DisableKeepAlives: true,
ResponseHeaderTimeout: timeout,
TLSHandshakeTimeout: timeout,
Proxy: http.ProxyFromEnvironment,
}
client := &http.Client{
Transport: transport,