diff --git a/core/groups.go b/core/groups.go index fcd4d9f7..785eb9ed 100644 --- a/core/groups.go +++ b/core/groups.go @@ -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 } diff --git a/core/sample.go b/core/sample.go index f4a72f2f..3b6dc473 100644 --- a/core/sample.go +++ b/core/sample.go @@ -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 diff --git a/handlers/groups.go b/handlers/groups.go index bffff987..4a801b5b 100644 --- a/handlers/groups.go +++ b/handlers/groups.go @@ -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) diff --git a/handlers/handlers.go b/handlers/handlers.go index 2c2cd9f8..cfbb372f 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -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) diff --git a/handlers/services.go b/handlers/services.go index c74a8b46..5265fc03 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -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) } diff --git a/source/tmpl/form_group.gohtml b/source/tmpl/form_group.gohtml index 9badb4cd..c011bb11 100644 --- a/source/tmpl/form_group.gohtml +++ b/source/tmpl/form_group.gohtml @@ -13,7 +13,15 @@ - +
+ +
+ + + + +
+
diff --git a/source/tmpl/form_service.gohtml b/source/tmpl/form_service.gohtml index eada6fbd..8b146463 100644 --- a/source/tmpl/form_service.gohtml +++ b/source/tmpl/form_service.gohtml @@ -107,7 +107,7 @@
diff --git a/source/tmpl/index.gohtml b/source/tmpl/index.gohtml index b9edaae1..e5fa5993 100644 --- a/source/tmpl/index.gohtml +++ b/source/tmpl/index.gohtml @@ -8,39 +8,23 @@
{{ .Description }}
{{ end }} - {{if ne (len Groups) 0}} - {{ range Groups }} -
-

{{.Name}}

- -
- {{end}} - {{else}} +{{ range Groups true }} + - {{end}} +
+{{end}} {{ if .Messages }}
diff --git a/source/tmpl/services.gohtml b/source/tmpl/services.gohtml index 6d71009d..551e8cbe 100644 --- a/source/tmpl/services.gohtml +++ b/source/tmpl/services.gohtml @@ -11,6 +11,7 @@ Name Status + Visibility @@ -18,7 +19,8 @@ {{range .Services}} {{.Name}} - {{if .Online}}ONLINE{{else}}OFFLINE{{end}} + {{if .Online}}ONLINE{{else}}OFFLINE{{end}} + {{if .Public.Bool}}PUBLIC{{else}}PRIVATE{{end}}
View @@ -43,14 +45,16 @@ Name Services + Visibility - {{range .Groups}} + {{range Groups false}} {{.Name}} {{len .Services}} + {{if .Public.Bool}}PUBLIC{{else}}PRIVATE{{end}}
{{if Auth}}{{end}} diff --git a/source/wiki.go b/source/wiki.go index 4f790729..e11f17e4 100644 --- a/source/wiki.go +++ b/source/wiki.go @@ -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 diff --git a/types/group.go b/types/group.go index 3cde59da..18384ef5 100644 --- a/types/group.go +++ b/types/group.go @@ -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"` } diff --git a/utils/utils.go b/utils/utils.go index eced735f..753a7915 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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,