feat(client): add settings clients

This commit is contained in:
hexxa 2021-10-09 16:21:38 +08:00 committed by Hexxa
parent 76cf1274af
commit 83436eac6a
5 changed files with 56 additions and 2 deletions

View file

@ -1,9 +1,12 @@
package client package client
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"github.com/ihexxa/quickshare/src/db/sitestore"
"github.com/ihexxa/quickshare/src/handlers/settings"
"github.com/parnurzeal/gorequest" "github.com/parnurzeal/gorequest"
) )
@ -28,3 +31,26 @@ func (cl *SettingsClient) Health() (*http.Response, string, []error) {
return cl.r.Options(cl.url("/v1/settings/health")). return cl.r.Options(cl.url("/v1/settings/health")).
End() End()
} }
func (cl *SettingsClient) GetClientCfg(token *http.Cookie) (*http.Response, *settings.ClientCfgMsg, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/settings/client")).
AddCookie(token).
End()
mResp := &settings.ClientCfgMsg{}
err := json.Unmarshal([]byte(body), mResp)
if err != nil {
errs = append(errs, err)
return nil, nil, errs
}
return resp, mResp, nil
}
func (cl *SettingsClient) SetClientCfg(cfg *sitestore.ClientConfig, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/settings/client")).
AddCookie(token).
Send(&settings.ClientCfgMsg{
ClientCfg: cfg,
}).
End()
}

View file

