-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: html server and template wrapper
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package html | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-chi/chi/v5" | ||
) | ||
|
||
// Server creates route for routing requests for static files | ||
func Server(router *chi.Mux, folder string) { | ||
fs := http.FileServer(http.Dir("./" + folder)) | ||
handler := http.StripPrefix("/"+folder+"/", fs) | ||
|
||
router.Get("/"+folder+"/*", handler.ServeHTTP) | ||
} |
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,68 @@ | ||
package html | ||
|
||
import ( | ||
"bytes" | ||
jsonslow "encoding/json" | ||
"html/template" | ||
"net/http" | ||
|
||
"github.com/noxecane/anansi" | ||
"github.com/noxecane/anansi/json" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
type Template interface { | ||
Render(*http.Request, http.ResponseWriter, any) error | ||
} | ||
|
||
type htmlTemplate struct { | ||
name string | ||
tmpl *template.Template | ||
} | ||
|
||
func Parse(name string, source ...string) Template { | ||
var tmpl *template.Template | ||
if len(source) == 1 { | ||
tmpl = template.Must(template.New(name).ParseFiles(source...)) | ||
} else if len(source) > 0 { | ||
tmpl = template.Must(template.ParseFiles(source...)) | ||
} else { | ||
panic("souce can't be empty") | ||
} | ||
|
||
return &htmlTemplate{name, tmpl} | ||
} | ||
|
||
func (t *htmlTemplate) Render(r *http.Request, w http.ResponseWriter, data any) error { | ||
log := zerolog.Ctx(r.Context()) | ||
raw, err := json.Marshal(data) | ||
|
||
if data != nil { | ||
log.UpdateContext(func(ctx zerolog.Context) zerolog.Context { | ||
buffer := new(bytes.Buffer) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := jsonslow.Compact(buffer, raw); err != nil { | ||
panic(err) | ||
} | ||
|
||
return ctx.RawJSON("html_data", buffer.Bytes()) | ||
}) | ||
} | ||
|
||
err = t.tmpl.ExecuteTemplate(w, t.name, data) | ||
|
||
if err == nil { | ||
log.Info(). | ||
Interface("response_headers", anansi.SimpleHeaders(w.Header())). | ||
Msg("") | ||
} else { | ||
log.Err(err). | ||
Interface("response_headers", anansi.SimpleHeaders(w.Header())). | ||
Msg("") | ||
} | ||
|
||
return err | ||
} |