Skip to content

Commit

Permalink
feat: servers use PORT variable when it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Jul 9, 2024
1 parent b921bde commit 3021bde
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 15 deletions.
8 changes: 4 additions & 4 deletions bulk-update/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"bytes"
"cmp"
"context"
"database/sql"
"embed"
"html/template"
"log"
"net/http"
"os"
"strconv"

"github.com/crhntr/httplog"
Expand Down Expand Up @@ -35,7 +37,7 @@ func main() {
mux := server.routes()
h := httplog.Wrap(mux)
log.Println("starting server")
log.Fatal(http.ListenAndServe(":8080", h))
log.Fatal(http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), ":8080"), h))
}

func must[T any](value T, err error) T {
Expand Down Expand Up @@ -98,9 +100,7 @@ func (server *Server) setActiveStatus(res http.ResponseWriter, req *http.Request
}

func (server *Server) writePage(res http.ResponseWriter, _ *http.Request, name string, status int, data any) {
var (
buf bytes.Buffer
)
var buf bytes.Buffer
err := server.templates.ExecuteTemplate(&buf, name, data)
if err != nil {
log.Println(err)
Expand Down
4 changes: 3 additions & 1 deletion click-to-edit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"cmp"
"context"
"database/sql"
_ "embed"
Expand All @@ -10,6 +11,7 @@ import (
"html/template"
"log"
"net/http"
"os"
"strconv"

"github.com/crhntr/httplog"
Expand All @@ -29,7 +31,7 @@ func main() {
server := newServer(database.New(db))
h := httplog.Wrap(server.routes())
log.Println("starting server")
log.Fatal(http.ListenAndServe(":8080", h))
log.Fatal(http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), ":8080"), h))
}

func must[T any](value T, err error) T {
Expand Down
4 changes: 3 additions & 1 deletion click-to-load/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"bytes"
"cmp"
_ "embed"
"html/template"
"math/rand"
"net/http"
"os"
)

//go:embed index.html.template
Expand All @@ -29,7 +31,7 @@ func main() {
res.WriteHeader(http.StatusOK)
_, _ = res.Write(buf.Bytes())
})
_ = http.ListenAndServe(":8080", nil)
_ = http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), ":8080"), nil)
}

func fillWithRandomNumbers(values []int) {
Expand Down
4 changes: 3 additions & 1 deletion delete-row/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"bytes"
"cmp"
_ "embed"
"encoding/json"
"html/template"
"log"
"net/http"
"os"
"path"
"slices"
"strconv"
Expand Down Expand Up @@ -59,7 +61,7 @@ func main() {
res.WriteHeader(http.StatusOK)
_, _ = res.Write(buf.Bytes())
})
log.Fatal(http.ListenAndServe(":8080", nil))
log.Fatal(http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), ":8080"), nil))
}

type Page struct {
Expand Down
4 changes: 3 additions & 1 deletion edit-row/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"bytes"
"cmp"
_ "embed"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"slices"
"strconv"
"sync"
Expand All @@ -31,7 +33,7 @@ func main() {
mux.HandleFunc("GET /", server.index)
mux.HandleFunc("GET /edit/{index}", server.getEdit)
mux.HandleFunc("POST /edit/{index}", server.postEdit)
log.Fatal(http.ListenAndServe(":8080", mux))
log.Fatal(http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), ":8080"), mux))
}

type server struct {
Expand Down
4 changes: 3 additions & 1 deletion infinite-scroll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"bytes"
"cmp"
_ "embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"strconv"
)

Expand Down Expand Up @@ -39,7 +41,7 @@ func main() {
NextURL: contactsURL(page + 1),
})
})
log.Fatal(http.ListenAndServe(":8080", mux))
log.Fatal(http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), ":8080"), mux))
}

func contactsURL(page int) string {
Expand Down
4 changes: 3 additions & 1 deletion inline-validation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"bytes"
"cmp"
"context"
_ "embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"strconv"
"time"

Expand Down Expand Up @@ -179,7 +181,7 @@ func main() {
}
})

log.Fatal(http.ListenAndServe(":8080", mux))
log.Fatal(http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), ":8080"), mux))
}

func render(res http.ResponseWriter, _ *http.Request, templates *template.Template, code int, name string, data any) {
Expand Down
4 changes: 3 additions & 1 deletion lazy-loading/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package main

import (
"cmp"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)

func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", indexHandler)
mux.HandleFunc("/endpoint", endpointHandler)
log.Fatal(http.ListenAndServe(":8080", mux))
log.Fatal(http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), ":8080"), mux))
}

func indexHandler(res http.ResponseWriter, _ *http.Request) {
Expand Down
9 changes: 5 additions & 4 deletions spreadsheet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"cmp"
_ "embed"
"encoding/json"
"flag"
Expand All @@ -10,6 +11,7 @@ import (
"io"
"log"
"net/http"
"os"
"regexp"
"slices"
"strconv"
Expand All @@ -31,7 +33,7 @@ func main() {
templates: template.Must(template.New("index.html.template").Parse(indexHTMLTemplate)),
}
log.Println("starting server")
log.Fatal(http.ListenAndServe(":8080", s.routes()))
log.Fatal(http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), ":8080"), s.routes()))
}

type server struct {
Expand Down Expand Up @@ -325,6 +327,7 @@ func (cell *Cell) String() string {
func (cell *Cell) IDPathParam() string {
return fmt.Sprintf("%s%d", columnLabel(cell.Column), cell.Row)
}

func (cell *Cell) ID() string {
return "cell-" + cell.IDPathParam()
}
Expand Down Expand Up @@ -573,9 +576,7 @@ func (node ParenNode) String() string {
}

func parse(tokens []Token, i, maxColumn, maxRow int) (ExpressionNode, int, error) {
var (
stack []ExpressionNode
)
var stack []ExpressionNode
for {
result, consumed, err := parseNodes(stack, tokens, i, maxColumn, maxRow)
if err != nil {
Expand Down

0 comments on commit 3021bde

Please sign in to comment.