Skip to content

Commit

Permalink
Solution for the exercise (#9)
Browse files Browse the repository at this point in the history
* Add handler to the route

* Move to students directory

* fix template format

* Update redirect to template execution
  • Loading branch information
cherednichenkoa authored and joncalhoun committed Jul 22, 2019
1 parent d78996c commit c897e0f
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

tmp/
.idea/
26 changes: 26 additions & 0 deletions students/cherednichenkoa/main.go
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()
}
52 changes: 52 additions & 0 deletions students/cherednichenkoa/route-handler/route_handler.go
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
}
19 changes: 19 additions & 0 deletions students/cherednichenkoa/settings/settings.go
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
}
36 changes: 36 additions & 0 deletions students/cherednichenkoa/source/json_file.go
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"`
}
28 changes: 28 additions & 0 deletions students/cherednichenkoa/templates/story.html
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>

0 comments on commit c897e0f

Please sign in to comment.