fix: themes bug

This commit is contained in:
NhanPT 2023-02-19 14:03:50 +07:00
parent e76a47f98a
commit e23b7d6b65
10 changed files with 90 additions and 56 deletions

View file

@ -1,6 +1,8 @@
package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
@ -13,17 +15,27 @@ import (
func main() {
prepare()
mux := http.NewServeMux()
modules.SetupLanguage()
modules.SetupTemplate()
modules.SetupWeather()
modules.SetupLanguage(mux)
modules.SetupTemplate(mux)
modules.SetupWeather(mux)
//loadAddons()
log.Println("Listening on http://localhost:7001 ...")
err := http.ListenAndServe(":7001", nil)
if err != nil {
log.Fatal(err)
server := http.Server{
Addr: fmt.Sprintf(":%d", 7001),
Handler: mux,
}
log.Println("Listening on http://localhost:7001 ...")
if err := server.ListenAndServe(); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
fmt.Printf("error running http server: %s\n", err)
}
}
// err := http.ListenAndServe(":7001", nil)
// if err != nil {
// log.Fatal(err)
// }
}
func prepare() {

View file

@ -2,7 +2,8 @@ package modules
import (
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
@ -45,7 +46,13 @@ func LoadConfig() {
}
AppConfig = defaultConfig
yamlFile, err := ioutil.ReadFile(filepath.Join("data", "config.yaml"))
file, err := os.Open(filepath.Join("data", "config.yaml"))
if err != nil {
fmt.Printf("failed reading file: %s", err)
return
}
defer file.Close()
yamlFile, err := io.ReadAll(file)
if err != nil {
fmt.Printf("Error reading YAML file: %s\n", err)
return

View file

@ -3,7 +3,8 @@ package modules
import (
"fmt"
"index/suffixarray"
"io/ioutil"
"io"
"os"
"path/filepath"
"regexp"
"strings"
@ -38,7 +39,13 @@ type BookmarkData struct {
func LoadContent() ContentData {
emptyData := ContentData{}
yamlFile, err := ioutil.ReadFile(filepath.Join("data", "data.yaml"))
file, err := os.Open(filepath.Join("data", "data.yaml"))
if err != nil {
fmt.Printf("failed reading file: %s", err)
return emptyData
}
defer file.Close()
yamlFile, err := io.ReadAll(file)
if err != nil {
fmt.Printf("Error reading YAML file: %s\n", err)
return emptyData

View file

@ -28,7 +28,6 @@ func CopyFile(source string, dest string) (err error) {
if err != nil {
return err
}
defer sourcefile.Close()
destfile, err := os.Create(dest)
@ -42,7 +41,7 @@ func CopyFile(source string, dest string) (err error) {
if err == nil {
sourceinfo, err := os.Stat(source)
if err != nil {
err = os.Chmod(dest, sourceinfo.Mode())
_ = os.Chmod(dest, sourceinfo.Mode())
}
}

View file

@ -2,16 +2,17 @@ package modules
import (
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
func SetupLanguage() {
func SetupLanguage(mux *http.ServeMux) {
languagefs := http.FileServer(http.Dir(filepath.Join(CurrentPath(), "languages")))
http.Handle("/languages/", http.StripPrefix("/languages/", languagefs))
mux.Handle("/languages/", http.StripPrefix("/languages/", languagefs))
}
type Language struct {
@ -42,7 +43,13 @@ type LanguageWeather struct {
}
func LoadLanguage(language string) Language {
yamlFile, err := ioutil.ReadFile(filepath.Join("languages", language+".yaml"))
file, err := os.Open(filepath.Join("languages", language+".yaml"))
if err != nil {
fmt.Printf("failed reading file: %s", err)
return LoadLanguage("en")
}
defer file.Close()
yamlFile, err := io.ReadAll(file)
if err != nil {
fmt.Printf("Error reading YAML file: %s\n", err)
return LoadLanguage("en")

View file

@ -1,6 +1,7 @@
package modules
import (
"fmt"
"log"
"net/http"
"path"
@ -11,17 +12,17 @@ import (
"github.com/fsnotify/fsnotify"
)
func SetupTemplate() {
func SetupTemplate(mux *http.ServeMux) {
loadData()
go watchChanges()
commonfs := http.FileServer(http.Dir(filepath.Join(pwd, "data")))
http.Handle("/common/", http.StripPrefix("/common/", commonfs))
commonfs := http.FileServer(http.Dir(filepath.Join(CurrentPath(), "data")))
mux.Handle("/common/", http.StripPrefix("/common/", commonfs))
th := themeHandler{}
http.Handle("/theme/", th)
mux.Handle("/theme/", th)
http.HandleFunc("/", serveTemplate)
mux.HandleFunc("/", serveTemplate)
}
var websiteData = struct {
@ -39,6 +40,7 @@ func loadData() {
websiteData.Config = AppConfig.Website
websiteData.Language = LoadLanguage(AppConfig.Website.Language)
websiteData.Contents = LoadContent().Data
loadTemplate()
// Download icon to local and remove local ressources access
for groupIndex := 0; groupIndex < len(websiteData.Contents); groupIndex++ {
@ -58,19 +60,17 @@ func loadData() {
//download icon
if bookmarkData.IsImage() || bookmarkData.IsSVG() {
fileName := path.Base(iconPath)
if DownloadFile(iconPath, filepath.Join(pwd, "data", "icon", fileName)) {
if DownloadFile(iconPath, filepath.Join(CurrentPath(), "data", "icon", fileName)) {
iconPath = "/common/icon/" + fileName
}
}
//add to private array
if bookmarkData.IsLocal || len(bookmarkData.UrlLocal) > 0 {
url := bookmarkData.Url
if len(bookmarkData.UrlLocal) > 0 {
url = bookmarkData.UrlLocal
}
columnDataPrivate = append(columnDataPrivate, BookmarkData{bookmarkData.Name, url, bookmarkData.UrlLocal, iconPath, true})
url := bookmarkData.Url
if len(bookmarkData.UrlLocal) > 0 {
url = bookmarkData.UrlLocal
}
columnDataPrivate = append(columnDataPrivate, BookmarkData{bookmarkData.Name, url, bookmarkData.UrlLocal, iconPath, true})
//add to public array
if !bookmarkData.IsLocal && len(bookmarkData.Url) > 0 {
@ -93,8 +93,6 @@ func loadData() {
privateContent = append(privateContent, GroupData{group.Title, groupDataPrivate, group.Icon})
}
}
loadTemplate()
}
var themeDir string
@ -104,17 +102,20 @@ type themeHandler struct {
}
func (th themeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, strings.Replace(r.URL.Path, "/theme", themeDir, 1))
newPath := strings.Replace(r.URL.Path, "/theme", themeDir, 1)
fmt.Println(newPath)
http.ServeFile(w, r, newPath)
}
func loadTemplate() {
themeDir = filepath.Join(pwd, "themes", AppConfig.Website.Theme)
themeDir = filepath.Join(CurrentPath(), "themes", AppConfig.Website.Theme)
tmpl, _ := template.ParseFiles(filepath.Join(themeDir, "index.html"))
webTemplate = tmpl
}
func serveTemplate(w http.ResponseWriter, r *http.Request) {
if ClientIsLocal(r) {
websiteData.IsLocal = ClientIsLocal(r)
if websiteData.IsLocal {
websiteData.Contents = privateContent
} else {
websiteData.Contents = publicContent
@ -126,6 +127,7 @@ func watchChanges() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
return
}
defer watcher.Close()

View file

@ -1,14 +1,14 @@
package modules
import (
"io/ioutil"
"io"
"log"
"net/http"
"time"
)
func SetupWeather() {
http.HandleFunc("/weather", serveWeather)
func SetupWeather(mux *http.ServeMux) {
mux.HandleFunc("/weather", serveWeather)
}
func serveWeather(w http.ResponseWriter, r *http.Request) {
@ -33,7 +33,7 @@ func GetWeather(apiKey string, latitude string, longitude string) []byte {
log.Fatalln(err)
return nil
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
return nil

View file

@ -91,8 +91,8 @@
<script src="/common/js/core.js"></script>
<script>
window.config = {
localization: {{.Config.Localization}},
language: {{.Config.Language}},
localization: '{{.Config.Localization}}',
language: '{{.Config.Language}}',
useMetric: {{.Config.UseMetric}}
};
@ -107,16 +107,16 @@
const greeting = document.querySelector("#greeting");
const hour = new Date().getHours();
if (hour >= 5 && hour < 12) {
greeting.innerText = {{.Language.Greeting.Morning}};
greeting.innerText = '{{.Language.Greeting.Morning}}'
}
else if (hour >= 12 && hour < 17) {
greeting.innerText = {{.Language.Greeting.Afternoon}};
greeting.innerText = '{{.Language.Greeting.Afternoon}}'
}
else if (hour >= 17 && hour < 20) {
greeting.innerText = {{.Language.Greeting.Evening}};
greeting.innerText = '{{.Language.Greeting.Evening}}'
}
else {
greeting.innerText = {{.Language.Greeting.Night}};
greeting.innerText = '{{.Language.Greeting.Night}}'
}
}) ();

View file

@ -90,8 +90,8 @@
<script src="/common/js/core.js"></script>
<script>
window.config = {
localization: {{.Config.Localization}},
language: {{.Config.Language}},
localization: '{{.Config.Localization}}',
language: '{{.Config.Language}}',
useMetric: {{.Config.UseMetric}}
};
@ -109,16 +109,16 @@
const greeting = document.querySelector("#greeting");
const hour = new Date().getHours();
if (hour >= 5 && hour < 12) {
greeting.innerText = {{.Language.Greeting.Morning}};
greeting.innerText = '{{.Language.Greeting.Morning}}'
}
else if (hour >= 12 && hour < 17) {
greeting.innerText = {{.Language.Greeting.Afternoon}};
greeting.innerText = '{{.Language.Greeting.Afternoon}}'
}
else if (hour >= 17 && hour < 20) {
greeting.innerText = {{.Language.Greeting.Evening}};
greeting.innerText = '{{.Language.Greeting.Evening}}'
}
else {
greeting.innerText = {{.Language.Greeting.Night}};
greeting.innerText = '{{.Language.Greeting.Night}}'
}
}) ();

View file

@ -44,24 +44,24 @@
<script src="/common/js/core.js"></script>
<script>
window.config = {
localization: {{.Config.Localization}},
language: {{.Config.Language}},
localization: '{{.Config.Localization}}',
language: '{{.Config.Language}}',
useMetric: {{.Config.UseMetric}}
};
(function setTimer() {
const greeting = document.querySelector("#greeting");
const hour = new Date().getHours();
if (hour >= 5 && hour < 12) {
greeting.innerText = {{.Language.Greeting.Morning}};
greeting.innerText = '{{.Language.Greeting.Morning}}'
}
else if (hour >= 12 && hour < 17) {
greeting.innerText = {{.Language.Greeting.Afternoon}};
greeting.innerText = '{{.Language.Greeting.Afternoon}}'
}
else if (hour >= 17 && hour < 20) {
greeting.innerText = {{.Language.Greeting.Evening}};
greeting.innerText = '{{.Language.Greeting.Evening}}'
}
else {
greeting.innerText = {{.Language.Greeting.Night}};
greeting.innerText = '{{.Language.Greeting.Night}}'
}
}) ();
</script>