Skip to content

Commit

Permalink
Server info route, Docker refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleu committed Jul 25, 2023
1 parent 3664834 commit d6c386a
Show file tree
Hide file tree
Showing 16 changed files with 524 additions and 303 deletions.
35 changes: 9 additions & 26 deletions app/controller/clib/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package clib

import (
"fmt"
"os"
"runtime"
"runtime/pprof"
"strings"
Expand All @@ -30,10 +29,14 @@ func Admin(rc *fasthttp.RequestCtx) {
return controller.Render(rc, as, &vadmin.Settings{Perms: user.GetPermissions()}, ps, "admin")
}
switch path[0] {
case "server":
info := util.DebugGetInfo(as.BuildInfo.Version, as.Started)
ps.Data = info
return controller.Render(rc, as, &vadmin.ServerInfo{Info: info}, ps, "admin", "Server Information")
case "cpu":
switch path[1] {
case "start":
err := startCPUProfile()
err := util.DebugStartCPUProfile()
if err != nil {
return "", err
}
Expand All @@ -50,14 +53,13 @@ func Admin(rc *fasthttp.RequestCtx) {
msg := fmt.Sprintf("ran garbage collection in [%s]", timer.EndString())
return controller.FlashAndRedir(true, msg, "/admin", rc, ps)
case "heap":
err := takeHeapProfile()
err := util.DebugTakeHeapProfile()
if err != nil {
return "", err
}
return controller.FlashAndRedir(true, "wrote heap profile", "/admin", rc, ps)
case "memusage":
x := &runtime.MemStats{}
runtime.ReadMemStats(x)
x := util.DebugMemStats()
ps.Data = x
return controller.Render(rc, as, &vadmin.MemUsage{Mem: x}, ps, "admin", "Memory Usage")
case "migrations":
Expand All @@ -66,13 +68,10 @@ func Admin(rc *fasthttp.RequestCtx) {
ps.Data = util.ValueMap{"available": ms, "applied": am}
return controller.Render(rc, as, &vadmin.Migrations{Available: ms, Applied: am}, ps, "admin", "Migrations")
case "modules":
di, err := util.GetDebugInfo()
if err != nil {
return "", err
}
di := util.DebugBuildInfo().Deps
ps.Title = "Modules"
ps.Data = di
return controller.Render(rc, as, &vadmin.Modules{Info: di}, ps, "admin", "Modules")
return controller.Render(rc, as, &vadmin.Modules{Modules: di}, ps, "admin", "Modules")
case "request":
ps.Title = "Request Debug"
ps.Data = cutil.RequestCtxToMap(rc, nil)
Expand All @@ -98,19 +97,3 @@ func Admin(rc *fasthttp.RequestCtx) {
}
})
}

func startCPUProfile() error {
f, err := os.Create("./tmp/cpu.pprof")
if err != nil {
return err
}
return pprof.StartCPUProfile(f)
}

func takeHeapProfile() error {
f, err := os.Create("./tmp/mem.pprof")
if err != nil {
return err
}
return pprof.WriteHeapProfile(f)
}
126 changes: 14 additions & 112 deletions app/lib/user/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ package user
import (
"strings"

"github.com/samber/lo"
"golang.org/x/exp/slices"

"github.com/kyleu/rituals/app/util"
)

Expand All @@ -17,23 +14,7 @@ type Account struct {
Token string `json:"-"`
}

func accountFromString(s string) *Account {
p, e := util.StringSplit(s, ':', true)
var t, pic string
if strings.Contains(e, "|") {
e, t = util.StringSplit(e, '|', true)
if decr, err := util.DecryptMessage(nil, t, nil); err == nil {
t = decr
if idx := strings.LastIndex(t, "@@"); idx > -1 {
pic = t[idx+2:]
t = t[:idx]
}
}
}
return &Account{Provider: p, Email: e, Picture: pic, Token: t}
}

func (a Account) String() string {
func (a *Account) String() string {
ret := a.Provider + ":" + a.Email
if a.Token != "" {
msg := a.Token
Expand All @@ -49,109 +30,30 @@ func (a Account) String() string {
return ret
}

func (a Account) TitleString() string {
func (a *Account) TitleString() string {
return a.Provider + ":" + a.Email
}

func (a Account) Domain() string {
func (a *Account) Domain() string {
if a.Email == "" || !strings.Contains(a.Email, "@") {
return ""
}
_, r := util.StringSplitLast(a.Email, '@', true)
return r
}

type Accounts []*Account

func (a Accounts) String() string {
return strings.Join(lo.Map(a, func(x *Account, _ int) string {
return x.String()
}), ",")
}

func (a Accounts) TitleString() string {
return strings.Join(lo.Map(a, func(x *Account, _ int) string {
return x.TitleString()
}), ",")
}

func (a Accounts) Images() []string {
ret := make(util.KeyVals[string], 0, len(a))
lo.ForEach(a, func(x *Account, _ int) {
if x.Picture != "" {
ret = append(ret, &util.KeyVal[string]{Key: x.Provider, Val: x.Picture})
}
})
return ret.Values()
}

func (a Accounts) Image() string {
if is := a.Images(); len(is) > 0 {
return is[0]
}
return ""
}

func (a Accounts) Sort() {
slices.SortFunc(a, func(l *Account, r *Account) bool {
if l.Provider == r.Provider {
return strings.ToLower(l.Email) < strings.ToLower(r.Email)
}
return l.Provider < r.Provider
})
}

func (a Accounts) GetByProvider(p string) Accounts {
return lo.Filter(a, func(x *Account, _ int) bool {
return x.Provider == p
})
}

func (a Accounts) GetByProviderDomain(p string, d string) *Account {
return lo.FindOrElse(a, nil, func(x *Account) bool {
return x.Provider == p && x.Domain() == d
})
}

func (a Accounts) Matches(match string) bool {
if match == "" || match == "*" {
return true
}
if strings.Contains(match, ",") {
return lo.ContainsBy(util.StringSplitAndTrim(match, ","), func(x string) bool {
return a.Matches(x)
})
}
prv, acct := util.StringSplit(match, ':', true)
for _, x := range a {
if x.Provider == prv {
if acct == "" {
return true
}
return strings.HasSuffix(x.Email, acct)
}
}
return false
}

func (a Accounts) Purge(keys ...string) Accounts {
ret := make(Accounts, 0, len(a))
for _, ss := range a {
hit := false
for _, key := range keys {
if ss.Provider == key {
hit = true
func accountFromString(s string) *Account {
p, e := util.StringSplit(s, ':', true)
var t, pic string
if strings.Contains(e, "|") {
e, t = util.StringSplit(e, '|', true)
if decr, err := util.DecryptMessage(nil, t, nil); err == nil {
t = decr
if idx := strings.LastIndex(t, "@@"); idx > -1 {
pic = t[idx+2:]
t = t[:idx]
}
}
if !hit {
ret = append(ret, ss)
}
}
return ret
}

func AccountsFromString(s string) Accounts {
return lo.Map(util.StringSplitAndTrim(s, ","), func(x string, _ int) *Account {
return accountFromString(x)
})
return &Account{Provider: p, Email: e, Picture: pic, Token: t}
}
106 changes: 106 additions & 0 deletions app/lib/user/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Content managed by Project Forge, see [projectforge.md] for details.
package user

import (
"strings"

"github.com/samber/lo"
"golang.org/x/exp/slices"

"github.com/kyleu/rituals/app/util"
)

type Accounts []*Account

func (a Accounts) String() string {
return strings.Join(lo.Map(a, func(x *Account, _ int) string {
return x.String()
}), ",")
}

func (a Accounts) TitleString() string {
return strings.Join(lo.Map(a, func(x *Account, _ int) string {
return x.TitleString()
}), ",")
}

func (a Accounts) Images() []string {
ret := make(util.KeyVals[string], 0, len(a))
lo.ForEach(a, func(x *Account, _ int) {
if x.Picture != "" {
ret = append(ret, &util.KeyVal[string]{Key: x.Provider, Val: x.Picture})
}
})
return ret.Values()
}

func (a Accounts) Image() string {
if is := a.Images(); len(is) > 0 {
return is[0]
}
return ""
}

func (a Accounts) Sort() {
slices.SortFunc(a, func(l *Account, r *Account) bool {
if l.Provider == r.Provider {
return strings.ToLower(l.Email) < strings.ToLower(r.Email)
}
return l.Provider < r.Provider
})
}

func (a Accounts) GetByProvider(p string) Accounts {
return lo.Filter(a, func(x *Account, _ int) bool {
return x.Provider == p
})
}

func (a Accounts) GetByProviderDomain(p string, d string) *Account {
return lo.FindOrElse(a, nil, func(x *Account) bool {
return x.Provider == p && x.Domain() == d
})
}

func (a Accounts) Matches(match string) bool {
if match == "" || match == "*" {
return true
}
if strings.Contains(match, ",") {
return lo.ContainsBy(util.StringSplitAndTrim(match, ","), func(x string) bool {
return a.Matches(x)
})
}
prv, acct := util.StringSplit(match, ':', true)
for _, x := range a {
if x.Provider == prv {
if acct == "" {
return true
}
return strings.HasSuffix(x.Email, acct)
}
}
return false
}

func (a Accounts) Purge(keys ...string) Accounts {
ret := make(Accounts, 0, len(a))
for _, ss := range a {
hit := false
for _, key := range keys {
if ss.Provider == key {
hit = true
}
}
if !hit {
ret = append(ret, ss)
}
}
return ret
}

func AccountsFromString(s string) Accounts {
return lo.Map(util.StringSplitAndTrim(s, ","), func(x string, _ int) *Account {
return accountFromString(x)
})
}
Loading

0 comments on commit d6c386a

Please sign in to comment.