-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add handler to the route * Move to students directory * fix template format * Update redirect to template execution
- Loading branch information
1 parent
d78996c
commit c897e0f
Showing
6 changed files
with
162 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"gopherex/cyoa/students/cherednichenkoa/route-handler" | ||
"gopherex/cyoa/students/cherednichenkoa/settings" | ||
) | ||
|
||
const ( | ||
storyTemplate = "cherednichenkoa/templates/story.html" | ||
) | ||
|
||
var ( | ||
filePath = flag.String("filePath","","path to the story source file") | ||
listenPort = flag.String("listenPort","","port number that app will listen") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
if len(*listenPort) == 0 || len(*filePath) == 0 { | ||
panic("Please specify application params (listenPort and filePath)") | ||
} | ||
config := settings.Settings{FilePath: *filePath, ListenPort: *listenPort, TemplatePath: storyTemplate} | ||
handler := route_handler.RouteHandler{Settings: config} | ||
handler.ServeRequests() | ||
} |
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,52 @@ | ||
package route_handler | ||
|
||
import ( | ||
"gopherex/cyoa/students/cherednichenkoa/settings" | ||
"gopherex/cyoa/students/cherednichenkoa/source" | ||
"html/template" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
const ( | ||
defaultStory = "intro" | ||
) | ||
|
||
type RouteHandler struct { | ||
Settings settings.Settings | ||
} | ||
|
||
func (rh *RouteHandler) ServeRequests() { | ||
fileHandler := source.JsonFileHandler{Settings: rh.Settings} | ||
fileContent, err := fileHandler.GetFileContent() | ||
if err != nil { | ||
panic(err) | ||
} | ||
urlHandler := rh.getMapHandler(fileContent) | ||
http.HandleFunc("/", urlHandler) | ||
http.ListenAndServe(rh.getPort(), nil) | ||
} | ||
|
||
func (rh *RouteHandler) getMapHandler(stories map[string]source.StoryDetails) http.HandlerFunc { | ||
return func(w http.ResponseWriter, req *http.Request) { | ||
tmpl := template.Must(template.ParseFiles(rh.Settings.GetTemplatePath())) | ||
path := rh.prepareUrl(req) | ||
story, ok := stories[path] | ||
if ok { | ||
tmpl.Execute(w, story) | ||
return | ||
} | ||
|
||
tmpl.Execute(w, stories[defaultStory]) | ||
} | ||
} | ||
|
||
func (rh *RouteHandler) getPort() string { | ||
port := ":" + rh.Settings.GetListenPort() | ||
return port | ||
} | ||
|
||
func (rh *RouteHandler) prepareUrl(req *http.Request) string { | ||
path := strings.Trim(req.URL.Path,"/") | ||
return path | ||
} |
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,19 @@ | ||
package settings | ||
|
||
type Settings struct { | ||
FilePath string | ||
ListenPort string | ||
TemplatePath string | ||
} | ||
|
||
func (conf *Settings) GetFilePath() string { | ||
return conf.FilePath | ||
} | ||
|
||
func (conf *Settings) GetListenPort() string { | ||
return conf.ListenPort | ||
} | ||
|
||
func (conf *Settings) GetTemplatePath() string { | ||
return conf.TemplatePath | ||
} |
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,36 @@ | ||
package source | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"gopherex/cyoa/students/cherednichenkoa/settings" | ||
"io/ioutil" | ||
) | ||
|
||
type JsonFileHandler struct { | ||
Settings settings.Settings | ||
} | ||
|
||
func (fh *JsonFileHandler) GetFileContent () (map[string]StoryDetails, error) { | ||
file, err := ioutil.ReadFile(fh.Settings.GetFilePath()) | ||
if err != nil { | ||
fmt.Println("Error during json file reading.") | ||
panic(err) | ||
} | ||
var out map[string]StoryDetails | ||
if err := json.Unmarshal(file, &out); err != nil { | ||
return nil, err | ||
} | ||
return out, nil | ||
} | ||
|
||
type StoryOption struct { | ||
Text string `json:"text"` | ||
Arc string `json:"arc"` | ||
} | ||
|
||
type StoryDetails struct { | ||
Title string `json:"title"` | ||
Story []string `json:"story"` | ||
Options []StoryOption `json:"options"` | ||
} |
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,28 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>{{.Title}}</title> | ||
</head> | ||
<body> | ||
<h1>{{.Title}}</h1> | ||
<div class="story-content"> | ||
{{range .Story}} | ||
<p>{{.}}</p> | ||
{{end}} | ||
</div> | ||
<div class="variations-links"> | ||
{{if .Options }} | ||
<ul> | ||
{{range .Options}} | ||
<li class="link-container"> | ||
<a href="{{.Arc}}">{{.Text}}</a> | ||
</li> | ||
{{end}} | ||
</ul> | ||
{{else}} | ||
<p ><b>The End !</b></p> | ||
{{end}} | ||
</div> | ||
</body> | ||
</html> |