From 6e5ee1617854c2d53d410bcc118e731590849e35 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Wed, 18 Nov 2020 22:54:45 +0100 Subject: [PATCH] updated vmail --- go.mod | 1 + go.sum | 28 +++++++++- src/api/mailbox.go | 1 + src/models/alias.go | 36 ++++++++++++- src/models/aliasdomain.go | 1 + src/models/config.go | 3 +- src/models/domain.go | 1 + src/models/domainadmins.go | 1 + src/models/fetchmail.go | 3 +- src/models/mailbox.go | 1 + src/models/quota.go | 1 + src/models/quota2.go | 1 + src/models/vacation.go | 1 + src/models/vacationnotification.go | 1 + src/routers/funcs.go | 87 ++++++++++++++++++++++++++++++ src/routers/main.go | 47 +--------------- vendor/modules.txt | 2 + 17 files changed, 165 insertions(+), 51 deletions(-) create mode 100644 src/routers/funcs.go diff --git a/go.mod b/go.mod index 8d593ac..a3cc9eb 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.14 require ( github.com/labstack/echo/v4 v4.1.16 github.com/lib/pq v1.8.0 + github.com/smartystreets/goconvey v1.6.4 // indirect gopkg.in/ini.v1 v1.57.0 xorm.io/xorm v1.0.3 ) diff --git a/go.sum b/go.sum index 115ff6c..24e9712 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,27 @@ +gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:lSA0F4e9A2NcQSqGqTOXqu2aRi/XEQxDCBwM8yJtE6s= gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:EXuID2Zs0pAQhH8yz+DNjUbjppKQzKFAn28TMYPB6IU= github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= +github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/labstack/echo v1.4.4 h1:1bEiBNeGSUKxcPDGfZ/7IgdhJJZx8wV/pICJh4W2NJI= -github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/labstack/echo/v4 v4.1.16 h1:8swiwjE5Jkai3RPfZoahp8kjVCRNq+y7Q0hPji2Kz0o= github.com/labstack/echo/v4 v4.1.16/go.mod h1:awO+5TzAjvL8XpibdsfXxPgHr+orhtXZJZIQCVjogKI= github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= @@ -28,13 +36,22 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= @@ -50,11 +67,13 @@ golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAak golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -69,12 +88,17 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/ini.v1 v1.57.0 h1:9unxIsFcTt4I55uWluz+UmL95q4kdJ0buvQ1ZIqVQww= gopkg.in/ini.v1 v1.57.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= xorm.io/builder v0.3.7 h1:2pETdKRK+2QG4mLX4oODHEhn5Z8j1m8sXa7jfu+/SZI= xorm.io/builder v0.3.7/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE= diff --git a/src/api/mailbox.go b/src/api/mailbox.go index 8ed77a8..f2f77e7 100644 --- a/src/api/mailbox.go +++ b/src/api/mailbox.go @@ -3,6 +3,7 @@ package api // Mailbox defines the admin struct type Mailbox struct { Username string `json:"username"` + Password string `json:"password"` Name string `json:"name"` Maildir string `json:"maildir"` Quota string `json:"quota"` diff --git a/src/models/alias.go b/src/models/alias.go index 46d1885..d90f5b4 100644 --- a/src/models/alias.go +++ b/src/models/alias.go @@ -2,12 +2,15 @@ package models import ( "context" + "fmt" "time" "git.paulbsd.com/paulbsd/vmail/src/api" "git.paulbsd.com/paulbsd/vmail/src/config" ) +const key = "address" + // GetAliases ... func GetAliases(ctx *context.Context, config *config.Config) (apialiases []*api.Alias, err error) { var aliases []Alias @@ -21,7 +24,7 @@ func GetAliases(ctx *context.Context, config *config.Config) (apialiases []*api. // GetAlias ... func GetAlias(ctx *context.Context, config *config.Config, id interface{}) (apialias *api.Alias, err error) { var alias Alias - has, err := config.Db.Where("username = ?", id).Get(&alias) + has, err := config.Db.Where(fmt.Sprintf("%s = ?", key), id).Get(&alias) if !has || err != nil { return } @@ -29,6 +32,37 @@ func GetAlias(ctx *context.Context, config *config.Config, id interface{}) (apia return } +// CreateAlias ... +func CreateAlias(ctx *context.Context, config *config.Config) (num int64, err error) { + var alias Alias + num, err = config.Db.Insert(&alias) + if err != nil { + return + } + return +} + +// UpdateAlias ... +func UpdateAlias(ctx *context.Context, config *config.Config, id interface{}) (apialias *api.Alias, err error) { + var alias Alias + has, err := config.Db.Where(fmt.Sprintf("%s = ?", key), id).Get(&alias) + if !has || err != nil { + return + } + apialias = alias.APIFormat() + return +} + +// DeleteAlias ... +func DeleteAlias(ctx *context.Context, config *config.Config, id interface{}) (num int64, err error) { + var alias Alias + num, err = config.Db.Where(fmt.Sprintf("%s = ?", key), id).Delete(&alias) + if num > 0 || err != nil { + return + } + return +} + // APIFormat returns a JSON formatted object of Admin func (alias *Alias) APIFormat() *api.Alias { if alias == nil { diff --git a/src/models/aliasdomain.go b/src/models/aliasdomain.go index 4d50707..2b12234 100644 --- a/src/models/aliasdomain.go +++ b/src/models/aliasdomain.go @@ -2,6 +2,7 @@ package models import "time" +// AliasDomain defines domain aliases type AliasDomain struct { AliasDomain string `xorm:"not null index(alias_domain_active) VARCHAR(255)"` TargetDomain string `xorm:"not null VARCHAR(255)"` diff --git a/src/models/config.go b/src/models/config.go index 41488dc..f6045e0 100644 --- a/src/models/config.go +++ b/src/models/config.go @@ -1,7 +1,8 @@ package models +// Config defines config table type Config struct { - Id int `xorm:"not null pk autoincr INTEGER"` + ID int `xorm:"not null pk autoincr INTEGER"` Name string `xorm:"not null unique VARCHAR(20)"` Value string `xorm:"not null VARCHAR(20)"` } diff --git a/src/models/domain.go b/src/models/domain.go index 99bb37b..3c48a48 100644 --- a/src/models/domain.go +++ b/src/models/domain.go @@ -2,6 +2,7 @@ package models import "time" +// Domain defines domain table type Domain struct { Domain string `xorm:"not null pk index(domain_domain_active) unique VARCHAR(255)"` Description string `xorm:"not null default '' VARCHAR(255)"` diff --git a/src/models/domainadmins.go b/src/models/domainadmins.go index dd92b29..408fcbe 100644 --- a/src/models/domainadmins.go +++ b/src/models/domainadmins.go @@ -2,6 +2,7 @@ package models import "time" +// DomainAdmins defines admin users of domains type DomainAdmins struct { Username string `xorm:"not null VARCHAR(255)"` Domain string `xorm:"not null VARCHAR(255)"` diff --git a/src/models/fetchmail.go b/src/models/fetchmail.go index a996afe..f99a59c 100644 --- a/src/models/fetchmail.go +++ b/src/models/fetchmail.go @@ -2,8 +2,9 @@ package models import "time" +// Fetchmail defines the fetchmail table type Fetchmail struct { - Id int `xorm:"not null pk autoincr INTEGER"` + ID int `xorm:"not null pk autoincr INTEGER"` Mailbox string `xorm:"not null default '' VARCHAR(255)"` SrcServer string `xorm:"not null default '' VARCHAR(255)"` SrcAuth string `xorm:"not null VARCHAR(15)"` diff --git a/src/models/mailbox.go b/src/models/mailbox.go index bf4ef0a..72d9745 100644 --- a/src/models/mailbox.go +++ b/src/models/mailbox.go @@ -36,6 +36,7 @@ func (mailbox *Mailbox) APIFormat() *api.Mailbox { } return &api.Mailbox{ Username: mailbox.Username, + Password: "****", Name: mailbox.Name, Maildir: mailbox.Maildir, Quota: quotaFormat(mailbox.Quota), diff --git a/src/models/quota.go b/src/models/quota.go index accbeeb..8f7ff34 100644 --- a/src/models/quota.go +++ b/src/models/quota.go @@ -1,5 +1,6 @@ package models +// Quota defines the Quota table type Quota struct { Username string `xorm:"not null pk VARCHAR(255)"` Path string `xorm:"not null pk VARCHAR(100)"` diff --git a/src/models/quota2.go b/src/models/quota2.go index 396c10e..f08c71b 100644 --- a/src/models/quota2.go +++ b/src/models/quota2.go @@ -1,5 +1,6 @@ package models +// Quota2 defines the Quota2 table type Quota2 struct { Username string `xorm:"not null pk VARCHAR(100)"` Bytes int64 `xorm:"not null default 0 BIGINT"` diff --git a/src/models/vacation.go b/src/models/vacation.go index 6419b51..dfc80c6 100644 --- a/src/models/vacation.go +++ b/src/models/vacation.go @@ -2,6 +2,7 @@ package models import "time" +// Vacation defines the vacation table type Vacation struct { Email string `xorm:"not null pk index(vacation_email_active) VARCHAR(255)"` Subject string `xorm:"not null VARCHAR(255)"` diff --git a/src/models/vacationnotification.go b/src/models/vacationnotification.go index 72e170f..45e7333 100644 --- a/src/models/vacationnotification.go +++ b/src/models/vacationnotification.go @@ -2,6 +2,7 @@ package models import "time" +//VacationNotification defines the VacationNotification table type VacationNotification struct { OnVacation string `xorm:"not null VARCHAR(255)"` Notified string `xorm:"not null pk VARCHAR(255)"` diff --git a/src/routers/funcs.go b/src/routers/funcs.go new file mode 100644 index 0000000..d837b5b --- /dev/null +++ b/src/routers/funcs.go @@ -0,0 +1,87 @@ +package routers + +import ( + "context" + "net/http" + + "git.paulbsd.com/paulbsd/vmail/src/config" + "git.paulbsd.com/paulbsd/vmail/src/models" + "github.com/labstack/echo/v4" +) + +// RegisterRoutes ... +func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) { + e.GET("/", func(c echo.Context) (err error) { + return c.HTML(http.StatusOK, "Welcome to Vmail API") + }) + e.POST("/auth", func(c echo.Context) (err error) { + return c.String(http.StatusOK, "/auth") + }) + e.GET("/api", func(c echo.Context) (err error) { + return c.JSON(http.StatusOK, "/api") + }) + + // Admins + e.GET("/api/admin", func(c echo.Context) (err error) { + admins, err := models.GetAdmins(ctx, cfg) + return c.JSON(http.StatusOK, admins) + }) + e.GET("/api/admin/:id", func(c echo.Context) (err error) { + admins, err := models.GetAdmins(ctx, cfg) + return c.JSON(http.StatusOK, admins) + }) + e.DELETE("/api/admin/:id", func(c echo.Context) (err error) { + admins, err := models.GetAdmins(ctx, cfg) + return c.JSON(http.StatusOK, admins) + }) + + // Aliases + e.GET("/api/alias", func(c echo.Context) (err error) { + aliases, err := models.GetAliases(ctx, cfg) + return c.JSON(http.StatusOK, aliases) + }) + e.GET("/api/alias/:id", func(c echo.Context) (err error) { + alias, err := models.GetAlias(ctx, cfg, c.Param("id")) + return JSONResult(c, err, alias) + }) + e.POST("/api/alias", func(c echo.Context) (err error) { + _, err = models.CreateAlias(ctx, cfg) + return c.JSON(http.StatusOK, nil) + }) + e.PUT("/api/alias/:id", func(c echo.Context) (err error) { + aliases, err := models.UpdateAlias(ctx, cfg, c.Param("id")) + return c.JSON(http.StatusOK, aliases) + }) + e.DELETE("/api/alias/:id", func(c echo.Context) (err error) { + aliases, err := models.DeleteAlias(ctx, cfg, c.Param("id")) + return c.JSON(http.StatusOK, aliases) + }) + + // Mailboxes + e.GET("/api/mailbox", func(c echo.Context) (err error) { + mailboxes, err := models.GetMailboxes(ctx, cfg) + return c.JSON(http.StatusOK, mailboxes) + }) + e.GET("/api/mailbox/:id", func(c echo.Context) (err error) { + mailbox, err := models.GetMailbox(ctx, cfg, c.Param("id")) + return c.JSON(http.StatusOK, mailbox) + }) + e.DELETE("/api/mailbox/:id", func(c echo.Context) (err error) { + mailbox, err := models.GetMailbox(ctx, cfg, c.Param("id")) + return c.JSON(http.StatusOK, mailbox) + }) + + // Logs + e.GET("/api/log", func(c echo.Context) (err error) { + logs, err := models.GetLogs(ctx, cfg) + return c.JSON(http.StatusOK, logs) + }) +} + +// JSONResult ... +func JSONResult(c echo.Context, inputerr error, data interface{}) (err error) { + if inputerr != nil { + return c.JSON(http.StatusInternalServerError, nil) + } + return c.JSON(http.StatusOK, data) +} diff --git a/src/routers/main.go b/src/routers/main.go index d330839..1034ba5 100644 --- a/src/routers/main.go +++ b/src/routers/main.go @@ -6,7 +6,6 @@ import ( "net/http" "git.paulbsd.com/paulbsd/vmail/src/config" - "git.paulbsd.com/paulbsd/vmail/src/models" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -22,51 +21,7 @@ func RunServer(ctx *context.Context, cfg *config.Config) (err error) { AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete}, })) - e.GET("/", func(c echo.Context) (err error) { - return c.HTML(http.StatusOK, "Welcome to Vmail API") - }) - e.POST("/auth", func(c echo.Context) (err error) { - return c.String(http.StatusOK, "/auth") - }) - e.GET("/api", func(c echo.Context) (err error) { - return c.JSON(http.StatusOK, "/api") - }) - - // Admins - e.GET("/api/admin", func(c echo.Context) (err error) { - admins, err := models.GetAdmins(ctx, cfg) - return c.JSON(http.StatusOK, admins) - }) - e.GET("/api/admin/:id", func(c echo.Context) (err error) { - admins, err := models.GetAdmins(ctx, cfg) - return c.JSON(http.StatusOK, admins) - }) - - // Aliases - e.GET("/api/alias", func(c echo.Context) (err error) { - aliases, err := models.GetAliases(ctx, cfg) - return c.JSON(http.StatusOK, aliases) - }) - e.GET("/api/alias/:id", func(c echo.Context) (err error) { - aliases, err := models.GetAliases(ctx, cfg) - return c.JSON(http.StatusOK, aliases) - }) - - // Mailboxes - e.GET("/api/mailbox", func(c echo.Context) (err error) { - mailboxes, err := models.GetMailboxes(ctx, cfg) - return c.JSON(http.StatusOK, mailboxes) - }) - e.GET("/api/mailbox/:id", func(c echo.Context) (err error) { - mailbox, err := models.GetMailbox(ctx, cfg, c.Param("id")) - return c.JSON(http.StatusOK, mailbox) - }) - - // Logs - e.GET("/api/log", func(c echo.Context) (err error) { - logs, err := models.GetLogs(ctx, cfg) - return c.JSON(http.StatusOK, logs) - }) + RegisterRoutes(e, ctx, cfg) e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", cfg.Port))) return diff --git a/vendor/modules.txt b/vendor/modules.txt index d06ff24..8203441 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -20,6 +20,8 @@ github.com/lib/pq/scram github.com/mattn/go-colorable # github.com/mattn/go-isatty v0.0.12 github.com/mattn/go-isatty +# github.com/smartystreets/goconvey v1.6.4 +## explicit # github.com/syndtr/goleveldb v1.0.0 github.com/syndtr/goleveldb/leveldb github.com/syndtr/goleveldb/leveldb/cache