bidet / internal / handlers / style.go

@ 797c7a407b8fa943b19b8241e470451b21fde72d | history


package handlers

import (
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
)

func getCSS(loc string) []byte {
	if loc == "" {
		return nil
	}
	if u, err := url.Parse(loc); err == nil && (u.Scheme == "http" || u.Scheme == "https") {
		if resp, err := http.Get(loc); err == nil {
			defer resp.Body.Close()
			if resp.StatusCode == http.StatusOK {
				if data, err := io.ReadAll(resp.Body); err == nil {
					return data
				}
			}
		}
		fmt.Printf("could not find stylesheet at %s; using default\n", loc)
		return nil
	}

	if data, err := os.ReadFile(loc); err == nil {
		return data
	}
	fmt.Printf("error reading stylesheet at %s; using default\n", loc)
	return nil
}