NhanPT 3 years ago
parent
commit
da3b3ea0dc
9 changed files with 126 additions and 136 deletions
  1. 4 15
      src/go.mod
  2. 9 46
      src/go.sum
  3. 13 7
      src/main.go
  4. 46 0
      src/modules/compile.go
  5. 49 39
      src/modules/config.go
  6. 0 25
      src/modules/config.ts
  7. 0 4
      src/modules/file.ts
  8. 5 0
      src/modules/server.go
  9. 0 0
      src/private/themes/custom/index.html

+ 4 - 15
src/go.mod

@@ -2,20 +2,9 @@ module github.com/help-14/magma
 
 go 1.18
 
+require gopkg.in/yaml.v2 v2.4.0
+
 require (
-	github.com/fsnotify/fsnotify v1.5.1 // indirect
-	github.com/hashicorp/hcl v1.0.0 // indirect
-	github.com/magiconair/properties v1.8.5 // indirect
-	github.com/mitchellh/mapstructure v1.4.3 // indirect
-	github.com/pelletier/go-toml v1.9.4 // indirect
-	github.com/spf13/afero v1.6.0 // indirect
-	github.com/spf13/cast v1.4.1 // indirect
-	github.com/spf13/jwalterweatherman v1.1.0 // indirect
-	github.com/spf13/pflag v1.0.5 // indirect
-	github.com/spf13/viper v1.10.1 // indirect
-	github.com/subosito/gotenv v1.2.0 // indirect
-	golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
-	golang.org/x/text v0.3.7 // indirect
-	gopkg.in/ini.v1 v1.66.2 // indirect
-	gopkg.in/yaml.v2 v2.4.0 // indirect
+	github.com/tdewolff/minify/v2 v2.10.0 // indirect
+	github.com/tdewolff/parse/v2 v2.5.27 // indirect
 )

+ 9 - 46
src/go.sum

@@ -1,52 +1,15 @@
-github.com/creasty/defaults v1.5.2 h1:/VfB6uxpyp6h0fr7SPp7n8WJBoV8jfxQXPCnkVSjyls=
-github.com/creasty/defaults v1.5.2/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY=
-github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
+github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U=
+github.com/djherbis/atime v1.1.0/go.mod h1:28OF6Y8s3NQWwacXc5eZTsEsiMzp7LF8MbXE+XJPdBE=
+github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
 github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
-github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
-github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
-github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
-github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
-github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
-github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
-github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
-github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
-github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
-github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
-github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
-github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
-github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY=
-github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
-github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
-github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
-github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
-github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
-github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
+github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=
 github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
-github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk=
-github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU=
-github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
-github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
-github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
-github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
-github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
-golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
-golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
-golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
-golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+github.com/tdewolff/minify/v2 v2.10.0 h1:ovVAHUcjfGrBDf1EIvsodRUVJiZK/28mMose08B7k14=
+github.com/tdewolff/minify/v2 v2.10.0/go.mod h1:6XAjcHM46pFcRE0eztigFPm0Q+Cxsw8YhEWT+rDkcZM=
+github.com/tdewolff/parse/v2 v2.5.27 h1:PL3LzzXaOpmdrknnOlIeO2muIBHAwiKp6TxN1RbU5gI=
+github.com/tdewolff/parse/v2 v2.5.27/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho=
+github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
 golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.0.0-20211210111614-af8b64212486 h1:5hpz5aRr+W1erYCL5JRhSUBJRph7l9XkNveoExlrKYk=
-golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
-golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
-golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
-golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
-golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI=
-gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
-gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
 gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

+ 13 - 7
src/main.go

@@ -4,21 +4,27 @@ import (
 	"html/template"
 	"log"
 	"net/http"
+	"os"
 	"path/filepath"
 
-	config "github.com/help-14/magma/modules"
+	modules "github.com/help-14/magma/modules"
 )
 
 func main() {
-	//fs := http.FileServer(http.Dir("./temp"))
-	//http.Handle("/static/", http.StripPrefix("/static/", fs))
-	//http.Handle("/", fs)
-	//http.HandleFunc("/", serveTemplate)
+	tempPath := filepath.Join(".", "temp")
+	err := os.MkdirAll(tempPath, os.ModePerm)
+	if err != nil {
+		log.Fatal(err)
+		return
+	}
+
+	fs := http.FileServer(http.Dir("./temp"))
+	http.Handle("/static/", http.StripPrefix("/static/", fs))
 
-	config.LoadConfig()
+	modules.LoadConfig()
 
 	log.Println("Listening on :7001 ...")
-	err := http.ListenAndServe(":7001", nil)
+	err = http.ListenAndServe(":7001", nil)
 	if err != nil {
 		log.Fatal(err)
 	}

+ 46 - 0
src/modules/compile.go

@@ -0,0 +1,46 @@
+package modules
+
+import (
+	"fmt"
+	"io/ioutil"
+	"regexp"
+
+	"github.com/tdewolff/minify"
+	"github.com/tdewolff/minify/css"
+	"github.com/tdewolff/minify/html"
+	"github.com/tdewolff/minify/js"
+	"github.com/tdewolff/minify/json"
+	"github.com/tdewolff/minify/svg"
+	"github.com/tdewolff/minify/xml"
+)
+
+func Compile() {
+	m := minify.New()
+	m.AddFunc("text/css", css.Minify)
+	m.AddFunc("text/html", html.Minify)
+	m.AddFunc("image/svg+xml", svg.Minify)
+	m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
+	m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
+	m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)
+
+	items, _ := ioutil.ReadDir("./private")
+	for _, item := range items {
+		if item.IsDir() {
+			subitems, _ := ioutil.ReadDir(item.Name())
+			for _, subitem := range subitems {
+				if !subitem.IsDir() {
+					// handle file there
+					fmt.Println(item.Name() + "/" + subitem.Name())
+				}
+			}
+		} else {
+			// handle file there
+			fmt.Println(item.Name())
+		}
+	}
+
+	s, err = m.String(mediatype, s)
+	if err != nil {
+		panic(err)
+	}
+}

