2023-01-07 21:03:51 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-01-07 22:14:25 +05:30
|
|
|
"os"
|
2023-01-07 21:03:51 +05:30
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/ProjectSegfault/publapi/pages"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2023-01-07 22:24:04 +05:30
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-01-07 22:46:35 +05:30
|
|
|
|
|
|
|
"runtime"
|
2023-01-07 21:03:51 +05:30
|
|
|
)
|
|
|
|
|
2023-01-07 21:32:06 +05:30
|
|
|
// publapi is a simple API for Project Segfault's public shared server (pubnix).
|
2023-01-07 21:03:51 +05:30
|
|
|
func main() {
|
|
|
|
app := fiber.New()
|
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"message": "welcome to publapi",
|
|
|
|
"status": c.Response().StatusCode(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Get("/online", func(c *fiber.Ctx) error {
|
2023-01-07 22:46:35 +05:30
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"message": "/online is not supported on Windows",
|
|
|
|
"status": c.Response().StatusCode(),
|
|
|
|
})
|
|
|
|
}
|
2023-01-07 21:03:51 +05:30
|
|
|
// Get the number of users online
|
2023-01-07 22:51:44 +05:30
|
|
|
out, err := exec.Command("bash", "-c", "/usr/bin/users | /usr/bin/wc -l").Output()
|
2023-01-07 22:56:14 +05:30
|
|
|
log.Info(string(out))
|
2023-01-07 21:03:51 +05:30
|
|
|
if err != nil {
|
2023-01-07 22:24:04 +05:30
|
|
|
log.Error(err)
|
2023-01-07 21:03:51 +05:30
|
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2023-01-07 23:27:57 +05:30
|
|
|
output := string(out)
|
|
|
|
|
2023-01-07 21:03:51 +05:30
|
|
|
return c.JSON(fiber.Map{
|
2023-01-07 23:27:57 +05:30
|
|
|
"users": output,
|
2023-01-07 21:03:51 +05:30
|
|
|
"status": c.Response().StatusCode(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.Post("/signup", pages.SignupPage)
|
|
|
|
|
2023-01-07 22:15:42 +05:30
|
|
|
app.Listen(GetPort())
|
2023-01-07 21:03:51 +05:30
|
|
|
}
|
2023-01-07 22:14:25 +05:30
|
|
|
|
|
|
|
// GetPort returns the port to listen on
|
|
|
|
func GetPort() string {
|
|
|
|
port := os.Getenv("PUBLAPI_PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "3000"
|
|
|
|
}
|
|
|
|
return ":" + port
|
|
|
|
}
|