POST:/users works
This commit is contained in:
@@ -5,11 +5,15 @@ type APIError int
|
||||
const (
|
||||
NO_ERROR APIError = 0000
|
||||
|
||||
MISSING_UID APIError = 1101
|
||||
MISSING_TOK APIError = 1102
|
||||
MISSING_TITLE APIError = 1103
|
||||
INVALID_PRIO APIError = 1104
|
||||
REQ_METHOD APIError = 1105
|
||||
MISSING_UID APIError = 1101
|
||||
MISSING_TOK APIError = 1102
|
||||
MISSING_TITLE APIError = 1103
|
||||
INVALID_PRIO APIError = 1104
|
||||
REQ_METHOD APIError = 1105
|
||||
INVALID_CLIENTTYPE APIError = 1106
|
||||
MISSING_QUERY_PARAM APIError = 1151
|
||||
MISSING_BODY_PARAM APIError = 1152
|
||||
MISSING_URI_PARAM APIError = 1153
|
||||
|
||||
NO_TITLE APIError = 1201
|
||||
TITLE_TOO_LONG APIError = 1202
|
||||
@@ -24,6 +28,12 @@ const (
|
||||
|
||||
QUOTA_REACHED APIError = 2101
|
||||
|
||||
FAILED_VERIFY_PRO_TOKEN APIError = 3001
|
||||
INVALID_PRO_TOKEN APIError = 3002
|
||||
|
||||
COMMIT_FAILED = 9001
|
||||
DATABASE_ERROR = 9002
|
||||
|
||||
FIREBASE_COM_FAILED APIError = 9901
|
||||
FIREBASE_COM_ERRORED APIError = 9902
|
||||
INTERNAL_EXCEPTION APIError = 9903
|
||||
|
||||
167
server/api/handler/api.go
Normal file
167
server/api/handler/api.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/models"
|
||||
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type APIHandler struct {
|
||||
app *logic.Application
|
||||
}
|
||||
|
||||
// CreateUser swaggerdoc
|
||||
//
|
||||
// @Summary Create a new user
|
||||
// @ID api-user-create
|
||||
//
|
||||
// @Param post_body body handler.CreateUser.body false " "
|
||||
//
|
||||
// @Success 200 {object} models.UserJSON
|
||||
// @Failure 400 {object} ginresp.apiError
|
||||
// @Failure 500 {object} ginresp.apiError
|
||||
//
|
||||
// @Router /api-v2/user/ [POST]
|
||||
func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
|
||||
type body struct {
|
||||
FCMToken string `form:"fcm_token"`
|
||||
ProToken *string `form:"pro_token"`
|
||||
Username *string `form:"username"`
|
||||
AgentModel string `form:"agent_model"`
|
||||
AgentVersion string `form:"agent_version"`
|
||||
ClientType string `form:"client_type"`
|
||||
}
|
||||
|
||||
ctx := h.app.StartRequest(g)
|
||||
defer ctx.Cancel()
|
||||
|
||||
var b body
|
||||
if err := g.ShouldBindJSON(&b); err != nil {
|
||||
return ginresp.InternAPIError(apierr.MISSING_BODY_PARAM, "Failed to read body", err)
|
||||
}
|
||||
|
||||
var clientType models.ClientType
|
||||
if b.ClientType == string(models.ClientTypeAndroid) {
|
||||
clientType = models.ClientTypeAndroid
|
||||
} else if b.ClientType == string(models.ClientTypeIOS) {
|
||||
clientType = models.ClientTypeIOS
|
||||
} else {
|
||||
return ginresp.InternAPIError(apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
|
||||
}
|
||||
|
||||
if b.ProToken != nil {
|
||||
ptok, err := h.app.VerifyProToken(*b.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
|
||||
}
|
||||
|
||||
if !ptok {
|
||||
return ginresp.InternAPIError(apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
|
||||
}
|
||||
}
|
||||
|
||||
readKey := h.app.GenerateRandomAuthKey()
|
||||
sendKey := h.app.GenerateRandomAuthKey()
|
||||
adminKey := h.app.GenerateRandomAuthKey()
|
||||
|
||||
err := h.app.Database.ClearFCMTokens(ctx, b.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
||||
}
|
||||
|
||||
if b.ProToken != nil {
|
||||
err := h.app.Database.ClearProTokens(ctx, b.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
||||
}
|
||||
}
|
||||
|
||||
userobj, err := h.app.Database.CreateUser(ctx, readKey, sendKey, adminKey, b.ProToken, b.Username)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(apierr.DATABASE_ERROR, "Failed to create user in db", err)
|
||||
}
|
||||
|
||||
_, err = h.app.Database.CreateClient(ctx, userobj.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(apierr.DATABASE_ERROR, "Failed to create user in db", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, userobj.JSON()))
|
||||
}
|
||||
|
||||
func (h APIHandler) GetUser(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) UpdateUser(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) GetClient(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) DeleteClient(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) ListChannels(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) GetChannel(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) GetChannelMessages(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) ListUserSubscriptions(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) ListChannelSubscriptions(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) GetSubscription(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) CancelSubscription(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) CreateSubscription(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) ListMessages(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) GetMessage(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func (h APIHandler) DeleteMessage(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func NewAPIHandler(app *logic.Application) APIHandler {
|
||||
return APIHandler{
|
||||
app: app,
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ type pingResponseInfo struct {
|
||||
// Ping swaggerdoc
|
||||
//
|
||||
// @Success 200 {object} pingResponse
|
||||
// @Failure 500 {object} ginresp.errBody
|
||||
// @Failure 500 {object} ginresp.apiError
|
||||
// @Router /ping [get]
|
||||
// @Router /ping [post]
|
||||
// @Router /ping [put]
|
||||
@@ -60,7 +60,7 @@ func (h CommonHandler) Ping(g *gin.Context) ginresp.HTTPResponse {
|
||||
// DatabaseTest swaggerdoc
|
||||
//
|
||||
// @Success 200 {object} handler.DatabaseTest.response
|
||||
// @Failure 500 {object} ginresp.errBody
|
||||
// @Failure 500 {object} ginresp.apiError
|
||||
// @Router /db-test [get]
|
||||
func (h CommonHandler) DatabaseTest(g *gin.Context) ginresp.HTTPResponse {
|
||||
type response struct {
|
||||
@@ -88,7 +88,7 @@ func (h CommonHandler) DatabaseTest(g *gin.Context) ginresp.HTTPResponse {
|
||||
// Health swaggerdoc
|
||||
//
|
||||
// @Success 200 {object} handler.Health.response
|
||||
// @Failure 500 {object} ginresp.errBody
|
||||
// @Failure 500 {object} ginresp.apiError
|
||||
// @Router /health [get]
|
||||
func (h CommonHandler) Health(*gin.Context) ginresp.HTTPResponse {
|
||||
type response struct {
|
||||
@@ -96,3 +96,14 @@ func (h CommonHandler) Health(*gin.Context) ginresp.HTTPResponse {
|
||||
}
|
||||
return ginresp.JSON(http.StatusOK, response{Status: "ok"})
|
||||
}
|
||||
|
||||
func (h CommonHandler) NoRoute(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.JSON(http.StatusNotFound, gin.H{
|
||||
"FullPath": g.FullPath(),
|
||||
"Method": g.Request.Method,
|
||||
"URL": g.Request.URL.String(),
|
||||
"RequestURI": g.Request.RequestURI,
|
||||
"Proto": g.Request.Proto,
|
||||
"Header": g.Request.Header,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,17 +1,10 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/models"
|
||||
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CompatHandler struct {
|
||||
@@ -32,8 +25,8 @@ func NewCompatHandler(app *logic.Application) CompatHandler {
|
||||
// @Param pro query string true "if the user is a paid account" Enums(true, false)
|
||||
// @Param pro_token query string true "the (android) IAP token"
|
||||
// @Success 200 {object} handler.Register.response
|
||||
// @Failure 500 {object} ginresp.internAPIError
|
||||
// @Router /register.php [get]
|
||||
// @Failure 500 {object} ginresp.apiError
|
||||
// @Router /api/register.php [get]
|
||||
func (h CompatHandler) Register(g *gin.Context) ginresp.HTTPResponse {
|
||||
type query struct {
|
||||
FCMToken *string `form:"fcm_token"`
|
||||
@@ -50,81 +43,9 @@ func (h CompatHandler) Register(g *gin.Context) ginresp.HTTPResponse {
|
||||
IsPro int `json:"is_pro"`
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
//TODO
|
||||
|
||||
var q query
|
||||
if err := g.ShouldBindQuery(&q); err != nil {
|
||||
return ginresp.InternAPIError(0, "Failed to read arguments")
|
||||
}
|
||||
|
||||
if q.FCMToken == nil {
|
||||
return ginresp.InternAPIError(0, "Missing parameter [[fcm_token]]")
|
||||
}
|
||||
if q.Pro == nil {
|
||||
return ginresp.InternAPIError(0, "Missing parameter [[pro]]")
|
||||
}
|
||||
if q.ProToken == nil {
|
||||
return ginresp.InternAPIError(0, "Missing parameter [[pro_token]]")
|
||||
}
|
||||
|
||||
isProInt := 0
|
||||
isProBool := false
|
||||
if *q.Pro == "true" {
|
||||
isProInt = 1
|
||||
isProBool = true
|
||||
} else {
|
||||
q.ProToken = nil
|
||||
}
|
||||
|
||||
if isProBool {
|
||||
ptok, err := h.app.VerifyProToken(*q.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to query purchaste status: %v", err))
|
||||
}
|
||||
|
||||
if !ptok {
|
||||
return ginresp.InternAPIError(0, "Purchase token could not be verified")
|
||||
}
|
||||
}
|
||||
|
||||
userKey := h.app.GenerateRandomAuthKey()
|
||||
|
||||
return h.app.RunTransaction(ctx, nil, func(tx *sql.Tx) (ginresp.HTTPResponse, bool) {
|
||||
|
||||
res, err := tx.ExecContext(ctx, "INSERT INTO users (user_key, fcm_token, is_pro, pro_token, timestamp_accessed) VALUES (?, ?, ?, ?, NOW())", userKey, *q.FCMToken, isProInt, q.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to create user: %v", err)), false
|
||||
}
|
||||
|
||||
userId, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to get user_id: %v", err)), false
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, "UPDATE users SET fcm_token=NULL WHERE user_id <> ? AND fcm_token=?", userId, q.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to update fcm: %v", err)), false
|
||||
}
|
||||
|
||||
if isProInt == 1 {
|
||||
_, err := tx.ExecContext(ctx, "UPDATE users SET is_pro=0, pro_token=NULL WHERE user_id <> ? AND pro_token = ?", userId, q.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to update ispro: %v", err)), false
|
||||
}
|
||||
}
|
||||
|
||||
return ginresp.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
Message: "New user registered",
|
||||
UserID: strconv.FormatInt(userId, 10),
|
||||
UserKey: userKey,
|
||||
QuotaUsed: 0,
|
||||
QuotaMax: h.app.QuotaMax(isProBool),
|
||||
IsPro: isProInt,
|
||||
}), true
|
||||
|
||||
})
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
// Info swaggerdoc
|
||||
@@ -134,8 +55,8 @@ func (h CompatHandler) Register(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param user_id query string true "the user_id"
|
||||
// @Param user_key query string true "the user_key"
|
||||
// @Success 200 {object} handler.Info.response
|
||||
// @Failure 500 {object} ginresp.internAPIError
|
||||
// @Router /info.php [get]
|
||||
// @Failure 500 {object} ginresp.apiError
|
||||
// @Router /api/info.php [get]
|
||||
func (h CompatHandler) Info(g *gin.Context) ginresp.HTTPResponse {
|
||||
type query struct {
|
||||
UserID string `form:"user_id"`
|
||||
@@ -155,7 +76,7 @@ func (h CompatHandler) Info(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
// Ack swaggerdoc
|
||||
@@ -166,8 +87,8 @@ func (h CompatHandler) Info(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param user_key query string true "the user_key"
|
||||
// @Param scn_msg_id query string true "the message id"
|
||||
// @Success 200 {object} handler.Ack.response
|
||||
// @Failure 500 {object} ginresp.internAPIError
|
||||
// @Router /ack.php [get]
|
||||
// @Failure 500 {object} ginresp.apiError
|
||||
// @Router /api/ack.php [get]
|
||||
func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
|
||||
type query struct {
|
||||
UserID string `form:"user_id"`
|
||||
@@ -183,7 +104,7 @@ func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
// Requery swaggerdoc
|
||||
@@ -193,8 +114,8 @@ func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param user_id query string true "the user_id"
|
||||
// @Param user_key query string true "the user_key"
|
||||
// @Success 200 {object} handler.Requery.response
|
||||
// @Failure 500 {object} ginresp.internAPIError
|
||||
// @Router /requery.php [get]
|
||||
// @Failure 500 {object} ginresp.apiError
|
||||
// @Router /api/requery.php [get]
|
||||
func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
|
||||
type query struct {
|
||||
UserID string `form:"user_id"`
|
||||
@@ -209,7 +130,7 @@ func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
// Update swaggerdoc
|
||||
@@ -220,8 +141,8 @@ func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param user_key query string true "the user_key"
|
||||
// @Param fcm_token query string true "the (android) fcm token"
|
||||
// @Success 200 {object} handler.Update.response
|
||||
// @Failure 500 {object} ginresp.internAPIError
|
||||
// @Router /update.php [get]
|
||||
// @Failure 500 {object} ginresp.apiError
|
||||
// @Router /api/update.php [get]
|
||||
func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
|
||||
type query struct {
|
||||
UserID string `form:"user_id"`
|
||||
@@ -240,7 +161,7 @@ func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
// Expand swaggerdoc
|
||||
@@ -248,8 +169,8 @@ func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Summary Get a whole (potentially truncated) message
|
||||
// @ID compat-expand
|
||||
// @Success 200 {object} handler.Expand.response
|
||||
// @Failure 500 {object} ginresp.internAPIError
|
||||
// @Router /expand.php [get]
|
||||
// @Failure 500 {object} ginresp.apiError
|
||||
// @Router /api/expand.php [get]
|
||||
func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
|
||||
type query struct {
|
||||
UserID string `form:"user_id"`
|
||||
@@ -264,7 +185,7 @@ func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
// Upgrade swaggerdoc
|
||||
@@ -276,8 +197,8 @@ func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param pro query string true "if the user is a paid account" Enums(true, false)
|
||||
// @Param pro_token query string true "the (android) IAP token"
|
||||
// @Success 200 {object} handler.Upgrade.response
|
||||
// @Failure 500 {object} ginresp.internAPIError
|
||||
// @Router /upgrade.php [get]
|
||||
// @Failure 500 {object} ginresp.apiError
|
||||
// @Router /api/upgrade.php [get]
|
||||
func (h CompatHandler) Upgrade(g *gin.Context) ginresp.HTTPResponse {
|
||||
type query struct {
|
||||
UserID string `form:"user_id"`
|
||||
@@ -293,47 +214,5 @@ func (h CompatHandler) Upgrade(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
}
|
||||
|
||||
// Send swaggerdoc
|
||||
//
|
||||
// @Summary Send a message
|
||||
// @Description (all arguments can either be supplied in the query or in the json body)
|
||||
// @ID compat-send
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param _ query handler.Send.query false " "
|
||||
// @Param post_body body handler.Send.body false " "
|
||||
// @Success 200 {object} handler.Send.response
|
||||
// @Failure 500 {object} ginresp.sendAPIError
|
||||
// @Router /send.php [post]
|
||||
func (h CompatHandler) Send(g *gin.Context) ginresp.HTTPResponse {
|
||||
type query struct {
|
||||
UserID string `form:"user_id" required:"true"`
|
||||
UserKey string `form:"user_key" required:"true"`
|
||||
Title string `form:"title" required:"true"`
|
||||
Content *string `form:"content"`
|
||||
Priority *string `form:"priority"`
|
||||
MessageID *string `form:"msg_id"`
|
||||
Timestamp *string `form:"timestamp"`
|
||||
}
|
||||
type body struct {
|
||||
UserID string `json:"user_id" required:"true"`
|
||||
UserKey string `json:"user_key" required:"true"`
|
||||
Title string `json:"title" required:"true"`
|
||||
Content *string `json:"content"`
|
||||
Priority *string `json:"priority"`
|
||||
MessageID *string `json:"msg_id"`
|
||||
Timestamp *string `json:"timestamp"`
|
||||
}
|
||||
type response struct {
|
||||
Success string `json:"success"`
|
||||
Message string `json:"message"`
|
||||
//TODO
|
||||
}
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.SendAPIError(apierr.INTERNAL_EXCEPTION, -1, "NotImplemented")
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
21
server/api/handler/message.go
Normal file
21
server/api/handler/message.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type MessageHandler struct {
|
||||
app *logic.Application
|
||||
}
|
||||
|
||||
func (h MessageHandler) SendMessage(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.NotImplemented()
|
||||
}
|
||||
|
||||
func NewMessageHandler(app *logic.Application) MessageHandler {
|
||||
return MessageHandler{
|
||||
app: app,
|
||||
}
|
||||
}
|
||||
98
server/api/handler/website.go
Normal file
98
server/api/handler/website.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"blackforestbytes.com/simplecloudnotifier/website"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type WebsiteHandler struct {
|
||||
app *logic.Application
|
||||
}
|
||||
|
||||
func NewWebsiteHandler(app *logic.Application) WebsiteHandler {
|
||||
return WebsiteHandler{
|
||||
app: app,
|
||||
}
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) Index(g *gin.Context) ginresp.HTTPResponse {
|
||||
return h.serveAsset(g, "index.html")
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) APIDocs(g *gin.Context) ginresp.HTTPResponse {
|
||||
return h.serveAsset(g, "api.html")
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) APIDocsMore(g *gin.Context) ginresp.HTTPResponse {
|
||||
return h.serveAsset(g, "api_more.html")
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) MessageSent(g *gin.Context) ginresp.HTTPResponse {
|
||||
return h.serveAsset(g, "message_sent.html")
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) FaviconIco(g *gin.Context) ginresp.HTTPResponse {
|
||||
return h.serveAsset(g, "favicon.ico")
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) FaviconPNG(g *gin.Context) ginresp.HTTPResponse {
|
||||
return h.serveAsset(g, "favicon.png")
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) Javascript(g *gin.Context) ginresp.HTTPResponse {
|
||||
type uri struct {
|
||||
Filename string `uri:"fn"`
|
||||
}
|
||||
|
||||
var u uri
|
||||
if err := g.ShouldBindUri(&u); err != nil {
|
||||
return ginresp.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
return h.serveAsset(g, "js/"+u.Filename)
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) CSS(g *gin.Context) ginresp.HTTPResponse {
|
||||
type uri struct {
|
||||
Filename string `uri:"fn"`
|
||||
}
|
||||
|
||||
var u uri
|
||||
if err := g.ShouldBindUri(&u); err != nil {
|
||||
return ginresp.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
return h.serveAsset(g, "css/"+u.Filename)
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) serveAsset(g *gin.Context, fn string) ginresp.HTTPResponse {
|
||||
data, err := website.Assets.ReadFile(fn)
|
||||
if err != nil {
|
||||
return ginresp.Status(http.StatusNotFound)
|
||||
}
|
||||
|
||||
mime := "text/plain"
|
||||
|
||||
lowerFN := strings.ToLower(fn)
|
||||
if strings.HasSuffix(lowerFN, ".html") || strings.HasSuffix(lowerFN, ".htm") {
|
||||
mime = "text/html"
|
||||
} else if strings.HasSuffix(lowerFN, ".css") {
|
||||
mime = "text/css"
|
||||
} else if strings.HasSuffix(lowerFN, ".js") {
|
||||
mime = "text/javascript"
|
||||
} else if strings.HasSuffix(lowerFN, ".json") {
|
||||
mime = "application/json"
|
||||
} else if strings.HasSuffix(lowerFN, ".jpeg") || strings.HasSuffix(lowerFN, ".jpg") {
|
||||
mime = "image/jpeg"
|
||||
} else if strings.HasSuffix(lowerFN, ".png") {
|
||||
mime = "image/png"
|
||||
} else if strings.HasSuffix(lowerFN, ".svg") {
|
||||
mime = "image/svg+xml"
|
||||
}
|
||||
|
||||
return ginresp.Data(http.StatusOK, mime, data)
|
||||
}
|
||||
23
server/api/models/client.go
Normal file
23
server/api/models/client.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type ClientType string
|
||||
|
||||
const (
|
||||
ClientTypeAndroid ClientType = "ANDROID"
|
||||
ClientTypeIOS ClientType = "IOS"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
ClientID int64
|
||||
UserID int64
|
||||
Type ClientType
|
||||
FCMToken *string
|
||||
TimestampCreated time.Time
|
||||
AgentModel string
|
||||
AgentVersion string
|
||||
}
|
||||
|
||||
type ClientJSON struct {
|
||||
}
|
||||
51
server/api/models/user.go
Normal file
51
server/api/models/user.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type User struct {
|
||||
UserID int64
|
||||
Username *string
|
||||
ReadKey string
|
||||
SendKey string
|
||||
AdminKey string
|
||||
TimestampCreated time.Time
|
||||
TimestampLastRead *time.Time
|
||||
TimestampLastSent *time.Time
|
||||
MessagesSent int
|
||||
QuotaToday int
|
||||
QuotaDay *string
|
||||
IsPro bool
|
||||
ProToken *string
|
||||
}
|
||||
|
||||
func (u User) JSON() UserJSON {
|
||||
return UserJSON{
|
||||
UserID: u.UserID,
|
||||
Username: u.Username,
|
||||
ReadKey: u.ReadKey,
|
||||
SendKey: u.SendKey,
|
||||
AdminKey: u.AdminKey,
|
||||
TimestampCreated: u.TimestampCreated.Format(time.RFC3339Nano),
|
||||
TimestampLastRead: timeOptFmt(u.TimestampLastRead, time.RFC3339Nano),
|
||||
TimestampLastSent: timeOptFmt(u.TimestampLastSent, time.RFC3339Nano),
|
||||
MessagesSent: u.MessagesSent,
|
||||
QuotaToday: u.QuotaToday,
|
||||
QuotaDay: u.QuotaDay,
|
||||
IsPro: u.IsPro,
|
||||
}
|
||||
}
|
||||
|
||||
type UserJSON struct {
|
||||
UserID int64 `json:"user_id"`
|
||||
Username *string `json:"username"`
|
||||
ReadKey string `json:"read_key"`
|
||||
SendKey string `json:"send_key"`
|
||||
AdminKey string `json:"admin_key"`
|
||||
TimestampCreated string `json:"timestamp_created"`
|
||||
TimestampLastRead *string `json:"timestamp_last_read"`
|
||||
TimestampLastSent *string `json:"timestamp_last_sent"`
|
||||
MessagesSent int `json:"messages_sent"`
|
||||
QuotaToday int `json:"quota_today"`
|
||||
QuotaDay *string `json:"quota_day"`
|
||||
IsPro bool `json:"is_pro"`
|
||||
}
|
||||
14
server/api/models/utils.go
Normal file
14
server/api/models/utils.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"time"
|
||||
)
|
||||
|
||||
func timeOptFmt(t *time.Time, fmt string) *string {
|
||||
if t == nil {
|
||||
return nil
|
||||
} else {
|
||||
return langext.Ptr(t.Format(fmt))
|
||||
}
|
||||
}
|
||||
@@ -12,16 +12,22 @@ import (
|
||||
type Router struct {
|
||||
app *logic.Application
|
||||
|
||||
commonHandler handler.CommonHandler
|
||||
compatHandler handler.CompatHandler
|
||||
commonHandler handler.CommonHandler
|
||||
compatHandler handler.CompatHandler
|
||||
websiteHandler handler.WebsiteHandler
|
||||
apiHandler handler.APIHandler
|
||||
messageHandler handler.MessageHandler
|
||||
}
|
||||
|
||||
func NewRouter(app *logic.Application) *Router {
|
||||
return &Router{
|
||||
app: app,
|
||||
|
||||
commonHandler: handler.NewCommonHandler(app),
|
||||
compatHandler: handler.NewCompatHandler(app),
|
||||
commonHandler: handler.NewCommonHandler(app),
|
||||
compatHandler: handler.NewCompatHandler(app),
|
||||
websiteHandler: handler.NewWebsiteHandler(app),
|
||||
apiHandler: handler.NewAPIHandler(app),
|
||||
messageHandler: handler.NewMessageHandler(app),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,23 +36,101 @@ func NewRouter(app *logic.Application) *Router {
|
||||
// @version 2.0
|
||||
// @description API for SCN
|
||||
// @host scn.blackforestbytes.com
|
||||
// @BasePath /api/
|
||||
// @BasePath /
|
||||
func (r *Router) Init(e *gin.Engine) {
|
||||
|
||||
e.Any("/ping", ginresp.Wrap(r.commonHandler.Ping))
|
||||
e.POST("/db-test", ginresp.Wrap(r.commonHandler.DatabaseTest))
|
||||
e.GET("/health", ginresp.Wrap(r.commonHandler.Health))
|
||||
// ================ General ================
|
||||
|
||||
e.GET("documentation/swagger", ginext.RedirectTemporary("/documentation/swagger/"))
|
||||
e.GET("documentation/swagger/", ginresp.Wrap(swagger.Handle))
|
||||
e.GET("documentation/swagger/:fn", ginresp.Wrap(swagger.Handle))
|
||||
e.Any("/api/common/ping", ginresp.Wrap(r.commonHandler.Ping))
|
||||
e.POST("/api/common/db-test", ginresp.Wrap(r.commonHandler.DatabaseTest))
|
||||
e.GET("/api/common/health", ginresp.Wrap(r.commonHandler.Health))
|
||||
|
||||
// ================ Swagger ================
|
||||
|
||||
e.GET("/documentation/swagger", ginext.RedirectTemporary("/documentation/swagger/"))
|
||||
e.GET("/documentation/swagger/", ginresp.Wrap(swagger.Handle))
|
||||
e.GET("/documentation/swagger/:fn", ginresp.Wrap(swagger.Handle))
|
||||
|
||||
// ================ Website ================
|
||||
|
||||
e.GET("/", ginresp.Wrap(r.websiteHandler.Index))
|
||||
e.GET("/index.php", ginresp.Wrap(r.websiteHandler.Index))
|
||||
e.GET("/index.html", ginresp.Wrap(r.websiteHandler.Index))
|
||||
e.GET("/index", ginresp.Wrap(r.websiteHandler.Index))
|
||||
|
||||
e.GET("/api", ginresp.Wrap(r.websiteHandler.APIDocs))
|
||||
e.GET("/api.php", ginresp.Wrap(r.websiteHandler.APIDocs))
|
||||
e.GET("/api.html", ginresp.Wrap(r.websiteHandler.APIDocs))
|
||||
|
||||
e.GET("/api_more", ginresp.Wrap(r.websiteHandler.APIDocsMore))
|
||||
e.GET("/api_more.php", ginresp.Wrap(r.websiteHandler.APIDocsMore))
|
||||
e.GET("/api_more.html", ginresp.Wrap(r.websiteHandler.APIDocsMore))
|
||||
|
||||
e.GET("/message_sent", ginresp.Wrap(r.websiteHandler.MessageSent))
|
||||
e.GET("/message_sent.php", ginresp.Wrap(r.websiteHandler.MessageSent))
|
||||
e.GET("/message_sent.html", ginresp.Wrap(r.websiteHandler.MessageSent))
|
||||
|
||||
e.GET("/favicon.ico", ginresp.Wrap(r.websiteHandler.FaviconIco))
|
||||
e.GET("/favicon.png", ginresp.Wrap(r.websiteHandler.FaviconPNG))
|
||||
|
||||
e.GET("/js/:fn", ginresp.Wrap(r.websiteHandler.Javascript))
|
||||
e.GET("/css/:fn", ginresp.Wrap(r.websiteHandler.CSS))
|
||||
|
||||
// ================ Compat (v1) ================
|
||||
|
||||
compat := e.Group("/api/")
|
||||
{
|
||||
compat.GET("/register.php", ginresp.Wrap(r.compatHandler.Register))
|
||||
compat.GET("/info.php", ginresp.Wrap(r.compatHandler.Info))
|
||||
compat.GET("/ack.php", ginresp.Wrap(r.compatHandler.Ack))
|
||||
compat.GET("/requery.php", ginresp.Wrap(r.compatHandler.Requery))
|
||||
compat.GET("/update.php", ginresp.Wrap(r.compatHandler.Update))
|
||||
compat.GET("/expand.php", ginresp.Wrap(r.compatHandler.Expand))
|
||||
compat.GET("/upgrade.php", ginresp.Wrap(r.compatHandler.Upgrade))
|
||||
}
|
||||
|
||||
// ================ Manage API ================
|
||||
|
||||
apiv2 := e.Group("/api-v2/")
|
||||
{
|
||||
|
||||
apiv2.POST("/user/", ginresp.Wrap(r.apiHandler.CreateUser))
|
||||
apiv2.GET("/user/:uid", ginresp.Wrap(r.apiHandler.GetUser))
|
||||
apiv2.PATCH("/user/:uid", ginresp.Wrap(r.apiHandler.UpdateUser))
|
||||
|
||||
apiv2.GET("/user/:uid/clients", ginresp.Wrap(r.apiHandler.ListClients))
|
||||
apiv2.GET("/user/:uid/clients/:cid", ginresp.Wrap(r.apiHandler.GetClient))
|
||||
apiv2.POST("/user/:uid/clients", ginresp.Wrap(r.apiHandler.AddClient))
|
||||
apiv2.DELETE("/user/:uid/clients", ginresp.Wrap(r.apiHandler.DeleteClient))
|
||||
|
||||
apiv2.GET("/user/:uid/channels", ginresp.Wrap(r.apiHandler.ListChannels))
|
||||
apiv2.GET("/user/:uid/channels/:cid", ginresp.Wrap(r.apiHandler.GetChannel))
|
||||
apiv2.GET("/user/:uid/channels/:cid/messages", ginresp.Wrap(r.apiHandler.GetChannelMessages))
|
||||
apiv2.GET("/user/:uid/channels/:cid/subscriptions", ginresp.Wrap(r.apiHandler.ListChannelSubscriptions))
|
||||
|
||||
apiv2.GET("/user/:uid/subscriptions", ginresp.Wrap(r.apiHandler.ListUserSubscriptions))
|
||||
apiv2.GET("/user/:uid/subscriptions/:sid", ginresp.Wrap(r.apiHandler.GetSubscription))
|
||||
apiv2.DELETE("/user/:uid/subscriptions/:sid", ginresp.Wrap(r.apiHandler.CancelSubscription))
|
||||
apiv2.POST("/user/:uid/subscriptions", ginresp.Wrap(r.apiHandler.CreateSubscription))
|
||||
|
||||
apiv2.GET("/messages", ginresp.Wrap(r.apiHandler.ListMessages))
|
||||
apiv2.GET("/messages/:mid", ginresp.Wrap(r.apiHandler.GetMessage))
|
||||
apiv2.DELETE("/messages/:mid", ginresp.Wrap(r.apiHandler.DeleteMessage))
|
||||
|
||||
apiv2.POST("/messages", ginresp.Wrap(r.messageHandler.SendMessage))
|
||||
}
|
||||
|
||||
// ================ Send API ================
|
||||
|
||||
sendAPI := e.Group("")
|
||||
{
|
||||
sendAPI.POST("/", ginresp.Wrap(r.messageHandler.SendMessage))
|
||||
sendAPI.POST("/send", ginresp.Wrap(r.messageHandler.SendMessage))
|
||||
sendAPI.POST("/send.php")
|
||||
}
|
||||
|
||||
if r.app.Config.ReturnRawErrors {
|
||||
e.NoRoute(ginresp.Wrap(r.commonHandler.NoRoute))
|
||||
}
|
||||
|
||||
e.POST("/send.php", ginresp.Wrap(r.compatHandler.Send))
|
||||
e.GET("/register.php", ginresp.Wrap(r.compatHandler.Register))
|
||||
e.GET("/info.php", ginresp.Wrap(r.compatHandler.Info))
|
||||
e.GET("/ack.php", ginresp.Wrap(r.compatHandler.Ack))
|
||||
e.GET("/requery.php", ginresp.Wrap(r.compatHandler.Requery))
|
||||
e.GET("/update.php", ginresp.Wrap(r.compatHandler.Update))
|
||||
e.GET("/expand.php", ginresp.Wrap(r.compatHandler.Expand))
|
||||
e.GET("/upgrade.php", ginresp.Wrap(r.compatHandler.Upgrade))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user