+ 49 - 39
src/modules/config.go

@@ -1,49 +1,59 @@
-package config
+package modules
 
-type YamlConfig struct {
-	website WebsiteConfig
-	addons  []string
+import (
+	"fmt"
+	"io/ioutil"
+
+	"gopkg.in/yaml.v2"
+)
+
+type Config struct {
+	Website        WebsiteConfig        `yaml:"website"`
+	OpenWeatherMap OpenWeatherMapConfig `yaml:"openweathermap"`
+	Addons         []string             `yaml:"addons"`
 }
 
 type WebsiteConfig struct {
-	title        string
-	description  string
-	language     string
-	localization string
-	useMetric    bool
-	theme        string
+	Title        string `yaml:"title"`
+	Description  string `yaml:"description"`
+	Language     string `yaml:"language"`
+	Localization string `yaml:"localization"`
+	UseMetric    bool   `yaml:"useMetric"`
+	Theme        string `yaml:"theme"`
+}
+
+type OpenWeatherMapConfig struct {
+	ApiKey    string `yaml:"apiKey"`
+	Longitude string `yaml:"lon"`
+	Latitude  string `yaml:"lat"`
 }
 
-func LoadConfig() YamlConfig {
-	defaultConfig := YamlConfig{
-		website: WebsiteConfig{
-			title:        "Magma Dashboard",
-			description:  "",
-			language:     "en",
-			localization: "en-us",
-			useMetric:    true,
-			theme:        "flame",
+func LoadConfig() Config {
+	defaultConfig := Config{
+		Website: WebsiteConfig{
+			Title:        "Magma Dashboard",
+			Description:  "",
+			Language:     "en",
+			Localization: "en-us",
+			UseMetric:    true,
+			Theme:        "flame",
 		},
-		addons: []string{},
+		Addons: []string{},
+	}
+
+	yamlFile, err := ioutil.ReadFile("./public/config.yaml")
+	if err != nil {
+		fmt.Printf("Error reading YAML file: %s\n", err)
+		return defaultConfig
+	}
+
+	var yamlConfig Config
+	err = yaml.Unmarshal(yamlFile, &yamlConfig)
+	if err != nil {
+		fmt.Printf("Error parsing YAML file: %s\n", err)
+		return defaultConfig
 	}
 
-	// yamlFile, err := ioutil.ReadFile("./public/config.yaml")
-	// if err != nil {
-	// 	fmt.Printf("Error reading YAML file: %s\n", err)
-	// 	return defaultConfig
-	// }
-	return defaultConfig
-	// fmt.Printf("YAML file: %s\n", yamlFile)
-
-	// var yamlConfig YamlConfig
-	// err = yaml.Unmarshal(yamlFile, &yamlConfig)
-	// if err != nil {
-	// 	fmt.Printf("Error parsing YAML file: %s\n", err)
-	// 	return defaultConfig
-	// }
-
-	// fmt.Printf(yamlConfig.website.title)
-	// fmt.Printf("%+v\n", yamlConfig)
-	// fmt.Printf("%+v\n", defaultConfig)
-	// return yamlConfig
+	fmt.Println("Loaded config:", yamlConfig)
+	return yamlConfig
 }

+ 0 - 25
src/modules/config.ts

@@ -1,25 +0,0 @@
-export class Config {
-  website: WebsiteConfig = new WebsiteConfig();
-  addons: string[] = [];
-}
-
-class WebsiteConfig {
-  public title: string = "";
-  public description: string = "";
-  public language: string = "en";
-  public localization: string = "en-US";
-  public useMetric: boolean = true;
-  public theme: string = "flame";
-}
-
-export async function loadConfig(): Promise<Config | null> {
-  try {
-    const json = JSON.parse(
-      await Deno.readTextFile(`${Deno.cwd()}/public/config.json`),
-    );
-    return json as Config;
-  } catch (err) {
-    console.error(err);
-    return null;
-  }
-}

+ 0 - 4
src/modules/file.ts

@@ -1,4 +0,0 @@
-// const watcher = Deno.watchFs("./public");
-// for await (const event of watcher) {
-//   console.log(">>>> event", event);
-//   Example event: { kind: "create", paths: [ "/home/alice/deno/foo.txt" ] }

+ 5 - 0
src/modules/server.go

@@ -0,0 +1,5 @@
+package modules
+
+func StartServer() {
+
+}

+ 0 - 0
src/private/themes/custom/index.html