From c3e905c2698f6eb44e291eeb6b180b13755b7705 Mon Sep 17 00:00:00 2001 From: jan Date: Mon, 21 Dec 2015 22:17:18 +0100 Subject: =?UTF-8?q?grilist=20als=20extra-package=20erstellt=20damit=20wir?= =?UTF-8?q?=20es=20sharen=20k=C3=B6nnen=20mit=20modulen.=20Module=20hinzug?= =?UTF-8?q?ef=C3=BCgt,=20jetzt=20brauchen=20wir=20langsam=20mal=20die=20ga?= =?UTF-8?q?nzen=20grils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diff --git a/config.go b/config.go deleted file mode 100644 index b7149a6..0000000 --- a/config.go +++ /dev/null @@ -1,63 +0,0 @@ -package main - -import ( - "fmt" - "log" - "os" - - "github.com/BurntSushi/toml" -) - -type Config struct { - Database struct { - Hostname string `toml:"hostname"` - //Port int `toml:"port"` - Database string `toml:"database"` - User string `toml:"user"` - Password string `toml:"password"` - SSLEnabled bool `toml:ssl_enabled"` - } `toml:"database"` -} - -func (c *Config) DBConnectionString() string { - var sslmode string - if c.Database.SSLEnabled { - sslmode = "require" - } else { - sslmode = "disable" - } - return fmt.Sprintf("host=%s user=%s dbname=%s password=%s sslmode=%s", - c.Database.Hostname, - c.Database.User, - c.Database.Database, - c.Database.Password, - sslmode) -} - -func LoadConfig() Config { - var config Config - var path string - log.Println("Loading config...") - for _, v := range []string{ - "grilist.conf", - "~/grilist.conf", - "/etc/grilist.conf", - } { - log.Printf("Try \"%s\"...", v) - if _, err := os.Stat(v); err != nil { - log.Printf("Loading config at \"%s\" failed!", v) - } else { - log.Printf("Config file found!") - path = v - break - } - } - if path == "" { - log.Fatal("No config file found at the possible locations!") - } - if _, err := toml.DecodeFile(path, &config); err != nil { - log.Printf("%v", err) - log.Fatal("Fatal error while reading the config!") - } - return config -} diff --git a/dashboard.go b/dashboard.go new file mode 100644 index 0000000..221e923 --- /dev/null +++ b/dashboard.go @@ -0,0 +1,3 @@ +package main + +import () 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 ( "net/http" ) +// Action ist die Aktion auf einer Card. +type Action struct { + Name string + Link string +} + +// Card ist eine Karte im Materialize-Design. +type Card struct { + Title string + Description string + Size string + Actions []Action +} + // Renderer rendert Seiten. type Renderer struct { 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 @@ -[database] -hostname = "localhost" -database = "grilist" -user = "root" -password = "root" -ssl_enabled = false - diff --git a/grilist/config.go b/grilist/config.go new file mode 100644 index 0000000..8798338 --- /dev/null +++ b/grilist/config.go @@ -0,0 +1,63 @@ +package grilist + +import ( + "fmt" + "log" + "os" + + "github.com/BurntSushi/toml" +) + +type Config struct { + Database struct { + Hostname string `toml:"hostname"` + //Port int `toml:"port"` + Database string `toml:"database"` + User string `toml:"user"` + Password string `toml:"password"` + SSLEnabled bool `toml:ssl_enabled"` + } `toml:"database"` +} + +func (c *Config) DBConnectionString() string { + var sslmode string + if c.Database.SSLEnabled { + sslmode = "require" + } else { + sslmode = "disable" + } + return fmt.Sprintf("host=%s user=%s dbname=%s password=%s sslmode=%s", + c.Database.Hostname, + c.Database.User, + c.Database.Database, + c.Database.Password, + sslmode) +} + +func LoadConfig() Config { + var config Config + var path string + log.Println("Loading config...") + for _, v := range []string{ + "grilist.conf", + "~/grilist.conf", + "/etc/grilist.conf", + } { + log.Printf("Try \"%s\"...", v) + if _, err := os.Stat(v); err != nil { + log.Printf("Loading config at \"%s\" failed!", v) + } else { + log.Printf("Config file found!") + path = v + break + } + } + if path == "" { + log.Fatal("No config file found at the possible locations!") + } + if _, err := toml.DecodeFile(path, &config); err != nil { + log.Printf("%v", err) + log.Fatal("Fatal error while reading the config!") + } + return config +} 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 @@ +package grilist + +import ( + "database/sql" + + "fagott.pw/charakterin" + "fagott.pw/grilist/frontend" + + "github.com/julienschmidt/httprouter" + _ "github.com/lib/pq" +) + +// Grilist struct +type Grilist struct { + Config Config + Renderer *frontend.Renderer + Charakterin *charakterin.Charakterin + DB *sql.DB + Modules map[string]Module + Router *httprouter.Router +} + +// Module ist ein Modul für Grilist. +type Module interface { + Init(*Grilist) + Name() string + ProvideDashboardData(*charakterin.User) DashboardCategory +} + +// DashboardCategory ist eine Kategorie mit Karten, die auf dem Dashboard angezeigt werden. +type DashboardCategory struct { + Title string + Cards []frontend.Card +} diff --git a/main.go b/main.go index f367922..e106a80 100644 --- a/main.go +++ b/main.go @@ -2,21 +2,23 @@ package main import ( "database/sql" + "fmt" "log" "net/http" - login "fagott.pw/charakterin" + "fagott.pw/charakterin" "fagott.pw/grilist/frontend" + "fagott.pw/grilist/grilist" + "fagott.pw/grilist/modules/lists" "github.com/julienschmidt/httprouter" _ "github.com/lib/pq" ) -var renderer *frontend.Renderer -var charakterin *login.Charakterin +var app *grilist.Grilist func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { - user, err := charakterin.GetUserFromRequest(r) + user, err := app.Charakterin.GetUserFromRequest(r) if err != nil { http.Redirect(w, r, "/login", 302) return @@ -25,11 +27,19 @@ func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { data := make(map[string]interface{}) data["username"] = user.GetName() - renderer.RenderPage("index", w, data) + app.Renderer.RenderPage("index", w, data) +} + +func loadModule(mod grilist.Module) error { + if _, ok := app.Modules[mod.Name()]; ok { + return fmt.Errorf("module with name %s already exists", mod.Name()) + } + app.Modules[mod.Name()] = mod + return nil } func main() { - config := LoadConfig() + config := grilist.LoadConfig() db, err := sql.Open("postgres", config.DBConnectionString()) if err != nil { log.Fatal(err) @@ -40,22 +50,34 @@ func main() { log.Println("database connection established") - renderer = frontend.New("views") - charakterin = login.New(db) - charakterin.UseRenderer(renderer) + renderer := frontend.New("views") + login := charakterin.New(db) + login.UseRenderer(renderer) router := httprouter.New() - router.HandlerFunc("GET", "/login", charakterin.DisplayLogin) - router.HandlerFunc("POST", "/login", charakterin.Login) - router.HandlerFunc("GET", "/logout", charakterin.Logout) - router.HandlerFunc("GET", "/register", charakterin.DisplayRegistration) - router.HandlerFunc("POST", "/register", charakterin.Register) + + app = &grilist.Grilist{ + config, + renderer, + login, + db, + make(map[string]grilist.Module), + router, + } + + router.HandlerFunc("GET", "/login", login.DisplayLogin) + router.HandlerFunc("POST", "/login", login.Login) + router.HandlerFunc("GET", "/logout", login.Logout) + router.HandlerFunc("GET", "/register", login.DisplayRegistration) + router.HandlerFunc("POST", "/register", login.Register) router.GET("/", index) fs := http.FileServer(http.Dir("assets")) http.Handle("/assets/", http.StripPrefix("/assets/", fs)) - http.Handle("/", router) + // Module laden + loadModule(lists.New()) + log.Fatal(http.ListenAndServe(":8080", nil)) } 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 @@ +package lists + +import ( + "fagott.pw/charakterin" + "fagott.pw/grilist/grilist" +) + +type ListsModule struct { + g *grilist.Grilist +} + +func (d *ListsModule) Name() string { + return "ListsModule" +} + +func (d *ListsModule) Init(g *grilist.Grilist) { + d.g = g +} + +func (d *ListsModule) ProvideDashboardData(user *charakterin.User) grilist.DashboardCategory { + return grilist.DashboardCategory{} +} + +func New() *ListsModule { + return &ListsModule{} +} -- cgit v0.10.1