{{.}}
+ {{end}} +diff --git a/.gitignore b/.gitignore index 73fd1e4..aa0bd68 100644 --- a/.gitignore +++ b/.gitignore @@ -12,5 +12,5 @@ # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 .glide/ - tmp/ +.idea/ diff --git a/students/cherednichenkoa/main.go b/students/cherednichenkoa/main.go new file mode 100644 index 0000000..be7a89f --- /dev/null +++ b/students/cherednichenkoa/main.go @@ -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() +} \ No newline at end of file diff --git a/students/cherednichenkoa/route-handler/route_handler.go b/students/cherednichenkoa/route-handler/route_handler.go new file mode 100644 index 0000000..b859a6a --- /dev/null +++ b/students/cherednichenkoa/route-handler/route_handler.go @@ -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 +} \ No newline at end of file diff --git a/students/cherednichenkoa/settings/settings.go b/students/cherednichenkoa/settings/settings.go new file mode 100644 index 0000000..3b18717 --- /dev/null +++ b/students/cherednichenkoa/settings/settings.go @@ -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 +} \ No newline at end of file diff --git a/students/cherednichenkoa/source/json_file.go b/students/cherednichenkoa/source/json_file.go new file mode 100644 index 0000000..465a260 --- /dev/null +++ b/students/cherednichenkoa/source/json_file.go @@ -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"` +} diff --git a/students/cherednichenkoa/templates/story.html b/students/cherednichenkoa/templates/story.html new file mode 100644 index 0000000..05e1e94 --- /dev/null +++ b/students/cherednichenkoa/templates/story.html @@ -0,0 +1,28 @@ + + +
+ +{{.}}
+ {{end}} +The End !
+ {{end}} +