forked from foxcpp/maddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
88 lines (67 loc) · 1.77 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package maddy
import (
"log"
"net/http"
"os"
"sync"
echo "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/foxcpp/maddy/framework/module"
"github.com/foxcpp/maddy/internal/rest/util/server"
"github.com/foxcpp/maddy/internal/rest/util/middleware/basic_auth"
)
var (
userDb module.PlainUserDB
imapDb module.ManageableStorage
mailboxes Mailboxes
)
func startApi(globals map[string]interface{}, mods []ModInfo, wg *sync.WaitGroup) (err error) {
defer wg.Done()
userDb, err = openUserDB(globals, mods)
if err != nil {
return err
}
defer closeIfNeeded(userDb)
imapDb, mailboxes, err = openStorage(globals, mods)
if err != nil {
return err
}
defer closeIfNeeded(imapDb)
if os.Getenv("ADMIN_EMAIL") == "" || os.Getenv("ADMIN_PASSWORD") == "" {
log.Fatal("ADMIN_EMAIL and ADMIN_PASSWORD environment variables must be set")
}
e := server.New()
NewV1(e)
server.Start(e, &server.Config{
Port: "8080",
ReadTimeoutSeconds: 30,
WriteTimeoutSeconds: 30,
Debug: true,
})
return err
}
func NewV1(e *echo.Echo) {
e.GET("/", healthCheck)
e.GET("/version", version)
v1 := e.Group("/v1")
v1.Use(middleware.BasicAuthWithConfig(middleware.BasicAuthConfig{Validator: basic_auth.AdminBasicAuthValidator}))
users := v1.Group("/users")
{
users.POST("", createUser)
users.GET("", listUsers)
users.GET("/:id", getUser)
users.POST("/:id/password", updateUserPassword)
users.DELETE("/:id", deleteUser)
}
mailboxes := v1.Group("/users/:id/mailboxes")
{
mailboxes.POST("", createImapAccount)
mailboxes.DELETE("", deleteImapAccount)
}
}
func healthCheck(c echo.Context) error {
return c.JSON(http.StatusOK, "OK")
}
func version(c echo.Context) error {
return c.JSON(http.StatusOK, Version)
}