The smallest watchdog on earth. Tiny, monitoring-plugins compatible monitoring with a status page. https://cloud.docker.com/repository/docker/momar/chihuahua/general
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package chihuahua
  2. //go:generate go-bindata -pkg web -fs -prefix web -o web/data.go -ignore data\\.go web web/icons web/lib
  3. import (
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "codeberg.org/momar/chihuahua/types"
  10. "codeberg.org/momar/chihuahua/web"
  11. "codeberg.org/momar/logg"
  12. "codeberg.org/momar/ternary"
  13. "github.com/aofei/air"
  14. )
  15. func Api(servers map[string]*types.Server) {
  16. // getCheck returns the check results
  17. getCheck := func(req *air.Request, res *air.Response) error {
  18. result := map[string]map[string]*types.Check{}
  19. for serverName, server := range servers {
  20. result[serverName] = map[string]*types.Check{}
  21. for checkName, check := range server.Checks {
  22. result[serverName][checkName] = check
  23. }
  24. }
  25. s := req.Param("server").Value()
  26. c := req.Param("check").Value()
  27. if c != nil || s != nil {
  28. sres, ok := result[s.String()]
  29. if ok && c != nil {
  30. cres, ok := sres[c.String()]
  31. if ok {
  32. return res.WriteJSON(cres)
  33. }
  34. } else if ok {
  35. return res.WriteJSON(sres)
  36. }
  37. res.Status = 404
  38. return res.WriteJSON(map[string]bool{})
  39. }
  40. return res.WriteJSON(result)
  41. }
  42. // putMessage adds a message to a check
  43. putMessage := func(req *air.Request, res *air.Response) error {
  44. // TODO
  45. return nil
  46. }
  47. // deleteMessage deletes the message from a check
  48. deleteMessage := func(req *air.Request, res *air.Response) error {
  49. // TODO:
  50. return nil
  51. }
  52. getSetupScript := func(req *air.Request, res *air.Response) error {
  53. files, _ := filepath.Glob(filepath.Join(os.Getenv("HOME"), ".ssh/id_*.pub"))
  54. keys := ""
  55. for _, file := range files {
  56. key, _ := ioutil.ReadFile(file)
  57. keys += strings.TrimSpace(string(key)) + "\n"
  58. }
  59. res.WriteString(SetupScript(string(keys)))
  60. return nil
  61. }
  62. app := air.New()
  63. app.GET("/setup.sh", getSetupScript)
  64. app.GET("/checks", getCheck)
  65. app.GET("/checks/:server", getCheck)
  66. app.GET("/checks/:server/:check", getCheck)
  67. app.PUT("/checks/:server/:check", putMessage)
  68. app.DELETE("/checks/:server/:check", deleteMessage)
  69. app.NotFoundHandler = air.WrapHTTPHandler(http.FileServer(web.AssetFile()))
  70. app.Address = ternary.Default(os.Getenv("ADDRESS"), ":8080").(string)
  71. logg.Tag("server").Info("Listening on %s", app.Address)
  72. err := app.Serve()
  73. if err != nil {
  74. logg.Error("%s", err)
  75. os.Exit(2)
  76. }
  77. }