Procházet zdrojové kódy

add homepage template

raylu před 14 roky
rodič
revize
8a05f63c5c
5 změnil soubory, kde provedl 95 přidání a 91 odebrání
  1. 38 48
      main.go
  2. 3 0
      templates/footer.html
  3. 13 0
      templates/header.html
  4. 10 0
      templates/home.html
  5. 31 43
      templates/p.html

+ 38 - 48
main.go

@@ -19,41 +19,57 @@ type Playlist struct {
 	Id string
 }
 
-var templates map[string]*template.Template
+var templates *template.Set
 const debug = true
 
-func home(w http.ResponseWriter, r *http.Request) {
-	fmt.Fprintf(w, "Welcome to Audio Axis!")
-}
+func main() {
+	templates = template.SetMust(template.ParseTemplateFiles("templates/*.html"))
 
-func playlist(w http.ResponseWriter, r *http.Request) {
-	id := r.URL.Path[len("/p/"):]
-	if len(id) != 8 {
-		http.Redirect(w, r, "/", 303)
-		return
+	initDb()
+
+	http.HandleFunc("/", home)
+	http.HandleFunc("/p/", playlist)
+	http.HandleFunc("/add/", add)
+	http.HandleFunc("/remove/", remove)
+	http.HandleFunc("/move/", move)
+	http.HandleFunc("/poll/", poll)
+	err := http.ListenAndServe("localhost:8000", nil)
+	if err != nil {
+		fmt.Println(err)
+		os.Exit(1)
 	}
-	playlist := Playlist{Id: id}
+}
 
+func renderPage(w http.ResponseWriter, page string, data interface{}) {
 	if debug {
-		t, err := template.ParseFile("templates/p.html")
-		if err != nil {
-			http.Error(w, err.String(), http.StatusInternalServerError)
-			return
-		}
-		err = t.Execute(w, playlist)
+		var err os.Error
+		templates, err = template.ParseTemplateFiles("templates/*.html")
 		if err != nil {
-			w.Write([]byte(err.String()))
 			fmt.Fprintln(os.Stderr, err.String())
+			http.Error(w, err.String(), http.StatusInternalServerError)
 			return
 		}
-	} else {
-		err := templates["p"].Execute(w, playlist)
-		if err != nil {
-			fmt.Fprintln(os.Stderr, err.String())
-		}
+	}
+	err := templates.Execute(w, page + ".html", data)
+	if err != nil {
+		fmt.Fprintln(os.Stderr, err.String())
 	}
 }
 
+func home(w http.ResponseWriter, r *http.Request) {
+	renderPage(w, "home", nil)
+}
+
+func playlist(w http.ResponseWriter, r *http.Request) {
+	id := r.URL.Path[len("/p/"):]
+	if len(id) != 8 {
+		http.Redirect(w, r, "/", 303)
+		return
+	}
+	p := Playlist{Id: id}
+	renderPage(w, "p", p)
+}
+
 func add(w http.ResponseWriter, r *http.Request) {
 	q := r.URL.Query()
 	db := <-dbPool
@@ -283,29 +299,3 @@ func poll(w http.ResponseWriter, r *http.Request) {
 		w.Write([]byte("[]"))
 	}
 }
-
-func main() {
-	templates = make(map[string]*template.Template)
-	for _, path := range []string{"p"} {
-		t, err := template.ParseFile("templates/" + path + ".html")
-		if err != nil {
-			fmt.Println(err)
-			return
-		}
-		templates[path] = t
-	}
-
-	initDb()
-
-	http.HandleFunc("/", home)
-	http.HandleFunc("/p/", playlist)
-	http.HandleFunc("/add/", add)
-	http.HandleFunc("/remove/", remove)
-	http.HandleFunc("/move/", move)
-	http.HandleFunc("/poll/", poll)
-	err := http.ListenAndServe("localhost:8000", nil)
-	if err != nil {
-		fmt.Println(err)
-		os.Exit(1)
-	}
-}

+ 3 - 0
templates/footer.html

@@ -0,0 +1,3 @@
+</body>
+
+</html>

+ 13 - 0
templates/header.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+<head>
+	<meta charset="UTF-8">
+	<title>Audio Axis</title>
+	<!--
+	<link href='http://fonts.googleapis.com/css?family=Cantarell' rel='stylesheet'>
+	-->
+	<link href="/static/cantarell.css" rel="stylesheet">
+	<link href="/static/base.css" rel="stylesheet">
+</head>
+
+<body>

+ 10 - 0
templates/home.html

@@ -0,0 +1,10 @@
+{{template "header.html"}}
+
+<header>
+	<img src="/static/audioaxis-white.png" alt="Audio Axis" class="aa">
+</header>
+<article>
+	Welcome to Audio Axis!
+</article>
+
+{{template "footer.html"}}

+ 31 - 43
templates/p.html

@@ -1,45 +1,33 @@
-<!DOCTYPE html>
-<html>
-<head>
-	<meta charset="UTF-8">
-	<title>Audio Axis</title>
-	<!--
-	<link href='http://fonts.googleapis.com/css?family=Cantarell' rel='stylesheet'>
-	-->
-	<link href="/static/cantarell.css" rel="stylesheet">
-	<link href="/static/base.css" rel="stylesheet">
-</head>
+{{template "header.html"}}
 
-<body>
-	<header>
-		<img src="/static/audioaxis-white.png" alt="Audio Axis" class="aa">
-		<form onsubmit="return search(this.song.value)">
-			<label for="user">Username:</label>
-			<input type="text" id="user" value="anonymous" size="16">
-			<br>
-			<label for="song">Add a song:</label>
-			<input type="text" id="song" autocomplete="off">
-			<input type="submit" value="Search">
-		</form>
-		<div id="results" onblur="$(this).slideUp()"><ul>
-		</ul></div>
-	</header>
-	<article>
-	</article>
-	{{$first := "82KEVKGRAhI"}}
-	<script>
-		var pid = '{{.Id}}';
-	</script>
-	<script src="/static/jquery.min.js"></script>
-	<!--
-	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
-	-->
-	<script src="/static/script.js"></script>
-	<object data="http://www.youtube.com/v/{{$first}}?version=3&amp;enablejsapi=1" id="player" width="1" height="1" type="application/x-shockwave-flash">
-		<param name="movie" value="http://www.youtube.com/v/{{$first}}?version=3&amp;enablejsapi=1">
-		<param name="allowScriptAccess" value="always">
-		<embed src="http://www.youtube.com/v/{{$first}}?version=3&amp;enablejsapi=1" allowScriptAccess="always">
-	</object>
-</body>
+<header>
+	<img src="/static/audioaxis-white.png" alt="Audio Axis" class="aa">
+	<form onsubmit="return search(this.song.value)">
+		<label for="user">Username:</label>
+		<input type="text" id="user" value="anonymous" size="16">
+		<br>
+		<label for="song">Add a song:</label>
+		<input type="text" id="song" autocomplete="off">
+		<input type="submit" value="Search">
+	</form>
+	<div id="results" onblur="$(this).slideUp()"><ul>
+	</ul></div>
+</header>
+<article>
+</article>
+{{$first := "82KEVKGRAhI"}}
+<script>
+	var pid = '{{.Id}}';
+</script>
+<script src="/static/jquery.min.js"></script>
+<!--
+<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
+-->
+<script src="/static/script.js"></script>
+<object data="http://www.youtube.com/v/{{$first}}?version=3&amp;enablejsapi=1" id="player" width="1" height="1" type="application/x-shockwave-flash">
+	<param name="movie" value="http://www.youtube.com/v/{{$first}}?version=3&amp;enablejsapi=1">
+	<param name="allowScriptAccess" value="always">
+	<embed src="http://www.youtube.com/v/{{$first}}?version=3&amp;enablejsapi=1" allowScriptAccess="always">
+</object>
 
-</html>
+{{template "footer.html"}}