Skip to content

Commit

Permalink
Gormified.
Browse files Browse the repository at this point in the history
  • Loading branch information
nitrix committed Feb 12, 2022
1 parent 9f5b5ee commit 0fa8889
Show file tree
Hide file tree
Showing 9 changed files with 509 additions and 434 deletions.
2 changes: 1 addition & 1 deletion board.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

type Board struct {
ID int `json:"id"`
ID uint64 `json:"id,string"`
Title string `json:"title"`
Lists []List `json:"lists,omitempty"`
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ module kanban
go 1.15

require (
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2
github.com/mattn/go-sqlite3 v1.14.6
github.com/mattn/go-sqlite3 v1.14.9
github.com/sethvargo/go-password v0.2.0
gorm.io/driver/postgres v1.2.3 // indirect
gorm.io/driver/sqlite v1.2.6 // indirect
gorm.io/gorm v1.22.5 // indirect
)
187 changes: 187 additions & 0 deletions go.sum

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

type List struct {
ID int `json:"id"`
Title string `json:"title"`
Notes []Note `json:"notes"`
}
ID uint64 `json:"id,string"`
BoardID uint64 `json:"board_id,string"`
Title string `json:"title"`
Order uint `json:"order"`
Notes []Note `json:"notes"`
}
32 changes: 26 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ package main
import (
"crypto/subtle"
"database/sql"
"github.com/gorilla/websocket"
tokenGenerator "github.com/sethvargo/go-password/password"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
"time"

"github.com/gorilla/websocket"
tokenGenerator "github.com/sethvargo/go-password/password"

"gorm.io/driver/postgres"
"gorm.io/gorm"
)

var connections map[*websocket.Conn]bool
var connectionsMutex sync.Mutex
var database *sql.DB
var db *gorm.DB

