3

Hello I am having an issue with getting my template to properly display my CSS file. It is reading my imgs correctly, but anything in my CSS file does not get displayed correctly. When I run through with Delve it gets the path correctly so I am unsure what is going on. Here is my code.

package main

import (
        "bufio"
        "log"
        "net/http"
        "os"
        "strings"
        "text/template"
)

func main() {
        templates := populateTemplates()

        http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
                requestedFile := req.URL.Path[1:]
                template := templates.Lookup(requestedFile + ".html")

                if template != nil {
                        template.Execute(w, nil)
                } else {
                        w.WriteHeader(404)
                }
        })

        http.HandleFunc("/img/", serveResource)
        http.HandleFunc("/css/", serveResource)

        http.ListenAndServe(":8080", nil)
}

func serveResource(w http.ResponseWriter, req *http.Request) {
        path := "../../public" + req.URL.Path
        var contentType string
        if strings.HasSuffix(path, ".css") {
                contentType = "text/css"
        } else if strings.HasSuffix(path, ".png") {
                contentType = "image/png"
        } else {
                contentType = "text/plain"
        }

        f, err := os.Open(path)
        if err != nil {
                w.WriteHeader(404)
        } else {
                defer f.Close()
                w.Header().Add("Content Type", contentType)

                br := bufio.NewReader(f)
                br.WriteTo(w)
        }
}

func populateTemplates() *template.Template {
        result := template.New("templates")

        basePath := "../../templates"
        templateFolder, err := os.Open(basePath)
        if err != nil {
                log.Fatal(err)
        }
        defer templateFolder.Close()

        templatePathsRaw, _ := templateFolder.Readdir(-1)

        templatePaths := new([]string)
        for _, pathInfo := range templatePathsRaw {
                if !pathInfo.IsDir() {
                        *templatePaths = append(*templatePaths, basePath+"/"+pathInfo.Name())
                }
        }

        result.ParseFiles(*templatePaths...)

        return result
}

(also on http://pastebin.com/7Vcm5t75)

I have a main folder with bin, pkg, src, public, and templates in it. Then in src is a main folder that houses main.go. Public contains img, css, scripts. Img has my images in it. CSS has my CSS file in it. Then templates has my two html pages in it. Shown in tree form below:

├── bin
├── pkg
├── public
│   ├── css
│   ├── img
│   └── scripts
├── src
│   └── main
└── templates

Any help is greatly appreciated.

6
  • Please list your folder with tree command. Commented Nov 30, 2015 at 1:32
  • Added that to original post Commented Nov 30, 2015 at 1:52
  • 5
    As a tip: replace your serveResource function with http.ServeFile and/or look at the docs for http.FileServer - which will do what you want to. Commented Nov 30, 2015 at 2:04
  • Thank you that worked much better! Commented Nov 30, 2015 at 2:22
  • Is this problem solved? Commented Nov 30, 2015 at 7:56

1 Answer 1

3

Used the tip above from elithrar and changed my serveResource func to read like this:

func serveResource(w http.ResponseWriter, req *http.Request) {
    path := "../../public" + req.URL.Path
    http.ServeFile(w, req, path)
}

My issue is now resolved. Thank you!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.