Skip to content

Commit

Permalink
Fix fs path
Browse files Browse the repository at this point in the history
  • Loading branch information
dude333 committed Aug 8, 2021
1 parent 8f09b51 commit e0e9981
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
11 changes: 11 additions & 0 deletions server/fs_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
package server

import (
"io/fs"
"log"
"os"
)

var _fs = os.DirFS(".")
var _contentFS fs.FS

func init() {
var err error
_contentFS, err = fs.Sub(_fs, "templates")
if err != nil {
log.Fatal(err)
}
}
15 changes: 14 additions & 1 deletion server/fs_prod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@

package server

import "embed"
import (
"embed"
"io/fs"
"log"
)

//go:embed templates
var _fs embed.FS
var _contentFS fs.FS

func init() {
var err error
_contentFS, err = fs.Sub(_fs, "templates")
if err != nil {
log.Fatal(err)
}
}
73 changes: 44 additions & 29 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,14 @@ func initServer(opts ...ServerOption) (*Server, error) {
return &srv, nil
}

// HTML is a very basic html server to show the reports.
// HTML is a very basic html server to handle the reports.
func HTML(opts ...ServerOption) {
srv, err := initServer(opts...)
if err != nil {
log.Fatal(err)
}

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
serveTemplate(w, r, srv)
})
http.HandleFunc("/", renderTemplate(srv))

log.Println("Listening on :3000...")
err = http.ListenAndServe(":3000", nil)
Expand All @@ -90,43 +88,60 @@ func HTML(opts ...ServerOption) {
}
}

func serveTemplate(w http.ResponseWriter, r *http.Request, srv *Server) {
lp := filepath.Join("templates", "layout.html")
fp := filepath.Join("templates", filepath.Clean(r.URL.Path))
if fp == "templates" {
fp = "templates/index.html"
}
// renderTemplate renders the file related to the URL path inside the layout
// templates. Template files are locates in _contentFS.
func renderTemplate(srv *Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
fp := filepath.Clean(r.URL.Path)
if strings.HasPrefix(fp, `/`) || strings.HasPrefix(fp, `\`) {
fp = fp[1:] // remove starting "/" (or "\" on Windows)
}
if fp == "" {
fp = "index.html"
}

log.Println("fp:", fp)
log.Println("rendering", fp)

tmpl, err := template.New("").Funcs(template.FuncMap{
"ptFmtFloat": ptFmtFloat,
}).ParseFS(_fs, lp, fp)
if err != nil {
log.Println(err)
return
// TODO: load all templates outside this funcion
tmpl, err := template.New("").Funcs(template.FuncMap{
"ptFmtFloat": ptFmtFloat,
}).ParseFS(_contentFS, "layout.html", fp)

if err != nil {
log.Println(err)
return
}

// Set the payload according to the URL path
var payload interface{}
if strings.Contains(fp, "fii.html") && r.Method == http.MethodPost {
payload = payloadFIIDividends(srv, r)
}

err = tmpl.ExecuteTemplate(w, "layout", payload)
if err != nil {
log.Println(err)
}
}
}

func payloadFIIDividends(srv *Server, r *http.Request) interface{} {
var payload struct {
Codes string
Months int
Data interface{}
}
if strings.Contains(fp, "fii.html") && r.Method == http.MethodPost {
codes := parseCodes(r.FormValue("codes"))
months, err := strconv.Atoi(r.FormValue("months"))
if err != nil {
months = 1
}
payload.Codes = strings.Join(codes, " ")
payload.Months = months
payload.Data = fiiDividends(srv, codes, months)
}

err = tmpl.ExecuteTemplate(w, "layout", payload)
codes := parseCodes(r.FormValue("codes"))
months, err := strconv.Atoi(r.FormValue("months"))
if err != nil {
log.Println(err)
months = 1
}
payload.Codes = strings.Join(codes, " ")
payload.Months = months
payload.Data = fiiDividends(srv, codes, months)

return &payload
}

type data struct {
Expand Down

0 comments on commit e0e9981

Please sign in to comment.