Skip to content

Commit

Permalink
Merge pull request #15 from fgtago/dw-pc-home
Browse files Browse the repository at this point in the history
add default errorpage handler
  • Loading branch information
agungdhewe authored Jun 15, 2024
2 parents 97a14f4 + 028904c commit 4111acf
Show file tree
Hide file tree
Showing 24 changed files with 190 additions and 160 deletions.
3 changes: 2 additions & 1 deletion appsmodel/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ type Configuration struct {
Logging struct {
Enabled bool `yaml:"enabled"`
} `yaml:"logging"`
ConfigPath string
ShowServerError bool `yaml:"showservererror"`
ConfigPath string
}
6 changes: 4 additions & 2 deletions appsmodel/pagevariable.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package appsmodel

type PageVariable struct {
Title string
Data any
Title string
HttpErrorNumber int
HttpErrorMessage string
Data any
}
1 change: 1 addition & 0 deletions appsmodel/webservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Webservice struct {
AllowedAsset map[string]*[]string
CurrentWsDir string
ApplicationData any
ShowServerError bool
}

var ws *Webservice
Expand Down
75 changes: 75 additions & 0 deletions defaulthandlers/errorpagehandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package defaulthandlers

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

"github.com/agungdhewe/dwlog"
"github.com/fgtago/fgweb/appsmodel"
)

func ErrorPageHandler(errnum int, errmsg string, pv *appsmodel.PageVariable, w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ws := appsmodel.GetWebservice()
device := ctx.Value(appsmodel.DeviceKeyName).(appsmodel.Device)

pv.Title = "Error"
pv.HttpErrorNumber = errnum
pv.HttpErrorMessage = errmsg

// TODO: implmentasikan tpl
tpl, exists, err := ws.TplMgr.GetPage("errorpage", device.Type)
if err != nil {
dwlog.Error(err.Error())
simpleErrorPage(errnum, errmsg, w)
return
}

if !exists {
simpleErrorPage(errnum, errmsg, w)
return
}

// render page
buff := new(bytes.Buffer)
err = tpl.Execute(buff, &pv)
if err != nil {
dwlog.Error(err.Error())
simpleErrorPage(errnum, errmsg, w)
return
}

// send bufer to browser
w.WriteHeader(errnum)
_, err = buff.WriteTo(w)
if err != nil {
dwlog.Error(err.Error())
simpleErrorPage(errnum, errmsg, w)
return
}

}

func simpleErrorPage(errnum int, errmsg string, w http.ResponseWriter) {
w.WriteHeader(errnum)

html := `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Error</title>
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<h1>Error %d</h1>
<p>%s</p>
</body>
</html>
`

fmt.Fprintf(w, html, errnum, errmsg)
}
47 changes: 0 additions & 47 deletions defaulthandlers/pagehomehandler.go

This file was deleted.

104 changes: 0 additions & 104 deletions defaulthandlers/pageloginhandler.go

This file was deleted.

59 changes: 59 additions & 0 deletions defaulthandlers/simplepagehandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package defaulthandlers

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

"github.com/agungdhewe/dwlog"
"github.com/fgtago/fgweb/appsmodel"
)

func SimplePageHandler(pagename string, pv *appsmodel.PageVariable, w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ws := appsmodel.GetWebservice()
device := ctx.Value(appsmodel.DeviceKeyName).(appsmodel.Device)

// TODO: implmentasikan tpl
tpl, exists, err := ws.TplMgr.GetPage(pagename, device.Type)
if err != nil {
if ws.ShowServerError {
ErrorPageHandler(500, err.Error(), pv, w, r)
} else {
dwlog.Error(err.Error())
ErrorPageHandler(500, "internal server error", pv, w, r)
}
return
}

if !exists {
// error 404
ErrorPageHandler(404, fmt.Sprintf("page %s not found", pagename), pv, w, r)
return
}

// render page
buff := new(bytes.Buffer)
err = tpl.Execute(buff, &pv)
if err != nil {
if ws.ShowServerError {
ErrorPageHandler(500, err.Error(), pv, w, r)
} else {
dwlog.Error(err.Error())
ErrorPageHandler(500, "internal server error", pv, w, r)
}
return
}

// send bufer to browser
_, err = buff.WriteTo(w)
if err != nil {
if ws.ShowServerError {
ErrorPageHandler(500, err.Error(), pv, w, r)
} else {
dwlog.Error(err.Error())
ErrorPageHandler(500, "internal server error", pv, w, r)
}
return
}
}
7 changes: 7 additions & 0 deletions fgweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ func httpRequestHandler(hnd RouteHandlerFunc) *chi.Mux {
// handle dari main program
hnd(mux)

// kalau halaman tidak ditemukan
mux.NotFound(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pv := ctx.Value(appsmodel.PageVariableKeyName).(*appsmodel.PageVariable)
defaulthandlers.ErrorPageHandler(404, "page not found", pv, w, r)
})

return mux
}

Expand Down
6 changes: 3 additions & 3 deletions main/config-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ cookie:

template:
cached: false
dir: "../data/templates/v2"
dir: "data/templates/v2"
options:
- missingkey=error

application:
pagedir: "../data/pages"
contentdir: "../data/content"
pagedir: "data/pages"
contentdir: "data/content"

logging:
enabled: true
Expand Down
12 changes: 12 additions & 0 deletions main/data/pages/errorpage/errorpage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{template "layoutdasar" .}}

{{define "pagestyle"}}
{{end}}


{{define "pagecontent"}}
<div>
<h1>Error {{.HttpErrorNumber}}</h1>
{{.HttpErrorMessage}}
</div>
{{end}}
10 changes: 10 additions & 0 deletions main/data/pages/errorpage/errorpage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: Home

device:
mobile:
- errorpage.html
tablet:
- errorpage.html
desktop:
- errorpage.html

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.
Loading

0 comments on commit 4111acf

Please sign in to comment.