Skip to content

Commit

Permalink
Add flag to disable TLS for live reload of templ components (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickclyde authored Aug 29, 2024
1 parent bd26dab commit 329cd33
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 15 deletions.
51 changes: 51 additions & 0 deletions .air-with-proxy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = ["-tls=false"]
bin = "./bin/main"
cmd = "go build -o ./bin/main ./cmd/phinvads-go && templ generate --notify-proxy"
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go", ".*_templ.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html", "css", "js"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = ["lsof -i :4000 | awk 'NR==2 {print $2; exit}' | xargs kill"]
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[proxy]
app_port = 0
enabled = false
proxy_port = 0

[screen]
clear_on_rebuild = false
keep_scroll = true
2 changes: 1 addition & 1 deletion .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
post_cmd = ["lsof -i :4000 | awk 'NR==2 {print $2; exit}' | xargs kill"]
pre_cmd = []
rerun = false
rerun_delay = 500
Expand Down
6 changes: 6 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DB_HOST=localhost
DB_PORT=5432
DB_NAME=phinvads

HOST=localhost
PORT=4000
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ tmp/
bin/

tls/*.pem

*_templ.txt
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ PHIN VADS written in Go. Load `phinvads.dump` into a PostgreSQL database using `
cd ..
```

1. Run the app!
1. Run the app! If you are only working on backend code, you can just run a simple live reload with air:

```bash
air
```

1. Air will also work for the frontend, but you will have to refresh your browser every time you make a change. To get automatic browser reloads, run the app this way:

```bash
templ generate --watch --proxy="http://localhost:4000" --cmd="air -c .air-with-proxy.toml"
```
15 changes: 12 additions & 3 deletions internal/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Application struct {
db *sql.DB
server *http.Server
repository *rp.Repository
tlsEnabled bool
}

func SetupApp(cfg *cfg.Config) *Application {
Expand All @@ -41,6 +42,7 @@ func SetupApp(cfg *cfg.Config) *Application {
logger: logger,
db: db,
repository: rp,
tlsEnabled: *cfg.TlsEnabled,
}

srv := &http.Server{
Expand All @@ -52,6 +54,7 @@ func SetupApp(cfg *cfg.Config) *Application {
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}

app.server = srv
return app
}
Expand All @@ -60,10 +63,16 @@ func SetupApp(cfg *cfg.Config) *Application {
func (app *Application) Run() {
app.logger.Info("starting server", slog.String("addr", app.server.Addr))

tlsCert := getEnvWithFallback("TLS_CERT", "./tls/localhost.pem")
tlsKey := getEnvWithFallback("TLS_KEY", "./tls/localhost-key.pem")
var err error
if app.tlsEnabled {
tlsCert := getEnvWithFallback("TLS_CERT", "./tls/localhost.pem")
tlsKey := getEnvWithFallback("TLS_KEY", "./tls/localhost-key.pem")

err = app.server.ListenAndServeTLS(tlsCert, tlsKey)
} else {
err = app.server.ListenAndServe()
}

err := app.server.ListenAndServeTLS(tlsCert, tlsKey)
app.logger.Error(err.Error())

defer app.db.Close()
Expand Down
18 changes: 14 additions & 4 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"fmt"
"log"
"os"
"strconv"

"github.com/joho/godotenv"
)

type Config struct {
Addr *string
Dsn *string
Addr *string
Dsn *string
TlsEnabled *bool
}

func init() {
Expand All @@ -30,12 +32,20 @@ func LoadConfig() *Config {
port := os.Getenv("PORT")
addrString := fmt.Sprintf(`%s:%s`, host, port)

tlsEnabledString := os.Getenv("TLS_ENABLED")
tlsEnabled, err := strconv.ParseBool(tlsEnabledString)
if err != nil {
tlsEnabled = true
}

addr := flag.String("addr", addrString, "HTTP network address")
dsn := flag.String("dsn", dbString, "PostgreSQL data source name")
tls := flag.Bool("tls", tlsEnabled, "Enable TLS")
flag.Parse()

return &Config{
Addr: addr,
Dsn: dsn,
Addr: addr,
Dsn: dsn,
TlsEnabled: tls,
}
}
1 change: 1 addition & 0 deletions internal/ui/assets/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ header.cdc-header .header-search {

.vh {
height: 50vh;
background-color: orange;
}

footer .row {
Expand Down
6 changes: 3 additions & 3 deletions internal/ui/components/base_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/ui/components/footer_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/ui/components/home.templ
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ templ Home() {
@Base() {
<h1>PHIN VADS Home</h1>
<div class="vh">
50 vh content
50 vh content foobar
</div>
}
}
2 changes: 1 addition & 1 deletion internal/ui/components/home_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 329cd33

Please sign in to comment.