aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dashboard.go3
-rw-r--r--frontend/renderer.go14
-rw-r--r--grilist.conf7
-rw-r--r--grilist/config.go (renamed from config.go)2
-rw-r--r--grilist/grilist.go34
-rw-r--r--main.go52
-rw-r--r--modules/lists/lists.go26
7 files changed, 115 insertions, 23 deletions
diff --git a/dashboard.go b/dashboard.go
new file mode 100644
index 0000000..221e923
--- /dev/null
+++ b/dashboard.go
@@ -0,0 +1,3 @@
1package main
2
3import ()
diff --git a/frontend/renderer.go b/frontend/renderer.go
index f03fe77..0a2b7e9 100644
--- a/frontend/renderer.go
+++ b/frontend/renderer.go
@@ -5,6 +5,20 @@ import (
5 "net/http" 5 "net/http"
6) 6)
7 7
8// Action ist die Aktion auf einer Card.
9type Action struct {
10 Name string
11 Link string
12}
13
14// Card ist eine Karte im Materialize-Design.
15type Card struct {
16 Title string
17 Description string
18 Size string
19 Actions []Action
20}
21
8// Renderer rendert Seiten. 22// Renderer rendert Seiten.
9type Renderer struct { 23type Renderer struct {
10 templates *template.Template 24 templates *template.Template
diff --git a/grilist.conf b/grilist.conf
deleted file mode 100644
index 65c15a3..0000000
--- a/grilist.conf
+++ /dev/null
@@ -1,7 +0,0 @@
1[database]
2hostname = "localhost"
3database = "grilist"
4user = "root"
5password = "root"
6ssl_enabled = false
7
diff --git a/config.go b/grilist/config.go
index b7149a6..8798338 100644
--- a/config.go
+++ b/grilist/config.go
@@ -1,4 +1,4 @@
1package main 1package grilist
2 2
3import ( 3import (
4 "fmt" 4 "fmt"
diff --git a/grilist/grilist.go b/grilist/grilist.go
new file mode 100644
index 0000000..c672dec
--- /dev/null
+++ b/grilist/grilist.go
@@ -0,0 +1,34 @@
1package grilist
2
3import (
4 "database/sql"
5
6 "fagott.pw/charakterin"
7 "fagott.pw/grilist/frontend"
8
9 "github.com/julienschmidt/httprouter"
10 _ "github.com/lib/pq"
11)
12
13// Grilist struct
14type Grilist struct {
15 Config Config
16 Renderer *frontend.Renderer
17 Charakterin *charakterin.Charakterin
18 DB *sql.DB
19 Modules map[string]Module
20 Router *httprouter.Router
21}
22
23// Module ist ein Modul für Grilist.
24type Module interface {
25 Init(*Grilist)
26 Name() string
27 ProvideDashboardData(*charakterin.User) DashboardCategory
28}
29
30// DashboardCategory ist eine Kategorie mit Karten, die auf dem Dashboard angezeigt werden.
31type DashboardCategory struct {
32 Title string
33 Cards []frontend.Card
34}
diff --git a/main.go b/main.go
index f367922..e106a80 100644
--- a/main.go
+++ b/main.go
@@ -2,21 +2,23 @@ package main
2 2
3import ( 3import (
4 "database/sql" 4 "database/sql"
5 "fmt"
5 "log" 6 "log"
6 "net/http" 7 "net/http"
7 8
8 login "fagott.pw/charakterin" 9 "fagott.pw/charakterin"
9 "fagott.pw/grilist/frontend" 10 "fagott.pw/grilist/frontend"
11 "fagott.pw/grilist/grilist"
12 "fagott.pw/grilist/modules/lists"
10 13
11 "github.com/julienschmidt/httprouter" 14 "github.com/julienschmidt/httprouter"
12 _ "github.com/lib/pq" 15 _ "github.com/lib/pq"
13) 16)
14 17
15var renderer *frontend.Renderer 18var app *grilist.Grilist
16var charakterin *login.Charakterin
17 19
18func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { 20func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
19 user, err := charakterin.GetUserFromRequest(r) 21 user, err := app.Charakterin.GetUserFromRequest(r)
20 if err != nil { 22 if err != nil {
21 http.Redirect(w, r, "/login", 302) 23 http.Redirect(w, r, "/login", 302)
22 return 24 return
@@ -25,11 +27,19 @@ func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
25 data := make(map[string]interface{}) 27 data := make(map[string]interface{})
26 28
27 data["username"] = user.GetName() 29 data["username"] = user.GetName()
28 renderer.RenderPage("index", w, data) 30 app.Renderer.RenderPage("index", w, data)
31}
32
33func loadModule(mod grilist.Module) error {
34 if _, ok := app.Modules[mod.Name()]; ok {
35 return fmt.Errorf("module with name %s already exists", mod.Name())
36 }
37 app.Modules[mod.Name()] = mod
38 return nil
29} 39}
30 40
31func main() { 41func main() {
32 config := LoadConfig() 42 config := grilist.LoadConfig()
33 db, err := sql.Open("postgres", config.DBConnectionString()) 43 db, err := sql.Open("postgres", config.DBConnectionString())
34 if err != nil { 44 if err != nil {
35 log.Fatal(err) 45 log.Fatal(err)
@@ -40,22 +50,34 @@ func main() {
40 50
41 log.Println("database connection established") 51 log.Println("database connection established")
42 52
43 renderer = frontend.New("views") 53 renderer := frontend.New("views")
44 charakterin = login.New(db) 54 login := charakterin.New(db)
45 charakterin.UseRenderer(renderer) 55 login.UseRenderer(renderer)
46 56
47 router := httprouter.New() 57 router := httprouter.New()
48 router.HandlerFunc("GET", "/login", charakterin.DisplayLogin) 58
49 router.HandlerFunc("POST", "/login", charakterin.Login) 59 app = &grilist.Grilist{
50 router.HandlerFunc("GET", "/logout", charakterin.Logout) 60 config,
51 router.HandlerFunc("GET", "/register", charakterin.DisplayRegistration) 61 renderer,
52 router.HandlerFunc("POST", "/register", charakterin.Register) 62 login,
63 db,
64 make(map[string]grilist.Module),
65 router,
66 }
67
68 router.HandlerFunc("GET", "/login", login.DisplayLogin)
69 router.HandlerFunc("POST", "/login", login.Login)
70 router.HandlerFunc("GET", "/logout", login.Logout)
71 router.HandlerFunc("GET", "/register", login.DisplayRegistration)
72 router.HandlerFunc("POST", "/register", login.Register)
53 router.GET("/", index) 73 router.GET("/", index)
54 74
55 fs := http.FileServer(http.Dir("assets")) 75 fs := http.FileServer(http.Dir("assets"))
56 http.Handle("/assets/", http.StripPrefix("/assets/", fs)) 76 http.Handle("/assets/", http.StripPrefix("/assets/", fs))
57
58 http.Handle("/", router) 77 http.Handle("/", router)
59 78
79 // Module laden
80 loadModule(lists.New())
81
60 log.Fatal(http.ListenAndServe(":8080", nil)) 82 log.Fatal(http.ListenAndServe(":8080", nil))
61} 83}
diff --git a/modules/lists/lists.go b/modules/lists/lists.go
new file mode 100644
index 0000000..f8f13bb
--- /dev/null
+++ b/modules/lists/lists.go
@@ -0,0 +1,26 @@
1package lists
2
3import (
4 "fagott.pw/charakterin"
5 "fagott.pw/grilist/grilist"
6)
7
8type ListsModule struct {
9 g *grilist.Grilist
10}
11
12func (d *ListsModule) Name() string {
13 return "ListsModule"
14}
15
16func (d *ListsModule) Init(g *grilist.Grilist) {
17 d.g = g
18}
19
20func (d *ListsModule) ProvideDashboardData(user *charakterin.User) grilist.DashboardCategory {
21 return grilist.DashboardCategory{}
22}
23
24func New() *ListsModule {
25 return &ListsModule{}
26}