-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use templ instead of standard html/template
Use templ as it better suited for htmx development. Supports: parameter based components, typed components, importing components instead of reading in templates
- Loading branch information
1 parent
fb79af6
commit a331dfe
Showing
36 changed files
with
617 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,5 @@ tmp | |
|
||
# node | ||
node_modules/ | ||
**/styles.css | ||
**/styles.css | ||
**/output.css |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/joho/godotenv" | ||
"github.com/tombrereton/go-hot-reload/internal/application" | ||
) | ||
|
||
func main() { | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Println("No .env file found - loading from environment") | ||
} | ||
cfg := application.NewConfigurationBuilder(). | ||
WithPort(os.Getenv("PORT")). | ||
Build() | ||
app := application.NewApp(cfg) | ||
|
||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) | ||
defer cancel() | ||
|
||
err = app.Start(ctx) | ||
if err == nil { | ||
fmt.Println("Shutting down server: %w", err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package application | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type App struct { | ||
cfg Configuration | ||
router http.Handler | ||
} | ||
|
||
func NewApp(c Configuration) *App { | ||
app := &App{ | ||
router: loadRoutes(), | ||
cfg: c, | ||
} | ||
|
||
return app | ||
} | ||
|
||
func (a *App) Start(ctx context.Context) error { | ||
port := a.cfg.port | ||
server := &http.Server{ | ||
Addr: ":" + port, | ||
Handler: a.router, | ||
} | ||
|
||
ch := make(chan error, 1) | ||
go func() { | ||
err := server.ListenAndServe() | ||
if err != nil { | ||
ch <- fmt.Errorf("error starting server: %w", err) | ||
} | ||
close(ch) | ||
}() | ||
|
||
fmt.Printf("Started server at http://localhost:%s\n", port) | ||
select { | ||
case err := <-ch: | ||
return err | ||
case <-ctx.Done(): | ||
timeout, cancel := context.WithTimeout(context.Background(), time.Second*10) | ||
defer cancel() | ||
return server.Shutdown(timeout) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package application | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
) | ||
|
||
func TestApp_Start(t *testing.T) { | ||
cfg := NewConfigurationBuilder().WithPort("7000").Build() | ||
app := NewApp(cfg) | ||
|
||
server := httptest.NewServer(app.router) | ||
defer server.Close() | ||
|
||
client := server.Client() | ||
resp, err := client.Get(server.URL + "/") | ||
if err != nil { | ||
t.Errorf("Failed to send test request: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("Unexpected status code: got %d, want %d", resp.StatusCode, http.StatusOK) | ||
} | ||
|
||
doc, err := goquery.NewDocumentFromReader(resp.Body) | ||
if err != nil { | ||
t.Errorf("Failed to parse response body: %v", err) | ||
} | ||
|
||
aboutLink := doc.Find("#about-link") | ||
if aboutLink.Length() == 0 { | ||
t.Errorf("Element with id 'about-link' not found") | ||
} | ||
|
||
attr, exists := aboutLink.Attr("hx-get") | ||
if !exists { | ||
t.Errorf("Href attribute not found for element with id 'about-link'") | ||
} else if attr != "/about" { | ||
t.Errorf("Unexpected href value: got %s, want /about", attr) | ||
} | ||
|
||
label := aboutLink.Text() | ||
if label != "About" { | ||
t.Errorf("Unexpected label value: got %s, want About", label) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package application | ||
|
||
type Configuration struct { | ||
port string | ||
} | ||
|
||
type ConfigurationBuilder struct { | ||
configuration Configuration | ||
} | ||
|
||
func NewConfigurationBuilder() *ConfigurationBuilder { | ||
return &ConfigurationBuilder{} | ||
} | ||
|
||
func (b *ConfigurationBuilder) WithPort(port string) *ConfigurationBuilder { | ||
if port == "" { | ||
panic("port cannot be empty") | ||
} | ||
b.configuration.port = port | ||
return b | ||
} | ||
|
||
func (b *ConfigurationBuilder) Build() Configuration { | ||
return b.configuration | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package application | ||
|
||
import ( | ||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/chi/v5/middleware" | ||
"github.com/tombrereton/go-hot-reload/internal/handler" | ||
) | ||
|
||
func loadRoutes() *chi.Mux { | ||
r := chi.NewRouter() | ||
|
||
r.Use(middleware.Logger) | ||
r.Use(middleware.RequestID) | ||
|
||
r.Route("/", marketingRoutes) | ||
r.Route("/static", staticRoutes) | ||
return r | ||
} | ||
|
||
func marketingRoutes(r chi.Router) { | ||
marketingHandler := &handler.Marketing{} | ||
|
||
r.Get("/", marketingHandler.GetLandingPage) | ||
r.Get("/about", marketingHandler.GetAboutPage) | ||
} | ||
|
||
func staticRoutes(r chi.Router) { | ||
staticHandler := &handler.Static{} | ||
|
||
r.Handle("/*", staticHandler.GetStaticFiles()) | ||
} |
Oops, something went wrong.