Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/rdp/sensible-cinema
Browse files Browse the repository at this point in the history
  • Loading branch information
rdp committed Dec 11, 2014
2 parents c39a9a1 + 140001c commit 6824601
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
3 changes: 3 additions & 0 deletions goweb/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ <h1>Editing {{.Title}}</h1>

<div><input type="submit" value="Save"></div>
</form>
<p>[<a href="/view/{{.Title}}">cancel</a>]</p>
<p>[<a href="/">index</a>]</p>

2 changes: 1 addition & 1 deletion goweb/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1>Index</h1>

{{range .}}
<p>[<a href="/view/{{.Title}}">{{.Title}}</a>] {{.Edl.UrlType}}</p>
<p>[<a href="/view/{{.Title}}">{{.Title}}</a>] {{.Edl.UrlType}} <a href="{{.Edl.UrlString}}">View</a></p>
{{end}}

<pre>
Expand Down
13 changes: 13 additions & 0 deletions goweb/random.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en" ng-app>
<head>
<meta charset="utf-8">
<title>Learning AngularJS</title>
</head>
<body>
<input type="number" ng-model="num1" />
<input type="number" ng-model="num2" />
<p>The sum is: {{ num1 + num2 }}</p>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions goweb/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ type EdlOld struct {
Skips []EditListEntry
}

type Page struct {
Title string
Body []byte
Edl* Edl
}


func (edl *Edl) UrlString() string {
urlString, _ := edl.UrlAndType()
return urlString
Expand Down
34 changes: 22 additions & 12 deletions goweb/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import ( "fmt"
"path"
)

type Page struct {
Title string
Body []byte
Edl* Edl
}

var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9- ]+)$") // security check

var DirName string = "editable_files"; // will be overwritten, can't assign nil?
Expand All @@ -35,10 +29,10 @@ func (p *Page) save() error {

func loadPage(title string) (*Page, error) {
filename := DirName + "/" + title + ".txt"
return loadPageFilename(filename)
return loadPageFromFilename(filename)
}

func loadPageFilename(filename string) (*Page, error) {
func loadPageFromFilename(filename string) (*Page, error) {
title := strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))
body, err := ioutil.ReadFile(filename)
if err != nil {
Expand Down Expand Up @@ -78,7 +72,7 @@ func editHandler(w http.ResponseWriter, r *http.Request, title string) {
empty.NetflixURL = movieurl
} else if strings.Contains(movieurl, "amazon.com") {
empty.AmazonURL = movieurl
} // else if strings.Contains(movieurl, 'play.google.com') { // youtube here too?
} // TODO else if strings.Contains(movieurl, 'play.google.com') { // youtube here too?

empty.Mutes = []EditListEntry{EditListEntry{}}
empty.Skips = []EditListEntry{EditListEntry{}}
Expand Down Expand Up @@ -116,7 +110,6 @@ func viewHandler(w http.ResponseWriter, r *http.Request, title string) {

}


func newHandler(w http.ResponseWriter, r *http.Request) {
moviename := r.URL.Query()["moviename"][0];
movieurl := r.URL.Query()["movieurl"][0];
Expand All @@ -136,8 +129,8 @@ func allPages() []*Page {
body, _:= ioutil.ReadFile(filename)
var edl Edl
edl.BytesToEdl(body)
page, _ := loadPageFilename(filename)
page.Edl = &edl
page, _ := loadPageFromFilename(filename)
page.Edl = &edl // only place we do this, for easy assocition purposes...
array2[i] = page
}
return array2
Expand All @@ -154,6 +147,17 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "index", allPages())
}

func redirectToMovieHandler(w http.ResponseWriter, r *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
http.NotFound(w, r) // not much here...
return
}
var edl Edl
edl.BytesToEdl(p.Body)
http.Redirect(w, r, edl.UrlString(), http.StatusFound)
}

func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
body := r.FormValue("body")
p := &Page{Title: title, Body: []byte(body)}
Expand Down Expand Up @@ -182,6 +186,10 @@ func helloWorldHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there")
}

func randomHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "random", allPages())
}

func main() {
if len(os.Args) > 1 {
Morph()
Expand All @@ -191,10 +199,12 @@ func main() {
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/redirect_to_movie/", makeHandler(redirectToMovieHandler))
http.HandleFunc("/", indexHandler)
http.HandleFunc("/search", searchHandler)
http.HandleFunc("/new", newHandler)
http.HandleFunc("/hello_world", helloWorldHandler)
http.HandleFunc("/random", randomHandler)
fmt.Println("serving on 8888")
http.ListenAndServe(":8888", nil)
fmt.Println("exiting")
Expand Down

0 comments on commit 6824601

Please sign in to comment.