123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- package utils
- import (
- "bufio"
- "encoding/base64"
- "errors"
- "io"
- "log"
- "net/http"
- "os"
- "strings"
- "time"
- )
- func SendTextResponse(w http.ResponseWriter, msg string) {
- w.Write([]byte(msg))
- }
- func SendJSONResponse(w http.ResponseWriter, json string) {
- w.Header().Set("Content-Type", "application/json")
- w.Write([]byte(json))
- }
- func SendErrorResponse(w http.ResponseWriter, errMsg string) {
- w.Header().Set("Content-Type", "application/json")
- w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))
- }
- func SendOK(w http.ResponseWriter) {
- w.Header().Set("Content-Type", "application/json")
- w.Write([]byte("\"OK\""))
- }
- func GetPara(r *http.Request, key string) (string, error) {
- keys, ok := r.URL.Query()[key]
- if !ok || len(keys[0]) < 1 {
- return "", errors.New("invalid " + key + " given")
- } else {
- return keys[0], nil
- }
- }
- func PostPara(r *http.Request, key string) (string, error) {
- r.ParseForm()
- x := r.Form.Get(key)
- if x == "" {
- return "", errors.New("invalid " + key + " given")
- } else {
- return x, nil
- }
- }
- func FileExists(filename string) bool {
- _, err := os.Stat(filename)
- if os.IsNotExist(err) {
- return false
- }
- return true
- }
- func IsDir(path string) bool {
- if FileExists(path) == false {
- return false
- }
- fi, err := os.Stat(path)
- if err != nil {
- log.Fatal(err)
- return false
- }
- switch mode := fi.Mode(); {
- case mode.IsDir():
- return true
- case mode.IsRegular():
- return false
- }
- return false
- }
- func TimeToString(targetTime time.Time) string {
- return targetTime.Format("2006-01-02 15:04:05")
- }
- func LoadImageAsBase64(filepath string) (string, error) {
- if !FileExists(filepath) {
- return "", errors.New("File not exists")
- }
- f, _ := os.Open(filepath)
- reader := bufio.NewReader(f)
- content, _ := io.ReadAll(reader)
- encoded := base64.StdEncoding.EncodeToString(content)
- return string(encoded), nil
- }
- func ConstructRelativePathFromRequestURL(requestURI string, redirectionLocation string) string {
- if strings.Count(requestURI, "/") == 1 {
-
- return redirectionLocation
- }
- for i := 0; i < strings.Count(requestURI, "/")-1; i++ {
- redirectionLocation = "../" + redirectionLocation
- }
- return redirectionLocation
- }
- func StringInArray(arr []string, str string) bool {
- for _, a := range arr {
- if a == str {
- return true
- }
- }
- return false
- }
- func StringInArrayIgnoreCase(arr []string, str string) bool {
- smallArray := []string{}
- for _, item := range arr {
- smallArray = append(smallArray, strings.ToLower(item))
- }
- return StringInArray(smallArray, strings.ToLower(str))
- }
|