app/control/service.go

71 lines
1.9 KiB
Go

package control
import (
"encoding/json"
"strings"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"kumoly.io/kumoly/app/auth"
"kumoly.io/kumoly/app/email"
"kumoly.io/kumoly/app/errors"
"kumoly.io/kumoly/app/server"
"kumoly.io/kumoly/app/system"
"kumoly.io/kumoly/app/task"
)
type Service struct{}
func (srv Service) GetName() string { return "ctrl.Service" }
func (srv Service) GetDependencies() []string { return []string{"server.Service", "auth.Auth"} }
func (srv Service) IsService() bool { return true }
func (srv Service) Init() error { return nil }
func (srv Service) Main() error { return nil }
func (srv Service) Del() {}
func (srv Service) Health() error { return nil }
func (srv Service) Load() error {
ctlAPI := server.API.Group("ctrl")
ctlAPI.GET("prof", auth.ACAdmin(), func(c *gin.Context) {
server.OK(c, system.GetProfile())
})
ctlAPI.GET("config", auth.ACAdmin(), func(c *gin.Context) {
conf, err := system.ConfigShow("json")
if err != nil {
panic(err)
}
data := map[string]interface{}{}
if err := json.Unmarshal([]byte(conf), &data); err != nil {
panic(err)
}
server.OK(c, data)
})
ctlAPI.POST("test_email", auth.ACAdmin(), func(c *gin.Context) {
tos := c.Query("to")
if tos == "" {
panic(errors.ErrorBadRequest)
}
to := strings.Split(tos, ",")
if err := email.Send(viper.GetString("email.username"), to, "Test Email", &email.Body{
Greeting: "Dear",
Receiver: "Administrator",
Intros: []string{
"This is a test email.",
},
}); err != nil {
panic(err)
}
server.OK(c, "email sent.")
})
// this should be in task service
ctlAPI.GET("tasks", auth.ACAdmin(), func(c *gin.Context) {
server.OK(c, task.GetProfile())
})
ctlAPI.GET("task", auth.ACAdmin(), func(c *gin.Context) {
server.OK(c, task.GetTasks())
})
return nil
}