@ -14,6 +14,7 @@ const listDirQuery = "dp";
function translateResp(resp: Response<any>): Response<any> { function translateResp(resp: Response<any>): Response<any> {
if (resp.status === 500) { if (resp.status === 500) {
// TODO: replace following with error code
if ( if (
resp.data == null || resp.data == null ||
resp.data === "" || resp.data === "" ||

View file

@ -65,6 +65,17 @@ export interface ListSharingsResp {
sharingDirs: string[]; sharingDirs: string[];
} }
export interface ClientConfig {
siteName: string;
siteDesc: string;
bg: {
url: string;
repeat: string;
position: string;
align: string;
};
}
export interface IUsersClient { export interface IUsersClient {
login: ( login: (
user: string, user: string,
@ -110,6 +121,12 @@ export interface IFilesClient {
generateHash: (filePath: string) => Promise<Response>; generateHash: (filePath: string) => Promise<Response>;
} }
export interface ISettingsClient {
health: () => Promise<Response>;
getClientCfg: () => Promise<Response>;
setClientCfg: (cfg: ClientConfig) => Promise<Response>;
}
export interface Response<T = any> { export interface Response<T = any> {
status: number; status: number;
statusText: string; statusText: string;

View file

@ -12,9 +12,9 @@ import (
"github.com/ihexxa/gocfg" "github.com/ihexxa/gocfg"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"github.com/ihexxa/quickshare/src/db/userstore"
"github.com/ihexxa/quickshare/src/depidx" "github.com/ihexxa/quickshare/src/depidx"
q "github.com/ihexxa/quickshare/src/handlers" q "github.com/ihexxa/quickshare/src/handlers"
"github.com/ihexxa/quickshare/src/db/userstore"
) )
var ( var (
@ -63,6 +63,8 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.AdminRole, "DELETE", "/v1/fs/uploadings"): true, apiRuleCname(userstore.AdminRole, "DELETE", "/v1/fs/uploadings"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/metadata"): true, apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/metadata"): true,
apiRuleCname(userstore.AdminRole, "OPTIONS", "/v1/settings/health"): true, apiRuleCname(userstore.AdminRole, "OPTIONS", "/v1/settings/health"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/settings/client"): true,
apiRuleCname(userstore.AdminRole, "PATCH", "/v1/settings/client"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/captchas/"): true, apiRuleCname(userstore.AdminRole, "GET", "/v1/captchas/"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/captchas/imgs"): true, apiRuleCname(userstore.AdminRole, "GET", "/v1/captchas/imgs"): true,
apiRuleCname(userstore.AdminRole, "POST", "/v1/fs/sharings"): true, apiRuleCname(userstore.AdminRole, "POST", "/v1/fs/sharings"): true,
@ -70,7 +72,6 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings"): true, apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings"): true,
apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings/exist"): true, apiRuleCname(userstore.AdminRole, "GET", "/v1/fs/sharings/exist"): true,
apiRuleCname(userstore.AdminRole, "POST", "/v1/fs/hashes/sha1"): true, apiRuleCname(userstore.AdminRole, "POST", "/v1/fs/hashes/sha1"): true,
// user rules // user rules
apiRuleCname(userstore.UserRole, "GET", "/"): true, apiRuleCname(userstore.UserRole, "GET", "/"): true,
apiRuleCname(userstore.UserRole, "GET", publicPath): true, apiRuleCname(userstore.UserRole, "GET", publicPath): true,
@ -93,6 +94,7 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.UserRole, "DELETE", "/v1/fs/uploadings"): true, apiRuleCname(userstore.UserRole, "DELETE", "/v1/fs/uploadings"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/fs/metadata"): true, apiRuleCname(userstore.UserRole, "GET", "/v1/fs/metadata"): true,
apiRuleCname(userstore.UserRole, "OPTIONS", "/v1/settings/health"): true, apiRuleCname(userstore.UserRole, "OPTIONS", "/v1/settings/health"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/settings/client"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/captchas/"): true, apiRuleCname(userstore.UserRole, "GET", "/v1/captchas/"): true,
apiRuleCname(userstore.UserRole, "GET", "/v1/captchas/imgs"): true, apiRuleCname(userstore.UserRole, "GET", "/v1/captchas/imgs"): true,
apiRuleCname(userstore.UserRole, "POST", "/v1/fs/sharings"): true, apiRuleCname(userstore.UserRole, "POST", "/v1/fs/sharings"): true,
@ -109,6 +111,7 @@ func NewMultiUsersSvc(cfg gocfg.ICfg, deps *depidx.Deps) (*MultiUsersSvc, error)
apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/files"): true, apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/files"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/dirs"): true, apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/dirs"): true,
apiRuleCname(userstore.VisitorRole, "OPTIONS", "/v1/settings/health"): true, apiRuleCname(userstore.VisitorRole, "OPTIONS", "/v1/settings/health"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/settings/client"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/"): true, apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/imgs"): true, apiRuleCname(userstore.VisitorRole, "GET", "/v1/captchas/imgs"): true,
apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/sharings/exist"): true, apiRuleCname(userstore.VisitorRole, "GET", "/v1/fs/sharings/exist"): true,

View file

@ -7,6 +7,7 @@ import (
"github.com/ihexxa/gocfg" "github.com/ihexxa/gocfg"
"github.com/ihexxa/quickshare/src/db/sitestore" "github.com/ihexxa/quickshare/src/db/sitestore"
"github.com/ihexxa/quickshare/src/db/userstore"
"github.com/ihexxa/quickshare/src/depidx" "github.com/ihexxa/quickshare/src/depidx"
q "github.com/ihexxa/quickshare/src/handlers" q "github.com/ihexxa/quickshare/src/handlers"
) )
@ -55,6 +56,12 @@ func (h *SettingsSvc) SetClientCfg(c *gin.Context) {
return return
} }
role := c.MustGet(q.RoleParam).(string)
if role != userstore.AdminRole {
c.JSON(q.ErrResp(c, 401, q.ErrUnauthorized))
return
}
err = h.deps.SiteStore().SetClientCfg(req.ClientCfg) err = h.deps.SiteStore().SetClientCfg(req.ClientCfg)
if err != nil { if err != nil {
c.JSON(q.ErrResp(c, 500, err)) c.JSON(q.ErrResp(c, 500, err))