func main() {
var err error

connections = make(map[*websocket.Conn]bool, 0)
connections = make(map[*websocket.Conn]bool)

_ = os.Mkdir("data", 0700)

Expand All @@ -31,7 +36,22 @@ func main() {

database, err = sql.Open("sqlite3", "data/kanban.db?_foreign_keys=on")
if err != nil {
log.Fatalln(err)
log.Fatalln("Unable to open Sqlite3 database:", err)
}

// db, err = gorm.Open(sqlite.Open("data/kanban.db?_foreign_keys=on"), nil)
// if err != nil {
// log.Fatalln("Unable to connect to Sqlite database:", err)
// }

db, err = gorm.Open(postgres.Open("postgresql://kanban@localhost:26257/kanban?sslmode=disable"), nil)
if err != nil {
log.Fatalln("Unable to connect to Postgres database:", err)
}

err = db.AutoMigrate(&Board{}, &List{}, &Note{})
if err != nil {
log.Fatalln("Unable to migrate database:", err)
}

username := os.Getenv("USERNAME")
Expand Down Expand Up @@ -73,7 +93,7 @@ func live(w http.ResponseWriter, r *http.Request) {
for {
_, data, err := c.ReadMessage()
if err != nil {
log.Println("Unable to read from websocket:", err)
// log.Println("Unable to read from websocket:", err)
return
}

Expand Down Expand Up @@ -102,7 +122,7 @@ func authenticationHandler(handler http.HandlerFunc, username, password, realm s
return handler
}

return func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
trustCookie, err := r.Cookie("trust")
if err == nil {
if isTrusted(trustCookie.Value) {
Expand Down
80 changes: 41 additions & 39 deletions message.go
Original file line number Diff line number Diff line change
@@ -1,83 +1,85 @@
package main

type Command string

const (
CommandGetBoardList Command = "GET_BOARD_LIST"
CommandGetBoard Command = "GET_BOARD"
CommandBoard Command = "BOARD"
CommandBoardList Command = "BOARD_LIST"
CommandAddNote Command = "ADD_NOTE"
CommandDeleteNote Command = "DELETE_NOTE"
CommandEditNote Command = "EDIT_NOTE"
CommandEditList Command = "EDIT_LIST"
CommandEditBoard Command = "EDIT_BOARD"
CommandAddList Command = "ADD_LIST"
CommandDeleteList Command = "DELETE_LIST"
CommandAddBoard Command = "ADD_BOARD"
CommandDeleteBoard Command = "DELETE_BOARD"
CommandMoveList Command = "MOVE_LIST"
CommandGetBoard Command = "GET_BOARD"
CommandBoard Command = "BOARD"
CommandBoardList Command = "BOARD_LIST"
CommandAddNote Command = "ADD_NOTE"
CommandDeleteNote Command = "DELETE_NOTE"
CommandEditNote Command = "EDIT_NOTE"
CommandEditList Command = "EDIT_LIST"
CommandEditBoard Command = "EDIT_BOARD"
CommandAddList Command = "ADD_LIST"
CommandDeleteList Command = "DELETE_LIST"
CommandAddBoard Command = "ADD_BOARD"
CommandDeleteBoard Command = "DELETE_BOARD"
CommandMoveList Command = "MOVE_LIST"
)

type Message struct {
Command Command `json:"command"`
Data interface{} `json:"data"`
Command Command `json:"command"`
Data interface{} `json:"data"`
}

type MessageMoveList struct {
Id int `json:"id"`
BoardId int `json:"board_id"`
Direction string `json:"direction"`
Id uint64 `json:"id,string"`
BoardId uint64 `json:"board_id,string"`
Direction string `json:"direction"`
ListIds []string `json:"list_ids"`
}

type MessageGetBoard struct {
Id int `json:"id"`
Id uint64 `json:"id,string"`
}

type MessageDeleteBoard struct {
Id int `json:"id"`
Id uint64 `json:"id,string"`
}

type MessageAddBoard struct {
Id int `json:"id,omitempty"`
Id uint64 `json:"id,string,omitempty"`
Title string `json:"title"`
}

type MessageAddList struct {
Id int `json:"id,omitempty"`
Title string `json:"title"`
BoardId int `json:"board_id"`
Id uint64 `json:"id,string,omitempty"`
Title string `json:"title"`
BoardId uint64 `json:"board_id,string"`
}

type MessageDeleteList struct {
Id int `json:"id"`
Id uint64 `json:"id,string"`
}

type MessageAddNote struct {
Id int `json:"id,omitempty"`
Uuid string `json:"uuid"`
Text string `json:"text"`
ListId int `json:"list_id"`
Id uint64 `json:"id,string,omitempty"`
Uuid string `json:"uuid"`
Text string `json:"text"`
ListId uint64 `json:"list_id,string"`
}

type MessageDeleteNote struct {
Id int `json:"id"`
Id uint64 `json:"id,string"`
}

type MessageEditNote struct {
Id int `json:"id"`
ListId int `json:"list_id,omitempty"`
Text string `json:"text,omitempty"`
Raw *bool `json:"raw,omitempty"`
Minimized *bool `json:"minimized,omitempty"`
PreviousNoteId *int `json:"previous_note_id,omitempty"`
Id uint64 `json:"id,string"`
ListId uint64 `json:"list_id,string,omitempty"`
Text string `json:"text,omitempty"`
Raw *bool `json:"raw,omitempty"`
Minimized *bool `json:"minimized,omitempty"`
PreviousNoteId *string `json:"previous_note_id,omitempty"`
}

type MessageEditList struct {
Id int `json:"id"`
Id uint64 `json:"id,string"`
Title string `json:"title"`
}

type MessageEditBoard struct {
Id int `json:"id"`
Id uint64 `json:"id,string"`
Title string `json:"title"`
}
}
11 changes: 6 additions & 5 deletions note.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

type Note struct {
ID int `json:"id"`
Minimized bool `json:"min"`
Raw bool `json:"raw"`
Text string `json:"text"`
order int
ID uint64 `json:"id,string"`
ListID uint64 `json:"list_id,string"`
Minimized bool `json:"min"`
Raw bool `json:"raw"`
Text string `json:"text"`
Order uint
}
Loading

0 comments on commit 0fa8889

Please sign in to comment.