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
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
located at [matterlinux.xyz](https://matterlinux.xyz)
### 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
```
### Managing Content
### Managing content
Website content can be managed by editing JSON and markdown files
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)
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

@ -43,7 +43,7 @@ func ListContent(dir string) ([]Content, error) {
res = append(res, subres...)
}
if(!strings.HasSuffix(e.Name(), ".json")) {
if !strings.HasSuffix(e.Name(), ".json") {
continue
}
@ -86,7 +86,7 @@ func GetContent(dir string, name string) (Content, error) {
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)
}

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...)
}

31
main.go
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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