WIP Branch completed: Overhaul of site logic, removed comments, repositories
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
)
|
||||
|
||||
func LoadAdminHash(path string) [32]byte {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
panic("Could not find admin hash...")
|
||||
}
|
||||
defer file.Close()
|
||||
reader := bufio.NewScanner(file)
|
||||
// The dsn should be the first line of the file
|
||||
if reader.Scan() {
|
||||
var buff [32]byte
|
||||
for i, b := range reader.Bytes() {
|
||||
buff[i] = b
|
||||
}
|
||||
return buff
|
||||
}
|
||||
panic(path + " is empty...")
|
||||
}
|
||||
@@ -16,6 +16,7 @@ GNU General Public License for more details.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.Rawley Fowler, 2022
|
||||
*/
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
@@ -23,10 +24,10 @@ import (
|
||||
|
||||
func LoadApiKey(path string) string {
|
||||
file, err := os.Open(path)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
panic("Could not find file: " + path)
|
||||
panic("Could not find DSN for database...")
|
||||
}
|
||||
defer file.Close()
|
||||
reader := bufio.NewScanner(file)
|
||||
if reader.Scan() {
|
||||
return reader.Text()
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GenerateCaptchaValues() (int, int) {
|
||||
return rand.Intn(100), rand.Intn(100)
|
||||
}
|
||||
|
||||
func UpdateCaptcha(captcha *[2]int) {
|
||||
for {
|
||||
captcha[0], captcha[1] = GenerateCaptchaValues()
|
||||
time.Sleep(30 * time.Minute)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
package utils
|
||||
|
||||
/*
|
||||
Copyright (C) 2022 Rawley Fowler
|
||||
|
||||
@@ -15,13 +16,17 @@ GNU General Public License for more details.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.Rawley Fowler, 2022
|
||||
*/
|
||||
|
||||
import (
|
||||
"gitlab.com/rawleyifowler/site-rework/models"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Performs migrations of all models
|
||||
func PerformMigrations(db *gorm.DB) {
|
||||
db.AutoMigrate(&models.BlogPost{})
|
||||
db.AutoMigrate(&models.Comment{})
|
||||
func CreateDatabase(dsn string) (*gorm.DB, error) {
|
||||
var err error
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
@@ -16,6 +16,7 @@ GNU General Public License for more details.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.Rawley Fowler, 2022
|
||||
*/
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
|
||||
31
utils/initialize_db.go
Normal file
31
utils/initialize_db.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package utils
|
||||
|
||||
/*
|
||||
Copyright (C) 2022 Rawley Fowler
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.Rawley Fowler, 2022
|
||||
*/
|
||||
|
||||
import (
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func InitializeDatabase(dsn string) *gorm.DB {
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("Failed to open database with dsn -> " + dsn)
|
||||
}
|
||||
return db
|
||||
}
|
||||
@@ -1,5 +1,22 @@
|
||||
package utils
|
||||
|
||||
/*
|
||||
Copyright (C) 2022 Rawley Fowler
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.Rawley Fowler, 2022
|
||||
*/
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -7,21 +24,14 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Returns a handler function to serve a page based on the template that is inputted
|
||||
func ServePage(temp string) gin.HandlerFunc {
|
||||
title := strings.Split(temp, ".")[0]
|
||||
func ServePage(t string) gin.HandlerFunc {
|
||||
title := strings.Split(t, ".")[0]
|
||||
if title == "index" {
|
||||
// Index is the home page so we don't care
|
||||
title = ""
|
||||
} else {
|
||||
// Else title it accordingly
|
||||
title = " | " + title
|
||||
}
|
||||
return func(c *gin.Context) {
|
||||
c.HTML(
|
||||
http.StatusOK,
|
||||
temp,
|
||||
struct{ Title string }{Title: title},
|
||||
)
|
||||
c.HTML(http.StatusOK, t, struct{ Title string }{Title: title})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package utils
|
||||
|
||||
// TODO: Implement a spam checking module
|
||||
func IsSpam(t string) bool {
|
||||
return false
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TimedClearMap(m *map[string]uint, timer uint) {
|
||||
for {
|
||||
t, err := time.ParseDuration(strconv.FormatUint(uint64(timer), 10) + "ms")
|
||||
if err != nil {
|
||||
panic("Something went wrong initializing time!!")
|
||||
}
|
||||
time.Sleep(t)
|
||||
for key := range *m {
|
||||
delete(*m, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
29
utils/timed_clear_map.go
Normal file
29
utils/timed_clear_map.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package utils
|
||||
|
||||
/*
|
||||
Copyright (C) 2022 Rawley Fowler
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.Rawley Fowler, 2022
|
||||
*/
|
||||
|
||||
import "time"
|
||||
|
||||
func TimeClearMap(m map[string]int64, t int64) {
|
||||
time.Sleep(time.Duration(t) * time.Millisecond)
|
||||
for k := range m {
|
||||
if m[k] < time.Now().UnixMilli()+t {
|
||||
delete(m, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user