2017-05-11 4 views
0

私は明らかに何かを見落としてしまったと確信していますが、わかりません。私は書籍のテンプレートページを提供するシンプルなWebアプリケーションを構築しています。テンプレートは正常に動作し、画像のパスは正しく表示されているようですが、画像自体に404エラーが発生します。ここGolangのWebサーバが静的ファイルを提供していない

テンプレートである:

<h1>{{.Title}}</h1> 
<h2>{{.Author.Name}}</h2> 
<image src="../images/{{.ImageURI}}" /> 

、ここでは、アプリケーション自体である:

package main 
import (
    "html/template" 
    "log" 
    "net/http" 
    "time" 

    "github.com/gorilla/mux" 
    "github.com/user/marketplace/typelibrary" 
) 

var books []typelibrary.Book 

func ItemHandler(w http.ResponseWriter, r *http.Request) { 
    params := mux.Vars(r) 
    var selected typelibrary.Book  
    //Retrieve item data 
    for _, item := range books { 
     if item.ID == params["id"] { 
      selected = item 
      break 
     } 
    } 
    t, _ := template.ParseFiles("./templates/book.html") 
    t.Execute(w, selected) 
} 

func main() { 
    router := mux.NewRouter() 
    books = append(books, typelibrary.Book{ID: "1", Title: "The Fellowship of the Ring", ImageURI: "LotR-FotR.jpg", Author: &typelibrary.Author{Name: "JRR Tolkien"}}) 
    books = append(books, typelibrary.Book{ID: "2", Title: "The Two Towers", ImageURI: "LotR-tTT.jpg", Author: &typelibrary.Author{Name: "JRR Tolkien"}}) 
    books = append(books, typelibrary.Book{ID: "3", Title: "The Return of the King", ImageURI: "LotR-RotK.jpg", Author: &typelibrary.Author{Name: "JRR Tolkien"}}) 
    books = append(books, typelibrary.Book{ID: "4", Title: "Monster Hunter International", ImageURI: "MHI1.jpg", Author: &typelibrary.Author{Name: "Larry Correia"}}) 

    router.Handle("/", http.FileServer(http.Dir("."))) 
    router.Handle("/images/", http.FileServer(http.Dir("../images/"))) 
    router.HandleFunc("/item/{id}", ItemHandler).Methods("GET") 

    srv := &http.Server{ 
     Handler:  router, 
     Addr:   ":8080", 
     WriteTimeout: 10 * time.Second, 
     ReadTimeout: 10 * time.Second, 
    } 
    log.Fatal(srv.ListenAndServe()) 
} 

画像が直接実行可能ファイルがあるディレクトリの下に、imagesサブディレクトリに格納されています。ページで壊れた画像を表示しようとすると、パスはlocalhost:8080/images/[imagename]と表示されますが、404エラーが表示されます。ここで欠落している設定やルーティングオプションは何ですか?

+0

ほとんどの場合、間違ったパスを 'http.Dir'に渡しています。なぜ.. ../'?サーバーをサブディレクトリから実行していますか?あなたの記述に基づいて '。/ images /'がほしいと思うでしょう。 – Flimzy

+0

@Flimzy正しいですが、正しく '。/ images /'にする必要がありますが、どちらの方法も問題はあります。 – FreeRangeOyster

+2

ディレクトリ構造とサーバーの起動方法はわかりませんが、これらの回答にはソリューションが含まれています:[404ページが見つかりません - レンダリングCSSファイル](http://stackoverflow.com/questions/28293452/404-page -not-found-go-rendering-css-file/28294524#28294524); [なぜ静的ファイルにアクセスするためにhttp.StripPrefixを使用する必要がありますか?](http://stackoverflow.com/questions/27945310/why-do-i-need-to-use-http-stripprefix-to-access -my-static-files/27946132#27946132) – icza

答えて

3

あなたの画像を配信するために経路を間違って作成しています。 Router.Handle()メソッドは、パス全体に一致するPath()マッチャでURLをマッチさせますが、実際には "/ image /"で始まるパスに一致させる必要があります。その代わり、PathPrefix()マッチャーでルートを作成します。

var imgServer = http.FileServer(http.Dir("./images/")) 
router.PathPrefix("/images/").Handler(http.StripPrefix("/images/", imgServer)) 

は、より多くの情報のためhttps://godoc.org/github.com/gorilla/mux#Router.Handleを参照してください。

関連する問題