Skip to content

Commit

Permalink
Add http.readOnly config.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtricht committed Apr 20, 2019
1 parent 0d49d5d commit 017b2f9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 15 deletions.
8 changes: 6 additions & 2 deletions assets/index.gohtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{{ define "index" }}
{{ template "header" }}
<p><a href="{{ .BaseURL }}add"><i class="fa fa-plus"></i> Add trello board</a></p>
{{ if not .ReadOnly }}
<p><a href="{{ .BaseURL }}add"><i class="fa fa-plus"></i> Add trello board</a></p>
{{ end }}
<table class="table table-bordered table-striped">
<tr>
<th></th>
Expand All @@ -21,7 +23,9 @@
<td>{{ .PointsCompleted }} / {{ .Points }}</td>
<td>
<a href="{{ $.BaseURL }}refresh/{{ .ID }}"><i class="fa fa-refresh" aria-hidden="true"></i></a>
<a href="{{ $.BaseURL }}delete/{{ .ID }}"><i class="fa fa-trash" aria-hidden="true"></i></a>
{{ if not $.ReadOnly }}
<a href="{{ $.BaseURL }}delete/{{ .ID }}"><i class="fa fa-trash" aria-hidden="true"></i></a>
{{ end }}
</td>
</tr>
{{ end }}
Expand Down
8 changes: 4 additions & 4 deletions assets/views.go

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/spf13/viper"
"github.com/mtricht/trello-burndown/pkg/server"
"github.com/mtricht/trello-burndown/pkg/trello"
"github.com/spf13/viper"
)

func init() {
Expand All @@ -27,6 +27,8 @@ func init() {
}
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.WatchConfig()
viper.SetDefault("http.readOnly", false)
log.SetFlags(log.LstdFlags | log.Lshortfile)
}

Expand Down
3 changes: 2 additions & 1 deletion config.yaml.default
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ database:
url: ./trello.db # Example for remote connection: postgresql://user:pass@host:5432/trello?connect_timeout=5
http:
port: 8080
baseURL: http://localhost:8080/ # With trailing slash
baseURL: http://localhost:8080/ # With trailing slash
readOnly: false # Disallow any creation or deletion of boards
10 changes: 9 additions & 1 deletion pkg/server/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import (
"net/http"
"time"

"github.com/spf13/viper"
"github.com/mtricht/trello-burndown/pkg/trello"
"github.com/spf13/viper"
)

type addPage struct {
BaseURL string
}

func addGet(w http.ResponseWriter, r *http.Request) {
if viper.GetBool("http.readOnly") {
w.WriteHeader(http.StatusUnauthorized)
return
}
err := templates.ExecuteTemplate(w, "add", addPage{
BaseURL: viper.GetString("http.baseURL"),
})
Expand All @@ -22,6 +26,10 @@ func addGet(w http.ResponseWriter, r *http.Request) {
}

func addPost(w http.ResponseWriter, r *http.Request) {
if viper.GetBool("http.readOnly") {
w.WriteHeader(http.StatusUnauthorized)
return
}
r.ParseForm()
db := trello.GetDatabase()
defer db.Close()
Expand Down
6 changes: 5 additions & 1 deletion pkg/server/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"net/http"

"github.com/gorilla/mux"
"github.com/spf13/viper"
"github.com/mtricht/trello-burndown/pkg/trello"
"github.com/spf13/viper"
)

func delete(w http.ResponseWriter, r *http.Request) {
if viper.GetBool("http.readOnly") {
w.WriteHeader(http.StatusUnauthorized)
return
}
vars := mux.Vars(r)
db := trello.GetDatabase()
defer db.Close()
Expand Down
12 changes: 7 additions & 5 deletions pkg/server/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package server
import (
"net/http"

"github.com/spf13/viper"
"github.com/mtricht/trello-burndown/pkg/trello"
"github.com/spf13/viper"
)

type indexPage struct {
Boards []trello.Board
BaseURL string
Boards []trello.Board
BaseURL string
ReadOnly bool
}

func index(w http.ResponseWriter, r *http.Request) {
Expand All @@ -18,8 +19,9 @@ func index(w http.ResponseWriter, r *http.Request) {
boards := []trello.Board{}
db.Order("date_start desc").Find(&boards)
indexPage := indexPage{
Boards: boards,
BaseURL: viper.GetString("http.baseURL"),
Boards: boards,
BaseURL: viper.GetString("http.baseURL"),
ReadOnly: viper.GetBool("http.readOnly"),
}
err := templates.ExecuteTemplate(w, "index", indexPage)
if err != nil {
Expand Down

0 comments on commit 017b2f9

Please sign in to comment.