music-library/utils.go

51 lines
931 B
Go
Raw Normal View History

2023-02-14 21:59:56 +01:00
package main
import (
"log"
"net/http"
"os"
"strings"
)
func MethodNotAllowed(w http.ResponseWriter, allowedMethods ...string) {
for _, method := range allowedMethods {
w.Header().Add("Allow", strings.ToUpper(method))
}
w.WriteHeader(http.StatusMethodNotAllowed)
}
func InternalServerError(w http.ResponseWriter, err error) {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
}
func And[T any](f1, f2 func(T) bool) func(T) bool {
return func(v T) bool {
return f1(v) && f2(v)
}
}
func Or[T any](f1, f2 func(T) bool) func(T) bool {
return func(v T) bool {
return f1(v) || f2(v)
}
}
func Not[T any](f func(T) bool) func(T) bool {
return func(v T) bool {
return !f(v)
}
}
func IsDir(entry os.DirEntry) bool {
return entry.IsDir()
}
func IsHidden(entry os.DirEntry) bool {
return strings.HasPrefix(entry.Name(), ".")
}
func FsEntry2Name(entry os.DirEntry) string {
return entry.Name()
}