43388

Go: serve CSS files with gorilla mux

Question:

I have this directory structure and I'm using Gorilla mux:

Directory structure

twitter layout stylesheets log.css log.html twitter.go

Following the advice here: <a href="http://www.shakedos.com/2014/Feb/08/serving-static-files-with-go.html" rel="nofollow">http://www.shakedos.com/2014/Feb/08/serving-static-files-with-go.html</a> I did this:

var router = mux.NewRouter() func ServeStatic(router *mux.Router, staticDirectory string) { staticPaths := map[string]string{ "styles": staticDirectory + "stylesheets", } for pathName, pathValue := range staticPaths { pathPrefix := "/" + pathName + "/" router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix, http.FileServer(http.Dir(pathValue)))) } } var staticDirectory = "/layout/" func main() { (//other code) ServeStatic(router, staticDirectory) }

Still I can't link the CSS file. What am I doing wrong?

Answer1:

Resolved.

I added this in func main()

router.PathPrefix("/").Handler(http.FileServer(http.Dir("./layout/")))

Answer2:

You can do this in a easier way without adding the extra line in main():

inside ServeStatic: add this: "."+ before pathValue

router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix, http.FileServer(http.Dir("."/pathValue))))

Recommend

  • Restify static webserver stops working after enabling HTTP basic authentication
  • Apache graceful restart - Does it affect current PHP script execution
  • Why does Perl's IO::Socket on Windows complain about “Resource Not Available” after 64 connecti
  • Open a Configuration Profile through app
  • Heroku R10 Boot Timeout Error
  • Socket Lose Connection
  • angular2 rc3 router alpha 3.0.0.7 default route
  • How to initialize widget in component Vue?
  • Post request Axios : Network error
  • Angular 2 router auxiliary routes not working on redirectTo
  • toastr undefined with requirejs
  • What is the difference between sap.ui.core.routing.Router.navTo() and sap.m.routing.Targets.display(
  • Angular 2: TypeError: Cannot read property 'isRouteActive' of undefined in
  • multiple colors on beanplot in R
  • Trying to find the last non-empty cell in a specific row/range over multiple sheets
  • Durandal Modal Won't Close
  • The C Programming Language K&R exercise 1- 9
  • AWS Elastic Beanstalk - changing owner of webapp folder
  • How to show different validation messages for email validation in Angular2 using Validators class?
  • angular.js ui-route how catch route or url or params after reject?
  • How to generate Date Series in HIVE? (Creating table)
  • why route of backbone needs the default route
  • How to unit test a Angular 4 component which uses router.paramMap
  • Webpack-dev-server and isomorphic react-node application
  • does jqgrid support a multiple checkbox list for editing
  • Update CALayer sublayers immediately
  • Optimizing database types to compact database (SQLite)
  • Cross-Platform Protobuf Serialization
  • sending/ receiving email in Java
  • Do I've to free mysql result after storing it?
  • Is there a mandatory requirement to switch app.yaml?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • Turn off referential integrity in Derby? is it possible?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • JaxB to read class hierarchy
  • Checking variable from a different class in C#
  • Busy indicator not showing up in wpf window [duplicate]