-
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.
- Loading branch information
1 parent
92d051f
commit 028904c
Showing
12 changed files
with
190 additions
and
160 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
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package appsmodel | ||
|
||
type PageVariable struct { | ||
Title string | ||
Data any | ||
Title string | ||
HttpErrorNumber int | ||
HttpErrorMessage string | ||
Data any | ||
} |
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
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,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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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 | ||
} | ||
} |
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
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
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,12 @@ | ||
{{template "layoutdasar" .}} | ||
|
||
{{define "pagestyle"}} | ||
{{end}} | ||
|
||
|
||
{{define "pagecontent"}} | ||
<div> | ||
<h1>Error {{.HttpErrorNumber}}</h1> | ||
{{.HttpErrorMessage}} | ||
</div> | ||
{{end}} |
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,10 @@ | ||
title: Home | ||
|
||
device: | ||
mobile: | ||
- errorpage.html | ||
tablet: | ||
- errorpage.html | ||
desktop: | ||
- errorpage.html | ||
|
Oops, something went wrong.