aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/auth/routes.go31
-rw-r--r--src/conf/conf.go34
-rw-r--r--src/main.go5
-rw-r--r--src/server/server.go15
-rw-r--r--src/server/templates.go9
-rw-r--r--src/user/routes.go33
-rw-r--r--src/user/user.go (renamed from src/user/user_model.go)0
7 files changed, 107 insertions, 20 deletions
diff --git a/src/auth/routes.go b/src/auth/routes.go
new file mode 100644
index 0000000..796d123
--- /dev/null
+++ b/src/auth/routes.go
@@ -0,0 +1,31 @@
+package auth
+
+import (
+ "html/template"
+ "log"
+ "net/http"
+ "os"
+
+ "gitlab.com/alexkavon/newsstand/src/server"
+)
+
+var Routes = server.Routes{
+ server.Route{
+ Name: "Register",
+ Method: "GET",
+ Pattern: "/auth/register",
+ AuthRequired: false,
+ HandlerFunc: Register,
+ },
+}
+
+func Register(s *server.Server) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ cwd, err := os.Getwd()
+ if err != nil {
+ log.Fatal(err)
+ }
+ tmpl := template.Must(template.ParseFiles(cwd + "/views/templates/main.tmpl.html"))
+ tmpl.Execute(w, nil)
+ }
+}
diff --git a/src/conf/conf.go b/src/conf/conf.go
index f68c2f4..8abb8a8 100644
--- a/src/conf/conf.go
+++ b/src/conf/conf.go
@@ -9,40 +9,46 @@ import (
type (
Conf struct {
- Db Db
- Server Server
+ Db Db `toml:"database"`
+ Server Server `toml:"server"`
}
Db struct {
- Adapter string `toml:"DB_ADAPTER"`
- User string `toml:"DB_USER"`
- Password string `toml:"DB_PASS"`
+ Adapter string `toml:"adapter"`
+ User string `toml:"user"`
+ Password string `toml:"pass"`
}
Server struct {
- Hostname string `toml:"SERVER_HOSTNAME"`
- Port string `toml:"SERVER_PORT"`
+ Hostname string `toml:"hostname"`
+ Port string `toml:"port"`
+ TemplatePath string `toml:"template_path"`
}
)
func NewConf() *Conf {
+ workingdir, err := os.Getwd()
+ if err != nil {
+ log.Fatal(err)
+ }
filepath := os.Getenv("NEWSSTAND_CONFIG_PATH")
if filepath == "" {
- workingdir, err := os.Getwd()
- if err != nil {
- log.Fatal(err)
- }
filepath = workingdir + "/.newsstandrc.toml"
}
log.Printf("Config file path: %s", filepath)
- var c Conf
- _, err := toml.DecodeFile(filepath, &c)
+ c := Conf{
+ Db{},
+ Server{
+ TemplatePath: workingdir + "/views",
+ },
+ }
+ _, err = toml.DecodeFile(filepath, &c)
if err != nil {
log.Fatalln(err)
}
- log.Println(c.Db.Adapter)
+ log.Printf("Config loaded: %s", c)
return &c
}
diff --git a/src/main.go b/src/main.go
index 354d1ec..fe6a51a 100644
--- a/src/main.go
+++ b/src/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "gitlab.com/alexkavon/newsstand/src/auth"
"gitlab.com/alexkavon/newsstand/src/conf"
"gitlab.com/alexkavon/newsstand/src/db"
"gitlab.com/alexkavon/newsstand/src/server"
@@ -12,5 +13,9 @@ func main() {
// connect database
// start server
s := server.NewServer(config, server.NewRouter(config), db.NewDb(config))
+ for _, route := range auth.Routes {
+ s.Router.
+ Method(route.Method, route.Pattern, route.HandlerFunc(s))
+ }
s.Serve()
}
diff --git a/src/server/server.go b/src/server/server.go
index 21cbe71..6b86159 100644
--- a/src/server/server.go
+++ b/src/server/server.go
@@ -2,6 +2,7 @@ package server
import (
"database/sql"
+ "html/template"
"net/http"
"github.com/go-chi/chi/v5"
@@ -9,16 +10,18 @@ import (
)
type Server struct {
- Router *chi.Mux
- Db *sql.DB
- Config *conf.Conf
+ Router *chi.Mux
+ Db *sql.DB
+ Config *conf.Conf
+ Templates map[string]*template.Template
}
func NewServer(config *conf.Conf, router *chi.Mux, db *sql.DB) *Server {
return &Server{
- Router: router,
- Db: db,
- Config: config,
+ Router: router,
+ Db: db,
+ Config: config,
+ Templates: buildTemplates("my/path", "my/other/path"),
}
}
diff --git a/src/server/templates.go b/src/server/templates.go
new file mode 100644
index 0000000..572633a
--- /dev/null
+++ b/src/server/templates.go
@@ -0,0 +1,9 @@
+package server
+
+import (
+ "html/template"
+)
+
+func buildTemplates(paths ...string) map[string]*template.Template {
+ return map[string]*template.Template{}
+}
diff --git a/src/user/routes.go b/src/user/routes.go
new file mode 100644
index 0000000..b265e67
--- /dev/null
+++ b/src/user/routes.go
@@ -0,0 +1,33 @@
+package user
+
+import (
+ "net/http"
+
+ "gitlab.com/alexkavon/newsstand/src/server"
+)
+
+var Routes = server.Routes{
+ server.Route{
+ Name: "Create",
+ Method: "POST",
+ Pattern: "/users",
+ AuthRequired: false,
+ HandlerFunc: Create,
+ },
+ server.Route{
+ Name: "Me",
+ Method: "GET",
+ Pattern: "/users/me",
+ AuthRequired: true,
+ HandlerFunc: Show,
+ },
+}
+
+func Create(s *server.Server) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ }
+}
+
+func Show(s *server.Server) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {}
+}
diff --git a/src/user/user_model.go b/src/user/user.go
index 9845b1b..9845b1b 100644
--- a/src/user/user_model.go
+++ b/src/user/user.go