Skip to content

Commit

Permalink
feat: setup template engine
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jan 20, 2019
1 parent 5406800 commit 0e3179f
Show file tree
Hide file tree
Showing 37 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/golang/protobuf v1.2.0
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
github.com/gorilla/handlers v1.4.0
github.com/gorilla/mux v1.6.2
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190104160321-4832df01553a
github.com/grpc-ecosystem/grpc-gateway v1.6.4
github.com/jtolds/gls v4.2.1+incompatible // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORR
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/handlers v1.4.0 h1:XulKRWSQK5uChr4pEgSE4Tc/OcmnU9GJuSwdog/tZsA=
github.com/gorilla/handlers v1.4.0/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.6.2 h1:Pgr17XVTNXAk3q/r4CpKzC5xBM/qW1uVLV+IhRZpIIk=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/pat v0.0.0-20180118222023-199c85a7f6d1/go.mod h1:YeAe0gNeiNT5hoiZRI4yiOky6jVdNvfO2N6Kav/HmxY=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
Expand Down
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gobuffalo/packr"
"github.com/gogo/gateway"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
Expand All @@ -26,6 +27,7 @@ import (

"ultre.me/calcbiz/api"
"ultre.me/calcbiz/svc"
"ultre.me/calcbiz/views"
)

// VERSION represents the version of the Camembert au lait crew's website
Expand Down Expand Up @@ -140,11 +142,14 @@ func startHTTPServer(ctx context.Context, opts *serverOptions) error {
return err
}
zap.L().Info("starting HTTP server", zap.String("bind", opts.HTTPBind))
router := mux.NewRouter()
views.Setup(router)
box := packr.NewBox("./static")
router.PathPrefix("/").Handler(http.FileServer(box))
mux := http.NewServeMux()
mux.Handle("/api/", gwmux)

box := packr.NewBox("./static")
mux.Handle("/", http.FileServer(box))
mux.Handle("/", router)
// FIXME: handle 404

handler := handlers.LoggingHandler(os.Stderr, mux)
handler = handlers.RecoveryHandler(handlers.PrintRecoveryStack(true))(handler)
Expand Down
1 change: 0 additions & 1 deletion static/index.html

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions templates/templates/home.html → templates/home.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<p>HELLO FROM home.html</p>

<p>Hello {{.hello}}</p>

{{/*
{% extends "base.html" %}
{# set page_title = 'Coucou' #}
{% set columns = 4 %}
Expand Down Expand Up @@ -31,3 +36,4 @@
</div>
<p class="well">Et plein d'autres..</p>
{% endblock %}
*/}}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 39 additions & 0 deletions views/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package views

import (
"bytes"
"fmt"
"html/template"
"net/http"

"github.com/gobuffalo/packr"
"go.uber.org/zap"
)

var box = packr.NewBox("../templates")

func setDefaultHeaders(w http.ResponseWriter) {
// push(w, "/static/xxx.css")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}

func renderError(w http.ResponseWriter, r *http.Request, err error) {
zap.L().Warn("rendering error", zap.Error(err))
fmt.Fprintf(w, "Error: %v\n", err)
}

func render(w http.ResponseWriter, r *http.Request, tplPath string, data interface{}) {
tplFile, err := box.FindString(tplPath)
if err != nil {
renderError(w, r, err)
return
}
tpl := template.Must(template.New(tplPath).Parse(tplFile))

buf := new(bytes.Buffer)
if err := tpl.ExecuteTemplate(buf, tplPath, data); err != nil {
renderError(w, r, err)
return
}
w.Write(buf.Bytes())
}
19 changes: 19 additions & 0 deletions views/views.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package views

import (
"net/http"

"github.com/gorilla/mux"
)

func Setup(router *mux.Router) {
router.HandleFunc("/", homeHandler)
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
setDefaultHeaders(w)
data := map[string]interface{}{
"hello": "world",
}
render(w, r, "home.html", data)
}

0 comments on commit 0e3179f

Please sign in to comment.