Skip to content

Instantly share code, notes, and snippets.

@rhoboro
Last active Nov 22, 2015
Embed
What would you like to do?
golangで複数ディレクトリから静的ファイルを配信する
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap-theme.min.css">
</head>
<header>
<style>
.theme-showcase {
margin-top: 30px;
}
</style>
</header>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" id="navbar"></nav>
<div id="root" class="container"></div>
<script src="/jquery/dist/jquery.min.js"></script>
<script src="/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="/app.js"></script>
</body>
</html>
package main
import (
"net/http"
)
func main() {
// http://localhost:8080/bootstrap/* が来たときのみ./bower_components/bootstrap/*を配信する
bfs := http.FileServer(http.Dir("./bower_components/"))
http.Handle("/bootstrap/", bfs)
// http://localhost:8080/jquery/* が来たときのみ./bower_components/jquery/*を配信する
jfs := http.FileServer(http.Dir("./bower_components/"))
http.Handle("/jquery/", jfs)
// 上記以外の静的ファイルの配信はこっち
fs := http.FileServer(http.Dir("./static/"))
http.Handle("/", fs)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment