diff options
| author | Alexander Kavon <hawk@alexkavon.com> | 2023-11-27 14:48:44 -0500 |
|---|---|---|
| committer | Alexander Kavon <hawk@alexkavon.com> | 2023-11-27 14:48:44 -0500 |
| commit | 1d05f918887e9ba4576149513e5df0bb57e6bd72 (patch) | |
| tree | 2f5e94e21f90183509fe1ae52905c47d67a3c5d0 | |
| parent | 8a30281eff35623d1a68642e4d94c814d1b47a0c (diff) | |
config updates, rename user model, load auth routes, initial templating poc
| -rw-r--r-- | .newsstandrc.toml.example | 11 | ||||
| -rw-r--r-- | src/auth/routes.go | 31 | ||||
| -rw-r--r-- | src/conf/conf.go | 34 | ||||
| -rw-r--r-- | src/main.go | 5 | ||||
| -rw-r--r-- | src/server/server.go | 15 | ||||
| -rw-r--r-- | src/server/templates.go | 9 | ||||
| -rw-r--r-- | src/user/routes.go | 33 | ||||
| -rw-r--r-- | src/user/user.go (renamed from src/user/user_model.go) | 0 | ||||
| -rw-r--r-- | views/templates/main.tmpl.html | 10 |
9 files changed, 125 insertions, 23 deletions
diff --git a/.newsstandrc.toml.example b/.newsstandrc.toml.example index 6d20ede..0b12f95 100644 --- a/.newsstandrc.toml.example +++ b/.newsstandrc.toml.example @@ -1,3 +1,8 @@ -DB_ADAPTER = "postgresql" -DB_USER = "newsstand" -DB_PASS = "newsstand" +[database] +adapter = "postgresql" +user = "newsstand" +pass = "newsstand" + +[server] +port = "8080" +hostname = "localhost" 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 diff --git a/views/templates/main.tmpl.html b/views/templates/main.tmpl.html new file mode 100644 index 0000000..b6eeb51 --- /dev/null +++ b/views/templates/main.tmpl.html @@ -0,0 +1,10 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <title>newsstand.nyc</title> + <script src="https://unpkg.com/htmx.org@1.9.9" integrity="sha384-QFjmbokDn2DjBjq+fM+8LUIVrAgqcNW2s0PjAxHETgRn9l4fvX31ZxDxvwQnyMOX" crossorigin="anonymous"></script> +</head> +<body> + <h1>Wello, Horld!</h1> +</body> +</html> |
