Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/add_readonly'
Browse files Browse the repository at this point in the history
  • Loading branch information
kfrico committed Jul 16, 2021
2 parents 79f3459 + 9834468 commit aa8526b
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 19 deletions.
4 changes: 4 additions & 0 deletions client/src/api/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ const config = {
hostinfo: {
type: 'GET',
url: `${domain}/hostinfo`
},
readonly: {
type: 'GET',
url: `${domain}/readonly`
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion client/src/components/side-bar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ el-menu(

//- NOTE: query
el-menu-item(
v-if="!$readonly"
@click="openQuery"
index="-1")
i(class="el-icon-edit")
span(lot="title") Query

//- NOTE: query
//- NOTE: host info
el-menu-item(
@click="goHostInfo"
index="-2")
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/table-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
i(class="table-list-icon el-icon-download")
span export
el-table-column(
v-if="!$readonly"
column-key="import"
label="import")
i(class="table-list-icon el-icon-upload2")
span import
el-table-column(
v-if="!$readonly"
column-key="truncate"
label="truncate")
i(class="table-list-icon el-icon-folder-delete")
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/table/find/result.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
width="150")
el-button-group(slot-scope="scope")
el-button(
v-if="!$readonly"
type="primary"
@click="handleOpenEditDialog(scope.row)"
icon="el-icon-edit"
size="mini")

el-button(
v-if="!$readonly"
type="danger"
icon="el-icon-delete"
size="mini"
Expand Down
27 changes: 22 additions & 5 deletions client/src/components/table/menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,28 @@ export default {
return {
defaultActive: get(arr, 5),

tabs: [
// {
// name: 'rows',
// trigger: this.showRows
// },
tabs: this.$readonly ? [
{
name: 'rows',
trigger: this.RowsToken
},
{
name: 'find',
trigger: this.Find
},
{
name: 'columns',
trigger: this.Columns
},
{
name: 'definition',
trigger: this.Definition
},
{
name: 'export',
trigger: this.Export
}
] : [
{
name: 'rows',
trigger: this.RowsToken
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/table/rowstoken.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
el-button-group(slot-scope="scope")

el-button(
v-if="!$readonly"
type="primary"
@click="handleOpenEditDialog(scope.row)"
icon="el-icon-edit"
size="mini")

el-button(
v-if="!$readonly"
type="danger"
icon="el-icon-delete"
size="mini"
Expand Down
34 changes: 26 additions & 8 deletions client/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,32 @@ import '@/plugins/vue-codemirror'
import Vue from 'vue';
import App from './App';
import router from './router';
import api from '@/api'

Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>',
render: h => h(App),
});
const service = api.make('root')


async function init() {
try {
const res = await service.request('readonly')
const ret = res.get()

Vue.prototype.$readonly = ret.readonly

/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>',
render: h => h(App),
});
} catch (error) {
// eslint-disable-next-line
console.log(error)
}
}

init();
1 change: 1 addition & 0 deletions service/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
HOST_PORT: ":8083"
READ_ONLY: false

CASSANDRA_HOST: cassandra
CASSANDRA_PORT: 9042
Expand Down
37 changes: 32 additions & 5 deletions service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
)

const (
PartitionKey = "partition_key"
ClusteringKey = "clustering"
PartitionKey = "partition_key"
ClusteringKey = "clustering"
ReadOnlyMessage = "Update/Insert action are not allowed"
)

// init 初始化
Expand Down Expand Up @@ -65,6 +66,7 @@ var env envStruct
// envStruct type
type envStruct struct {
HostPort string `mapstructure:"HOST_PORT" json:"HOST_PORT"`
ReadOnly bool `mapstructure:"READ_ONLY" json:"READ_ONLY"`
CassandraHost string `mapstructure:"CASSANDRA_HOST" json:"CASSANDRA_HOST"`
CassandraPort int `mapstructure:"CASSANDRA_PORT" json:"CASSANDRA_PORT"`
CassandraUsername string `mapstructure:"CASSANDRA_USERNAME" json:"CASSANDRA_USERNAME"`
Expand Down Expand Up @@ -144,7 +146,7 @@ func run(c *cli.Context) {
log.Fatal(err)
}

h := &Handler{Session: session}
h := &Handler{Session: session, ReadOnly: env.ReadOnly}

// Echo instance
e := echo.New()
Expand Down Expand Up @@ -176,17 +178,27 @@ func run(c *cli.Context) {
e.GET("/columns", h.Columns)
e.GET("/export", h.Export)
e.GET("/hostinfo", h.HostInfo)
e.GET("/readonly", h.ReadOnlyInfo)

// Start server
e.Logger.Fatal(e.Start(env.HostPort))
}

type Handler struct {
Session *gocql.Session
Session *gocql.Session
ReadOnly bool
}

func (h *Handler) ReadOnlyInfo(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]bool{"readonly": h.ReadOnly})
}

// Query Query cql語法處理
func (h *Handler) Query(c echo.Context) error {
if h.ReadOnly {
return echo.NewHTTPError(http.StatusForbidden, ReadOnlyMessage)
}

req := struct {
Query string `json:"query" form:"query" query:"query"`
}{}
Expand Down Expand Up @@ -346,7 +358,6 @@ func (h *Handler) HostInfo(c echo.Context) error {

// RowToken
func (h *Handler) RowToken(c echo.Context) error {

var (
req RowTokenReq
schema []map[string]interface{}
Expand Down Expand Up @@ -579,6 +590,10 @@ func (h *Handler) Columns(c echo.Context) error {
}

func (h *Handler) Save(c echo.Context) error {
if h.ReadOnly {
return echo.NewHTTPError(http.StatusForbidden, ReadOnlyMessage)
}

req := struct {
Table string `json:"table" form:"table" query:"table"`
Item string `json:"item" form:"item" query:"item"`
Expand Down Expand Up @@ -624,6 +639,10 @@ func (h *Handler) Save(c echo.Context) error {

// Delete 刪除row
func (h *Handler) Delete(c echo.Context) error {
if h.ReadOnly {
return echo.NewHTTPError(http.StatusForbidden, ReadOnlyMessage)
}

req := struct {
Table string `json:"table" form:"table" query:"table"`
Item string `json:"item" form:"item" query:"item"`
Expand Down Expand Up @@ -860,6 +879,10 @@ func (h *Handler) Export(c echo.Context) error {

// Export 匯入copy file
func (h *Handler) Import(c echo.Context) error {
if h.ReadOnly {
return echo.NewHTTPError(http.StatusForbidden, ReadOnlyMessage)
}

file, err := c.FormFile("file")
table := c.FormValue("table")

Expand Down Expand Up @@ -928,6 +951,10 @@ func (h *Handler) Import(c echo.Context) error {

// Truncate 清除table資料
func (h *Handler) Truncate(c echo.Context) error {
if h.ReadOnly {
return echo.NewHTTPError(http.StatusForbidden, ReadOnlyMessage)
}

req := struct {
Table string `json:"table" form:"table" query:"table"`
}{}
Expand Down

0 comments on commit aa8526b

Please sign in to comment.