2022-10-08 23:23:30 +13:00
|
|
|
package apiv1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/axllent/mailpit/config"
|
2023-09-25 19:25:45 +13:00
|
|
|
"github.com/axllent/mailpit/internal/storage"
|
2023-09-25 18:08:04 +13:00
|
|
|
"github.com/axllent/mailpit/internal/updater"
|
2022-10-08 23:23:30 +13:00
|
|
|
)
|
|
|
|
|
2023-04-21 11:55:32 +12:00
|
|
|
// Response includes the current and latest Mailpit version, database info, and memory usage
|
2023-03-31 17:29:04 +13:00
|
|
|
//
|
|
|
|
// swagger:model AppInformation
|
|
|
|
type appInformation struct {
|
|
|
|
// Current Mailpit version
|
|
|
|
Version string
|
|
|
|
// Latest Mailpit version
|
2022-10-08 23:23:30 +13:00
|
|
|
LatestVersion string
|
2023-03-31 17:29:04 +13:00
|
|
|
// Database path
|
|
|
|
Database string
|
|
|
|
// Database size in bytes
|
|
|
|
DatabaseSize int64
|
|
|
|
// Total number of messages in the database
|
|
|
|
Messages int
|
|
|
|
// Current memory usage in bytes
|
|
|
|
Memory uint64
|
2022-10-08 23:23:30 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppInfo returns some basic details about the running app, and latest release.
|
|
|
|
func AppInfo(w http.ResponseWriter, r *http.Request) {
|
2023-03-31 17:29:04 +13:00
|
|
|
// swagger:route GET /api/v1/info application AppInformation
|
|
|
|
//
|
2023-04-21 11:55:32 +12:00
|
|
|
// # Get application information
|
2023-03-31 17:29:04 +13:00
|
|
|
//
|
|
|
|
// Returns basic runtime information, message totals and latest release version.
|
|
|
|
//
|
|
|
|
// Produces:
|
2023-04-21 11:55:32 +12:00
|
|
|
// - application/json
|
2023-03-31 17:29:04 +13:00
|
|
|
//
|
|
|
|
// Schemes: http, https
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: InfoResponse
|
|
|
|
// default: ErrorResponse
|
|
|
|
info := appInformation{}
|
2022-10-08 23:23:30 +13:00
|
|
|
info.Version = config.Version
|
|
|
|
|
2022-10-16 11:36:28 +13:00
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
|
|
|
|
info.Memory = m.Sys - m.HeapReleased
|
|
|
|
|
2022-10-08 23:23:30 +13:00
|
|
|
latest, _, _, err := updater.GithubLatest(config.Repo, config.RepoBinaryName)
|
|
|
|
if err == nil {
|
|
|
|
info.LatestVersion = latest
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Database = config.DataFile
|
|
|
|
|
|
|
|
db, err := os.Stat(info.Database)
|
|
|
|
if err == nil {
|
|
|
|
info.DatabaseSize = db.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Messages = storage.CountTotal()
|
|
|
|
|
|
|
|
bytes, _ := json.Marshal(info)
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
_, _ = w.Write(bytes)
|
|
|
|
}
|