1// Copyright 2014 Google Inc. All Rights Reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Handler for /static content. 16 17package static 18 19import ( 20 "fmt" 21 "mime" 22 "net/http" 23 "net/url" 24 "path" 25) 26 27const StaticResource = "/static/" 28 29var staticFiles = map[string]string{ 30 "containers.css": containersCss, 31 "containers.js": containersJs, 32 "bootstrap-3.1.1.min.css": bootstrapCss, 33 "bootstrap-theme-3.1.1.min.css": bootstrapThemeCss, 34 "jquery-1.10.2.min.js": jqueryJs, 35 "bootstrap-3.1.1.min.js": bootstrapJs, 36 "google-jsapi.js": googleJsapiJs, 37} 38 39func HandleRequest(w http.ResponseWriter, u *url.URL) error { 40 if len(u.Path) <= len(StaticResource) { 41 return fmt.Errorf("unknown static resource %q", u.Path) 42 } 43 44 // Get the static content if it exists. 45 resource := u.Path[len(StaticResource):] 46 content, ok := staticFiles[resource] 47 if !ok { 48 return fmt.Errorf("unknown static resource %q", resource) 49 } 50 51 // Set Content-Type if we were able to detect it. 52 contentType := mime.TypeByExtension(path.Ext(resource)) 53 if contentType != "" { 54 w.Header().Set("Content-Type", contentType) 55 } 56 57 _, err := w.Write([]byte(content)) 58 return err 59} 60