15 Commits

Author SHA1 Message Date
Midou36O
daf1ffe95c Merge pull request #11 from ProjectSegfault/options-pub
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Options pub
2022-09-18 18:21:57 +00:00
6bdbf3b83d Make it compatible with the previous version.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-09-18 19:16:57 +01:00
1c2e6e3da9 Fix everything odyssey pointed out.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-09-18 17:44:37 +01:00
65d35747fc Fix Some more stuff, disabling form actually works now.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-09-18 17:18:14 +01:00
af39a52971 Ok so, pages don't get disabled, but they don't show up! Half working.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-09-18 01:43:17 +01:00
16ea5c3138 Fix the go error.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-09-18 00:40:46 +01:00
0beb8a33de Did I forget the image? Maybe I did.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-09-18 00:37:34 +01:00
3abdbde812 Add dev to docker (there has to be a way to improve this!) 2022-09-18 00:34:45 +01:00
bfdc165346 I'm stupid.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-09-18 00:26:10 +01:00
64a39ac5e5 fuck this was the solution
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-09-17 23:24:32 +01:00
950d65ca59 huh?
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-09-17 23:23:39 +01:00
65634751b6 fix
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-09-17 23:20:23 +01:00
7542cfba4a Meanwhile on gitea
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-09-17 23:16:52 +01:00
9a504d1638 Partial working form and announcements settings.
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-09-17 23:12:06 +01:00
e614ab79fa Use viper instead of the clusterfuck of json i was trying to use.
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-09-17 22:33:44 +01:00
8 changed files with 79 additions and 160 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,2 @@
data/config.toml
data/options.json
data/announcements.json

View File

@@ -7,9 +7,12 @@ pipeline:
- go build -o segfautils
dockerize_n_publish:
when:
branch : [master]
event: [push]
name: dockerize and publish
image: plugins/docker
image: plugins/docker
registry: git.projectsegfau.lt
repo: git.projectsegfau.lt/projectsegfault/segfautils
settings:
username:
from_secret: username
@@ -17,6 +20,20 @@ pipeline:
from_secret: password
repo: projectsegfault/segfautils
dockerfile: Dockerfile
dockerize_dev:
when:
event: [push]
image: plugins/docker
name: dockerize and publish dev
registry: git.projectsegfau.lt
repo: git.projectsegfau.lt/projectsegfault/segfautils
settings:
username:
from_secret: username
password:
from_secret: password
repo: projectsegfau.lt/segfautils
tags: dev
dockerfile: Dockerfile

View File

