Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting template to properly read CSS file in Golang

Tags:

html

css

go

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.

like image 567
j5juice Avatar asked Nov 29 '25 14:11

j5juice


1 Answers

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!

like image 155
j5juice Avatar answered Dec 01 '25 03:12

j5juice