From 1bc25628c3d4bd77fb37840926ef9af91d2fc479 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Thu, 27 Jun 2024 19:51:50 -0300 Subject: [PATCH] refactor(router)!: refactor routes handling to be "servermore" --- cmd/build/main.go | 30 ------------ cmd/vercel/main.go | 65 ------------------------- components/warning.templ | 10 ++++ config/routes.go | 18 ------- internals/config.go | 4 ++ layouts/page.templ | 6 ++- pages/index.templ | 100 ++++++++++++++++----------------------- pages/router.go | 30 ++++++++++++ 8 files changed, 91 insertions(+), 172 deletions(-) delete mode 100644 cmd/build/main.go delete mode 100644 cmd/vercel/main.go create mode 100644 components/warning.templ delete mode 100644 config/routes.go create mode 100644 internals/config.go create mode 100644 pages/router.go diff --git a/cmd/build/main.go b/cmd/build/main.go deleted file mode 100644 index a67fdb6..0000000 --- a/cmd/build/main.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "context" - "flag" - "log" - - "extrovert/config" - "extrovert/internals" -) - -func main() { - dir := flag.String("d", "./dist", "the directory to write the files") - staticDir := flag.String("s", "./static", "the directory to copy static files from") - - flag.Parse() - - w := internals.StaticWriter{ - DistDir: dir, - StaticDir: staticDir, - Pages: config.ROUTES, - Context: context.Background(), - Logger: *log.Default(), - } - - err := w.WriteAll() - if err != nil { - log.Fatal(err) - } -} diff --git a/cmd/vercel/main.go b/cmd/vercel/main.go deleted file mode 100644 index ef87034..0000000 --- a/cmd/vercel/main.go +++ /dev/null @@ -1,65 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "flag" - "fmt" - "log" - "net/http" - "os" - - "extrovert/config" - "extrovert/internals" -) - -type VercelConfig struct { - OutputDirectory string `json:"outputDirectory"` -} - -var logger = log.Default() - -func main() { - configPath := flag.String("c", "./vercel.json", "the path to the vercel.json file") - staticDir := flag.String("s", "./static", "the directory to copy static files from") - port := flag.Int("p", 8080, "the port to run the server") - - flag.Parse() - - configFile, err := os.ReadFile(*configPath) - if err != nil { - logger.Fatalf("Unable to read vercel.json file due to:\n%s", err) - } - - var c VercelConfig - err = json.Unmarshal(configFile, &c) - if err != nil { - logger.Fatalf("Unable to parse vercel.json file due to:\n%s", err) - } - - w := internals.StaticWriter{ - DistDir: &c.OutputDirectory, - StaticDir: staticDir, - Pages: config.ROUTES, - Context: context.Background(), - Logger: *log.Default(), - } - - logger.Print("Writing static files") - err = w.WriteAll() - if err != nil { - logger.Fatal(err) - } - - logger.Print("Starting server") - mux := http.NewServeMux() - - config.APIROUTES(mux) - mux.Handle("/", http.FileServer(http.Dir(c.OutputDirectory))) - - logger.Printf("Running server at port: %v", *port) - err = http.ListenAndServe(fmt.Sprintf(":%v", *port), mux) - if err != nil { - logger.Fatalf("Server crashed due to:\n%s", err) - } -} diff --git a/components/warning.templ b/components/warning.templ new file mode 100644 index 0000000..2381b78 --- /dev/null +++ b/components/warning.templ @@ -0,0 +1,10 @@ +package components + +templ Warning(title string) { +
+
{ title }
+

+ { children... } +

+
+} diff --git a/config/routes.go b/config/routes.go deleted file mode 100644 index b75b763..0000000 --- a/config/routes.go +++ /dev/null @@ -1,18 +0,0 @@ -package config - -import ( - "net/http" - - "extrovert/api" - "extrovert/internals" - "extrovert/pages" -) - -var ROUTES = []internals.Page{ - {Path: "index.html", Component: pages.Index()}, -} - -func APIROUTES(mux *http.ServeMux) { - mux.HandleFunc("/robots.txt", api.RobotsTxt) - mux.HandleFunc("/ai.txt", api.AiTxt) -} diff --git a/internals/config.go b/internals/config.go new file mode 100644 index 0000000..c66f76f --- /dev/null +++ b/internals/config.go @@ -0,0 +1,4 @@ +package internals + +const APP_VERSION = "1" +const APP_NAME = "project-extrovert" diff --git a/layouts/page.templ b/layouts/page.templ index 67c571c..539ace1 100644 --- a/layouts/page.templ +++ b/layouts/page.templ @@ -15,9 +15,13 @@ templ Page(title string) { rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css" /> + { title } - + { children... } diff --git a/pages/index.templ b/pages/index.templ index e8329cf..73189f6 100644 --- a/pages/index.templ +++ b/pages/index.templ @@ -1,69 +1,53 @@ package pages import ( + "net/http" + "extrovert/layouts" + "extrovert/components" + "extrovert/internals" ) -templ Index() { - @layouts.Page("013") { - -
-
-
- -
- This is a project by - +
+
- Capytal Code - , - keep visting this link to see the project's evoluition and development. -
+ -
-
+ + } } diff --git a/pages/router.go b/pages/router.go new file mode 100644 index 0000000..b405679 --- /dev/null +++ b/pages/router.go @@ -0,0 +1,30 @@ +package pages + +import ( + "net/http" + + "github.com/a-h/templ" +) + +var ROUTES = []Route{ + { + Pattern: "/index.html", + Static: true, + Page: IndexPage(), + Handler: IndexHandler, + }, + +type RouteHandler = func(http.ResponseWriter, *http.Request) + +type Route struct { + Pattern string + Static bool + Handler RouteHandler + Page templ.Component +} + +func RegisterAllRoutes(routes []Route, s *http.ServeMux) { + for _, r := range routes { + s.HandleFunc(r.Pattern, r.Handler) + } +}