update: general formatting and cleanup

This commit is contained in:
ngn 2024-08-14 00:32:48 +03:00
parent 25f30b51f0
commit 87a9236e01
15 changed files with 402 additions and 344 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
docker-compose.yml
compose.yml
content content
website website

9
Makefile Normal file
View File

@ -0,0 +1,9 @@
all: website
website: */*.go *.go
go build -o $@
format:
gofmt -s -w .
.PHONY: format

View File

@ -1,18 +1,33 @@
# website | MatterLinux Website # website | MatterLinux website
Soruce code of Matterlinux's offical website, which is Soruce code of Matterlinux's offical website, which is
located at [matterlinux.xyz](https://matterlinux.xyz) located at [matterlinux.xyz](https://matterlinux.xyz)
### Deployment ### Deployment
Web server can be built and deployed with docker: Web server can be built and deployed with docker compose using the following
configuration file:
```yaml
version: "3"
services:
website:
image: matterwebsite
restart: unless-stopped
build:
context: ./
ports:
- "127.0.0.1:9878:9878"
volumes:
- "./content:/app/content"
``` ```
docker build --tag matterwebsite . After saving the configuration file, you can build and run the docker container:
```bash
docker-compose up -d docker-compose up -d
``` ```
### Managing Content ### Managing content
Website content can be managed by editing JSON and markdown files Website content can be managed by editing JSON and markdown files
under the `content` direcotry. under the `content` direcotry.
Matterlinux's website content directory can be found in the MatterLinux's website content directory can be found in the
[content](https://git.matterlinux.xyz/Matter/content) [content](https://git.matterlinux.xyz/Matter/content)
repository. repository.

View File

@ -1,12 +0,0 @@
version: "3"
services:
website:
image: matterwebsite
restart: unless-stopped
build:
context: ./
ports:
- "127.0.0.1:9878:9878"
volumes:
- "./content:/app/content"

View File

@ -11,7 +11,7 @@ import (
const CONTENT_PATH string = "content" const CONTENT_PATH string = "content"
type Content struct{ type Content struct {
Dir string Dir string
Name string Name string
@ -31,7 +31,7 @@ func ListContent(dir string) ([]Content, error) {
dirpath := path.Join(CONTENT_PATH, dir) dirpath := path.Join(CONTENT_PATH, dir)
entries, err := os.ReadDir(dirpath) entries, err := os.ReadDir(dirpath)
if err != nil { if err != nil {
return nil, errors.New("Cannot readdir: "+dirpath) return nil, errors.New("Cannot readdir: " + dirpath)
} }
for _, e := range entries { for _, e := range entries {
@ -43,7 +43,7 @@ func ListContent(dir string) ([]Content, error) {
res = append(res, subres...) res = append(res, subres...)
} }
if(!strings.HasSuffix(e.Name(), ".json")) { if !strings.HasSuffix(e.Name(), ".json") {
continue continue
} }
@ -52,12 +52,12 @@ func ListContent(dir string) ([]Content, error) {
jsoncon, err := os.ReadFile(jsonfile) jsoncon, err := os.ReadFile(jsonfile)
if err != nil { if err != nil {
return nil, errors.New("Cannot get JSON: "+jsonfile) return nil, errors.New("Cannot get JSON: " + jsonfile)
} }
err = json.Unmarshal(jsoncon, &con) err = json.Unmarshal(jsoncon, &con)
if err != nil { if err != nil {
return nil, errors.New("Cannot parse JSON: "+jsonfile) return nil, errors.New("Cannot parse JSON: " + jsonfile)
} }
con.Dir = dir con.Dir = dir
@ -78,16 +78,16 @@ func GetContent(dir string, name string) (Content, error) {
jsoncontent, err := os.ReadFile(jsonfile) jsoncontent, err := os.ReadFile(jsonfile)
if err != nil { if err != nil {
return res, errors.New("Cannot read JSON file: "+jsonfile) return res, errors.New("Cannot read JSON file: " + jsonfile)
} }
rawcontent, err := os.ReadFile(mdfile) rawcontent, err := os.ReadFile(mdfile)
if err != nil { if err != nil {
return res, errors.New("Cannot read markdown file: "+mdfile) return res, errors.New("Cannot read markdown file: " + mdfile)
} }
if(json.Unmarshal(jsoncontent, &res)!= nil) { if json.Unmarshal(jsoncontent, &res) != nil {
return res, errors.New("Cannot parse JSON: "+jsonfile) return res, errors.New("Cannot parse JSON: " + jsonfile)
} }
res.Md = string(rawcontent) res.Md = string(rawcontent)

View File

@ -12,9 +12,9 @@ import (
// python3 -c "import string; print(string.ascii_letters+string.digits+\" /,_-?!'\\\"\")" // python3 -c "import string; print(string.ascii_letters+string.digits+\" /,_-?!'\\\"\")"
var valid string = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 /,_-?!'"` var valid string = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 /,_-?!'"`
func IsStringValid(s string) bool{ func IsStringValid(s string) bool {
for _, c := range s { for _, c := range s {
if !strings.Contains(valid, string(c)){ if !strings.Contains(valid, string(c)) {
fmt.Printf("%c\n", c) fmt.Printf("%c\n", c)
return false return false
} }
@ -22,7 +22,7 @@ func IsStringValid(s string) bool{
return true return true
} }
func RenderError(c *fiber.Ctx, code int) error{ func RenderError(c *fiber.Ctx, code int) error {
var msg string = "Server Error" var msg string = "Server Error"
c.Status(code) c.Status(code)

23
log/log.go Normal file
View File

@ -0,0 +1,23 @@
package log
import (
"fmt"
"time"
)
func Log(p string, f string, args ...interface{}) {
now := time.Now()
nstr := now.Format("[02/01/06 15:04:05]")
fmt.Printf("%s -%s- ", nstr, p)
fmt.Printf(f, args...)
fmt.Println()
}
func Info(f string, args ...interface{}) {
Log("INFO", f, args...)
}
func Error(f string, args ...interface{}) {
Log("ERROR", f, args...)
}

33
main.go
View File

@ -1,6 +1,6 @@
/* /*
* website | MatterLinux Official Website * website | MatterLinux website
* MatterLinux 2023-2024 (https://matterlinux.xyz) * MatterLinux 2023-2024 (https://matterlinux.xyz)
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -21,16 +21,15 @@
package main package main
import ( import (
"log"
"git.matterlinux.xyz/Matter/website/lib" "git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"git.matterlinux.xyz/Matter/website/routes" "git.matterlinux.xyz/Matter/website/routes"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2" "github.com/gofiber/template/html/v2"
) )
func main(){ func main() {
log.SetFlags(log.Lshortfile | log.Ltime) var err error
engine := html.New("./templates", ".html") engine := html.New("./templates", ".html")
app := fiber.New(fiber.Config{ app := fiber.New(fiber.Config{
@ -40,25 +39,25 @@ func main(){
app.Static("/", "./public") app.Static("/", "./public")
app.Static("/assets", "./content/assets") app.Static("/assets", "./content/assets")
app.Get("/", routes.IndexRoute) app.Get("/", routes.GET_Index)
app.Get("/download", routes.DownloadRoute) app.Get("/download", routes.GET_Download)
app.Get("/news", routes.NewsRoute) app.Get("/news", routes.GET_News)
app.Get("/news/:id", routes.PostRoute) app.Get("/news/:id", routes.GET_New)
app.Get("/wiki", routes.WikiMainRoute) app.Get("/wiki", routes.GET_Wiki)
app.Get("/wiki/:id", routes.WikiRoute) app.Get("/wiki/:id", routes.GET_WikiPage)
app.Get("/hub", routes.ConfigsRoute) app.Get("/hub", routes.GET_Configs)
app.Get("/hub/:name", routes.ConfigRoute) app.Get("/hub/:name", routes.GET_Config)
app.Get("*", func(c *fiber.Ctx) error { app.Get("*", func(c *fiber.Ctx) error {
return lib.RenderError(c, 404) return lib.RenderError(c, 404)
}) })
log.Println("Starting MatterLinux Website on port 9878") log.Info("Starting MatterLinux website on port 9878")
err := app.Listen(":9878")
if err != nil { if err = app.Listen(":9878"); err != nil {
log.Fatalf("Error starting server: %s", err) log.Error("Error starting server: %s", err.Error())
} }
} }

View File

@ -11,7 +11,7 @@
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
background: var(--dark-second); background: var(--dark-second);
border: solid 1px var(--bright-main); border: solid 1px var(--bright-second);
padding: 10px; padding: 10px;
} }

View File

@ -1,7 +1,7 @@
main { main {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
padding-top: 50px; padding-top: 40px;
gap: 10px; gap: 10px;
} }

View File

@ -4,12 +4,12 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os" "os"
"path" "path"
"strings" "strings"
"git.matterlinux.xyz/Matter/website/lib" "git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"github.com/bigkevmcd/go-configparser" "github.com/bigkevmcd/go-configparser"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -25,7 +25,7 @@ type Config struct {
Keywords string Keywords string
} }
func LoadConfig(c *Config) (error) { func (c *Config) Load() error {
agent := fiber.Get(c.Url) agent := fiber.Get(c.Url)
code, body, errs := agent.Bytes() code, body, errs := agent.Bytes()
if len(errs) > 0 { if len(errs) > 0 {
@ -64,10 +64,10 @@ func LoadConfig(c *Config) (error) {
c.Author = author c.Author = author
c.Keywords = strings.ReplaceAll(keywords, ",", ", ") c.Keywords = strings.ReplaceAll(keywords, ",", ", ")
if(!lib.IsStringValid(c.Name) || if !lib.IsStringValid(c.Name) ||
!lib.IsStringValid(c.Desc) || !lib.IsStringValid(c.Desc) ||
!lib.IsStringValid(c.Author) || !lib.IsStringValid(c.Author) ||
!lib.IsStringValid(c.Keywords)){ !lib.IsStringValid(c.Keywords) {
return fmt.Errorf("keywords or name contain illegal chars: %s", c.Url) return fmt.Errorf("keywords or name contain illegal chars: %s", c.Url)
} }
@ -93,8 +93,7 @@ func GetConfigs() ([]Config, error) {
} }
for i := range data["list"] { for i := range data["list"] {
err = LoadConfig(&data["list"][i]) if err = data["list"][i].Load(); err != nil {
if err != nil {
return nil, err return nil, err
} }
} }
@ -102,16 +101,20 @@ func GetConfigs() ([]Config, error) {
return data["list"], nil return data["list"], nil
} }
func ConfigsRoute(c *fiber.Ctx) error{ func GET_Configs(c *fiber.Ctx) error {
con, err := lib.GetContent("", "configs") var (
if err != nil { err error
log.Printf("GetContent failed: %s", err.Error()) con lib.Content
configs []Config
)
if con, err = lib.GetContent("", "configs"); err != nil {
log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500) return lib.RenderError(c, 500)
} }
configs, err := GetConfigs() if configs, err = GetConfigs(); err != nil {
if err != nil { log.Error("GetConfigs failed: %s", err.Error())
log.Printf("GetConfigs failed: %s", err.Error())
return lib.RenderError(c, 500) return lib.RenderError(c, 500)
} }
@ -121,12 +124,17 @@ func ConfigsRoute(c *fiber.Ctx) error{
}) })
} }
func ConfigRoute(c *fiber.Ctx) error{ func GET_Config(c *fiber.Ctx) error {
name := c.Params("name") var (
err error
name string
configs []Config
)
configs, err := GetConfigs() name = c.Params("name")
if err != nil {
log.Printf("GetConfigs failed: %s", err.Error()) if configs, err = GetConfigs(); err != nil {
log.Error("GetConfigs failed: %s", err.Error())
return lib.RenderError(c, 500) return lib.RenderError(c, 500)
} }

View File

@ -1,16 +1,19 @@
package routes package routes
import ( import (
"log"
"git.matterlinux.xyz/Matter/website/lib" "git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func DownloadRoute(c *fiber.Ctx) error{ func GET_Download(c *fiber.Ctx) error {
con, err := lib.GetContent("", "download") var (
if err != nil { con lib.Content
log.Printf("GetContent failed: %s", err.Error()) err error
)
if con, err = lib.GetContent("", "download"); err != nil {
log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500) return lib.RenderError(c, 500)
} }

View File

@ -1,16 +1,19 @@
package routes package routes
import ( import (
"log"
"git.matterlinux.xyz/Matter/website/lib" "git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func IndexRoute(c *fiber.Ctx) error{ func GET_Index(c *fiber.Ctx) error {
con, err := lib.GetContent("", "index") var (
if err != nil { err error
log.Printf("GetContent failed: %s", err.Error()) con lib.Content
)
if con, err = lib.GetContent("", "index"); err != nil {
log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500) return lib.RenderError(c, 500)
} }

View File

@ -1,30 +1,30 @@
package routes package routes
import ( import (
"log"
"sort" "sort"
"git.matterlinux.xyz/Matter/website/lib" "git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func NewsRoute(c *fiber.Ctx) error { func GET_News(c *fiber.Ctx) error {
contents, err := lib.ListContent("news") contents, err := lib.ListContent("news")
if err != nil { if err != nil {
log.Printf("ListContent failed: %s", err.Error()) log.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500) return lib.RenderError(c, 500)
} }
sort.Slice(contents, func(i, j int) bool { sort.Slice(contents, func(i, j int) bool {
time1, err := lib.TimeFromString(contents[i].Date) time1, err := lib.TimeFromString(contents[i].Date)
if err != nil { if err != nil {
log.Printf("Bad date while sorting: %s", err.Error()) log.Error("Bad date while sorting: %s", err.Error())
return false return false
} }
time2, err := lib.TimeFromString(contents[j].Date) time2, err := lib.TimeFromString(contents[j].Date)
if err != nil { if err != nil {
log.Printf("Bad date while sorting: %s", err.Error()) log.Error("Bad date while sorting: %s", err.Error())
return false return false
} }
@ -36,7 +36,7 @@ func NewsRoute(c *fiber.Ctx) error {
}) })
} }
func PostRoute(c *fiber.Ctx) error { func GET_New(c *fiber.Ctx) error {
postid := c.Params("id") postid := c.Params("id")
if len(postid) == 0 { if len(postid) == 0 {
return lib.RenderError(c, 404) return lib.RenderError(c, 404)
@ -44,18 +44,18 @@ func PostRoute(c *fiber.Ctx) error {
contents, err := lib.ListContent("news") contents, err := lib.ListContent("news")
if err != nil { if err != nil {
log.Printf("ListContent failed: %s", err.Error()) log.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500) return lib.RenderError(c, 500)
} }
for _, con := range contents { for _, con := range contents {
if(con.ID != postid) { if con.ID != postid {
continue continue
} }
con, err = lib.GetContent(con.Dir, con.Name) con, err = lib.GetContent(con.Dir, con.Name)
if err != nil { if err != nil {
log.Printf("ListContent failed: %s", err.Error()) log.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500) return lib.RenderError(c, 500)
} }

View File

@ -1,16 +1,19 @@
package routes package routes
import ( import (
"log"
"git.matterlinux.xyz/Matter/website/lib" "git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func WikiMainRoute(c *fiber.Ctx) error { func GET_Wiki(c *fiber.Ctx) error {
con, err := lib.GetContent("wiki", "main") var (
if err != nil { con lib.Content
log.Printf("GetContent failed: %s", err.Error()) err error
)
if con, err = lib.GetContent("wiki", "main"); err != nil {
log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500) return lib.RenderError(c, 500)
} }
@ -20,30 +23,35 @@ func WikiMainRoute(c *fiber.Ctx) error {
}) })
} }
func WikiRoute(c *fiber.Ctx) error{ func GET_WikiPage(c *fiber.Ctx) error {
var (
contents []lib.Content
err error
)
docid := c.Params("id") docid := c.Params("id")
if len(docid) == 0 { if len(docid) == 0 {
return lib.RenderError(c, 404) return lib.RenderError(c, 404)
} }
contents, err := lib.ListContent("wiki") if contents, err = lib.ListContent("wiki"); err != nil {
if err != nil { log.Error("ListContent failed: %s", err.Error())
log.Printf("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500) return lib.RenderError(c, 500)
} }
for _, con := range contents { for _, con := range contents {
if(con.ID != docid) { if con.ID != docid {
continue continue
} }
con, err = lib.GetContent(con.Dir, con.Name) con, err = lib.GetContent(con.Dir, con.Name)
if err != nil { if err != nil {
log.Printf("GetContent failed: %s", err.Error()) log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500) return lib.RenderError(c, 500)
} }
con.Title = "Wiki: "+con.Title con.Title = "Wiki: " + con.Title
return c.Render("post", fiber.Map{ return c.Render("post", fiber.Map{
"title": con.Title, "title": con.Title,
"post": con, "post": con,