@@ -2,7 +2,6 @@ package api
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
@@ -16,28 +15,30 @@ import (
var (
authToken = config.AuthToken()
resAnn = config.OptAnn()
)
func CheckAnn() {
jsonFile, err := os.Open("./data/options.json")
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var result map[string]interface{}
json.Unmarshal([]byte(byteValue), &result)
res := result["Announcements"]
if res == "true" {
Announcements()
} else {
log.Println("Announcements disabled")
http.HandleFunc("/api/announcements", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Announcements are disabled.", http.StatusNotFound)
func AnnCheck() {
if resAnn == "false" {
log.Println("[Segfautils] Announcements are disabled")
http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Announcements are disabled.", http.StatusServiceUnavailable)
})
http.HandleFunc("/api/announcements", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "{\"enabled\": \"false\"}", http.StatusServiceUnavailable)
})
} else {
AnnPage()
Announcements()
}
}
func AnnPage() {
http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/announcements.html")
})
}
func Announcements() {
http.HandleFunc("/api/announcements", getAnnouncements)
http.HandleFunc("/api/announcements/post", handleAnnouncements)
@@ -60,6 +61,7 @@ func handleAnnouncements(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
now := time.Now().Unix()
data := map[string]interface{}{
"enabled": "true",
"title": r.FormValue("title"),
"link": r.FormValue("link"),
"severity": r.FormValue("severity"),

View File

@@ -1,21 +1,16 @@
package api
import (
"io/ioutil"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/goccy/go-json"
"github.com/kataras/hcaptcha"
"fmt"
"io"
"net/url"
"text/template"
"github.com/ProjectSegfault/segfautils/config"
"github.com/ProjectSegfault/segfautils/utils"
"github.com/kataras/hcaptcha"
)
var (
@@ -23,28 +18,37 @@ var (
secretKey = config.HCaptchaSecretKey()
webhookURL = config.WebhookURL()
client = hcaptcha.New(secretKey) /* See `Client.FailureHandler` too. */
resForm = config.OptForm()
)
func FormCheck() {
jsonFile, err := os.Open("./data/options.json")
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var result map[string]interface{}
json.Unmarshal([]byte(byteValue), &result)
res := result["Form"]
if res == "true" {
Form()
} else {
log.Println("Forms disabled")
http.HandleFunc("/api/form", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Disabled")
if resForm == "false" {
log.Println("[Segfautils] Contact form is disabled")
http.HandleFunc("/form", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Form is disabled.", http.StatusServiceUnavailable)
})
} else {
FormPage()
Form()
}
}
func FormPage() {
type StaticThing struct {
HCaptchaSiteKey string
}
tmpl_form := template.Must(template.ParseFiles("static/form.html"))
http.HandleFunc("/form/", func(w http.ResponseWriter, r *http.Request) {
hcaptcha_site_key := config.HCaptchaSiteKey()
data := StaticThing{
HCaptchaSiteKey: hcaptcha_site_key,
}
tmpl_form.Execute(w, data)
})
}
func Form() {
http.HandleFunc("/api/form", client.HandlerFunc(theActualFormCode))
}

View File

@@ -1,90 +0,0 @@
package api
import (
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/ProjectSegfault/segfautils/config"
"github.com/goccy/go-json"
)
var (
announcements = config.OptAnn()
form = config.OptForm()
)
func Settings() {
CheckSet()
http.HandleFunc("/api/options", getOpt)
}
func CheckSet() {
os.Remove("./data/options.json")
if form == "true" && announcements == "false" {
data := map[string]interface{}{
"Announcements": "false",
"Form": "true",
}
jsonData, err := json.Marshal(data)
if err != nil {
log.Printf("Could not marshal json : %s\n", err)
return
}
ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm)
} else if form == "true" && announcements == "true" {
data := map[string]interface{}{
"Announcements": "true",
"Form": "true",
}
jsonData, err := json.Marshal(data)
if err != nil {
log.Printf("Could not marshal json : %s\n", err)
return
}
ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm)
} else if form == "false" && announcements == "true" {
data := map[string]interface{}{
"Announcements": "true",
"Form": "false",
}
jsonData, err := json.Marshal(data)
if err != nil {
log.Printf("Could not marshal json : %s\n", err)
return
}
ioutil.WriteFile("./data/options.json", jsonData, os.ModePerm)
} else {
resp := []byte("The fuck do you want me to do then?")
ioutil.WriteFile("./data/options.json", resp, os.ModePerm)
}
}
func getOpt(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if _, err := os.Stat("./data/options.json"); errors.Is(err, os.ErrNotExist) {
http.Error(w, "There is nothing to see here.", http.StatusNotFound)
return
} else {
f, err := os.Open("./data/options.json")
if err != nil {
log.Fatal(err)
}
defer f.Close()
io.Copy(w, f)
}
}

View File

@@ -6,13 +6,13 @@ import (
"github.com/spf13/viper"
)
func OptForm() string {
func OptAnn() string {
viper.SetConfigName("config")
viper.AddConfigPath("./data")
err := viper.ReadInConfig()
if err != nil {
log.Println("Error reading config. Error getting: options.form", err.Error())
log.Println("Error reading config. Error getting: options.announce", err.Error())
}
result := viper.GetString("options.form")
result := viper.GetString("options.announce")
return result
}

View File

@@ -6,13 +6,13 @@ import (
"github.com/spf13/viper"
)
func OptAnn() string {
func OptForm() string {
viper.SetConfigName("config")
viper.AddConfigPath("./data")
err := viper.ReadInConfig()
if err != nil {
log.Println("Error reading config. Error getting: options.announce", err.Error())
log.Println("Error reading config. Error getting: options.form", err.Error())
}
result := viper.GetString("options.announce")
result := viper.GetString("options.form")
return result
}

26
main.go
View File

@@ -12,18 +12,13 @@ import (
)
type StaticThingy struct {
Port string
HCaptchaSiteKey string
Port string
}
var port string
var shit bool
func main() {
log.Println("[Segfautils] Starting")
utils.CheckConfig()
log.Println("[HTTP] Starting server")
hcaptcha_site_key := config.HCaptchaSiteKey()
tmpl := template.Must(template.ParseFiles("static/index.html"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := StaticThingy{
@@ -32,23 +27,14 @@ func main() {
tmpl.Execute(w, data)
})
tmpl_form := template.Must(template.ParseFiles("static/form.html"))
http.HandleFunc("/form/", func(w http.ResponseWriter, r *http.Request) {
data := StaticThingy{
HCaptchaSiteKey: hcaptcha_site_key,
}
tmpl_form.Execute(w, data)
})
log.Println("[HTTP] Starting server")
api.AnnCheck()
api.FormCheck()
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "welcome to hell")
})
http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/announcements.html")
})
api.Settings()
api.FormCheck()
api.CheckAnn()
log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!")
log.Println(http.ListenAndServe(":"+config.Port(), nil))
}