1
0
Fork 0

Logging with zap and increase modular split

main
Ambrose Chua 2020-11-08 17:00:23 +08:00
parent 68b01a0cba
commit dfaaa0ef4c
11 changed files with 249 additions and 68 deletions

View File

@ -6,7 +6,7 @@ RUN apk add \
WORKDIR /go/src/app
COPY . .
RUN make
RUN make TAGS=production
FROM alpine:3.12

View File

@ -17,7 +17,7 @@ clean:
build: datetime
datetime: *.go
$(GO) build -v -o datetime
$(GO) build -tags "$(TAGS)" -v -o datetime
.PHONY: test
test:

95
app.go Normal file
View File

@ -0,0 +1,95 @@
package main
import (
"html/template"
"net/http"
"github.com/serverwentdown/datetime.link/data"
"go.uber.org/zap"
)
// Datetime is the main application server
type Datetime struct {
*http.ServeMux
tmpl *template.Template
cities map[string]*data.City
}
// NewDatetime creates an application instance. It assumes certain resources
// like templates and data exist.
func NewDatetime() (*Datetime, error) {
// Data
tmpl, err := template.ParseGlob("templates/*")
if err != nil {
return nil, err
}
cities, err := data.ReadCities()
if err != nil {
return nil, err
}
// Mux
mux := http.NewServeMux()
app := &Datetime{mux, tmpl, cities}
// Routes
mux.Handle("/data/", http.FileServer(http.Dir(".")))
mux.Handle("/js/", http.FileServer(http.Dir("assets")))
mux.Handle("/css/", http.FileServer(http.Dir("assets")))
mux.Handle("/favicon.ico", http.FileServer(http.Dir("assets")))
mux.HandleFunc("/", app.index)
return app, nil
}
// index handles all incoming page requests
func (app Datetime) index(w http.ResponseWriter, req *http.Request) {
var err error
if req.Method != http.MethodGet && req.Method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
accept := req.Header.Get("Accept")
tmpl, acceptable := app.chooseTemplate(accept)
if !acceptable {
w.WriteHeader(http.StatusNotAcceptable)
return
}
if tmpl == nil {
l.Error("unable to find template", zap.String("accept", accept))
w.WriteHeader(http.StatusInternalServerError)
return
}
if req.Method == http.MethodHead {
return
}
l.Debug("", zap.Reflect("url", req.URL))
err = tmpl.Execute(w, nil)
if err != nil {
l.Error("templating failed", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError) // Usually this will fail
return
}
}
// chooseTemplate returns a template based on the accepted mime types from the
// client, and if a template cannot be found it returns a nil template.
func (app Datetime) chooseTemplate(accept string) (t *template.Template, acceptable bool) {
responseType := chooseResponseType(accept)
templateName := ""
switch responseType {
case responsePlain:
templateName = "index.txt"
case responseHTML:
templateName = "index.html"
case responseAny:
templateName = "index.txt"
case responseUnknown:
return nil, false
}
return app.tmpl.Lookup(templateName), true
}

38
app_test.go Normal file
View File

@ -0,0 +1,38 @@
package main
import (
"fmt"
"html/template"
"testing"
)
func TestChooseTemplate(t *testing.T) {
tmpl, err := template.ParseGlob("templates/*")
if err != nil {
t.Errorf("Unable to load templates: %v", err)
}
app := &Datetime{tmpl: tmpl}
type chooseTest struct {
accept string
acceptable bool
template string
}
tests := []chooseTest{
chooseTest{"text/html", true, "index.html"},
chooseTest{"text/html;q=0.9,text/plain", true, "index.txt"},
chooseTest{"image/png", false, ""},
chooseTest{"*/*", true, "index.txt"},
}
for _, test := range tests {
tmpl, acceptable := app.chooseTemplate(test.accept)
fn := fmt.Sprintf("chooseTemplate(\"%s\")", test.accept)
if acceptable != test.acceptable {
t.Errorf("%s; acceptable = %v; wanted %v", fn, acceptable, test.acceptable)
}
if tmpl != app.tmpl.Lookup(test.template) {
t.Errorf("%s; tmpl = %v; wanted template for %v", fn, tmpl.Name(), test.template)
}
}
}

22
data/data.go Normal file
View File

