don't depend on wc for online user count; wip meta info in json

This commit is contained in:
Arya Kiran 2023-01-17 19:35:28 +05:30
parent 4321c7230b
commit d1c36f7af4
Signed by: arya
GPG Key ID: 842D12BDA50DF120
3 changed files with 104 additions and 15 deletions

1
go.mod
View File

@ -19,4 +19,5 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

2
go.sum
View File

@ -245,4 +245,6 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

116
main.go
View File

@ -1,19 +1,90 @@
package main
import (
"os"
"os/exec"
"fmt"
"github.com/ProjectSegfault/publapi/pages"
"github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
)
type Userstruct struct {
Status int
Online int
Users []Userinfo
}
type Userinfo struct {
name string
fullname string
loc string
email string
desc string
website string
capsule string
online string
}
func Dedup(input string) string {
unique := []string{}
words := strings.Split(input, " ")
for _, word := range words {
// If we alredy have this word, skip.
if contains(unique, word) {
continue
}
unique = append(unique, word)
}
return strings.Join(unique, " ")
}
func contains(strs []string, str string) bool {
for _, s := range strs {
if s == str {
return true
}
}
return false
}
func confparse(username, item string) string {
filename := "/home/" + username + "/meta-info.yaml"
file, err := ioutil.ReadFile(filename)
if err != nil {
log.Error(err)
}
parsedData := make(map[interface{}]interface{})
err2 := yaml.Unmarshal(file, &parsedData)
if err2 != nil {
log.Error(err2)
}
val, err3 := parsedData[item].(string)
if !err3 {
log.Error(err3)
}
return val
}
func userdata(username string) Userinfo {
var user Userinfo
user.name = confparse(username, "name")
user.fullname = confparse(username, "fullname")
user.capsule = confparse(username, "capsule")
user.website = confparse(username, "website")
user.desc = confparse(username, "desc")
user.email = confparse(username, "email")
user.loc = confparse(username, "loc")
//fmt.Println(user)
return user
}
// publapi is a simple API for Project Segfault's public shared server (pubnix).
func main() {
app := fiber.New()
@ -33,19 +104,34 @@ func main() {
})
}
// Get the number of users online
out, err := exec.Command("bash", "-c", "/usr/bin/users | /usr/bin/wc -l").Output()
log.Info(string(out))
usersonline, err := exec.Command("bash", "-c", "/usr/bin/users").Output()
usersonlinestr := string(usersonline)
usersonlinededup := Dedup(usersonlinestr)
outputa := int(strings.Count(usersonlinededup, " "))
var output int = 0
output = outputa + 1
if err != nil {
log.Error(err)
return c.SendStatus(fiber.StatusInternalServerError)
}
output := string(out)
return c.JSON(fiber.Map{
"users": strings.TrimSuffix(output, "\n"),
"status": c.Response().StatusCode(),
})
users, err2 := exec.Command("bash", "-c", "/usr/bin/ls /home").Output()
if err2 != nil {
log.Error(err2)
}
userstr := string(users)
userstr2 := strings.TrimSuffix(userstr, "\n")
usersarr := strings.Split(userstr2, "\n")
//var userinfoarr []interface{}
var userinfostruct []Userinfo
for i := 0; i < len(usersarr); i++ {
uname := string(usersarr[i])
userinfostruct = append(userinfostruct, userdata(uname))
}
data := Userstruct{
Status: c.Response().StatusCode(),
Online: output,
Users: userinfostruct,
}
return c.JSON(data)
})
app.Post("/signup", pages.SignupPage)