@ -0,0 +1,22 @@
package data
import (
"encoding/json"
"io/ioutil"
)
// ReadCities opens the file "data/cities.json" and reads it into a map
func ReadCities() (map[string]*City, error) {
cities := make(map[string]*City)
buf, err := ioutil.ReadFile("data/cities.json")
if err != nil {
return nil, err
}
err = json.Unmarshal(buf, &cities)
if err != nil {
return nil, err
}
return cities, nil
}

View File

@ -1,11 +1,6 @@
package data
// Data is all the data needed to map cities to timezones
type Data struct {
Cities map[string]*City
}
// City represents a city that belongs inside an administrative division level 1
// city represents a city that belongs inside an administrative division level 1
// and a country
type City struct {
// Ref is the ASCII name of the city

5
go.mod
View File

@ -2,4 +2,7 @@ module github.com/serverwentdown/datetime.link
go 1.14
require github.com/hbollon/go-edlib v1.3.1
require (
github.com/hbollon/go-edlib v1.3.1
go.uber.org/zap v1.16.0
)

42
go.sum
View File

@ -1,2 +1,44 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/hbollon/go-edlib v1.3.1 h1:3x2Faq1xbShKhel5wEYyCNZFguh+s8GH75jdp8w6phU=
github.com/hbollon/go-edlib v1.3.1/go.mod h1:wnt6o6EIVEzUfgbUZY7BerzQ2uvzp354qmS2xaLkrhM=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=

11
logger_development.go Normal file
View File

@ -0,0 +1,11 @@
// +build !production
package main
import (
"go.uber.org/zap"
)
func zapConfig() zap.Config {
return zap.NewDevelopmentConfig()
}

11
logger_production.go Normal file
View File

@ -0,0 +1,11 @@
// +build production
package main
import (
"go.uber.org/zap"
)
func zapConfig() zap.Config {
return zap.NewProductionConfig()
}

82
main.go
View File

@ -2,83 +2,47 @@ package main
import (
"flag"
"html/template"
"log"
"net/http"
"time"
"go.uber.org/zap"
)
var listen string
var tmpl *template.Template
var l *zap.Logger
func main() {
var err error
flag.StringVar(&listen, "listen", ":8000", "Listen address")
level := zap.LevelFlag("log", zap.InfoLevel, "zap logging level")
flag.Parse()
server := &http.Server{
Addr: listen,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
tmpl, err = template.ParseGlob("templates/*")
config := zapConfig()
config.Level = zap.NewAtomicLevelAt(*level)
l, err = config.Build()
if err != nil {
panic(err)
log.Fatalf("failed to initialize zap logger: %v", err)
}
defer l.Sync()
app, err := NewDatetime()
if err != nil {
l.Panic("failed to create datetime", zap.Error(err))
}
http.Handle("/data/", http.FileServer(http.Dir(".")))
http.Handle("/js/", http.FileServer(http.Dir("assets")))
http.Handle("/css/", http.FileServer(http.Dir("assets")))
http.Handle("/favicon.ico", http.FileServer(http.Dir("assets")))
http.HandleFunc("/", index)
server := &http.Server{
Addr: listen,
Handler: app,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Printf("Listening on %v", server.Addr)
l.Info("server is listening", zap.String("addr", listen))
err = server.ListenAndServe()
if err != nil {
panic(err)
}
}
func index(w http.ResponseWriter, req *http.Request) {
var err error
if req.Method != http.MethodGet && req.Method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
accept := req.Header.Get("Accept")
responseType := chooseResponseType(accept)
templateName := ""
switch responseType {
case responsePlain:
templateName = "index.txt"
case responseHTML:
templateName = "index.html"
case responseAny:
templateName = "index.txt"
case responseUnknown:
w.WriteHeader(http.StatusNotAcceptable)
return
}
t := tmpl.Lookup(templateName)
if t == nil {
log.Printf("Unable to find index template")
w.WriteHeader(http.StatusInternalServerError)
return
}
if req.Method == http.MethodHead {
return
}
err = t.Execute(w, nil)
if err != nil {
log.Printf("Error: %v", err)
// Usually, the following will fail
w.WriteHeader(http.StatusInternalServerError)
return
l.Panic("server failed", zap.Error(err))
}
}