9 Commits

Author SHA1 Message Date
427bec5081 make it better
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-09-17 21:55:50 +01:00
dc69cc447d Initial work
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-09-17 21:50:55 +01:00
c5b3c2bd98 Initial work. 2022-09-17 21:50:46 +01:00
Midou36O
e8d8b9b096 Update docker.yml
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
2022-09-15 17:37:33 +00:00
Midou36O
a18f76d206 Merge pull request #2 from ProjectSegfault/form-update
WIP: Forms UI rework.
2022-09-14 13:35:17 +00:00
50b058c1d2 Finished. 2022-09-14 14:32:56 +01:00
fc5fc6a91d Partial work. 2022-09-14 10:17:18 +01:00
cbd31e8893 fuck it, commit to main. 2022-09-13 14:47:10 +01:00
Midou36O
f0ed8fbc39 Merge pull request #1 from ProjectSegfault/optional
remove the required tag
2022-09-13 13:36:38 +00:00
10 changed files with 267 additions and 9 deletions

View File

@@ -26,4 +26,4 @@ jobs:
uses: docker/build-push-action@v3
with:
push: true
tags: projectsegfault/segfautils:latest
tags: realprojectsegfault/segfautils:latest

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
data/config.toml

View File

@@ -2,6 +2,7 @@ package api
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
@@ -17,6 +18,26 @@ var (
authToken = config.AuthToken()
)
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) {
io.WriteString(w, "Disabled")
})
}
}
func Announcements() {
http.HandleFunc("/api/announcements", getAnnouncements)
http.HandleFunc("/api/announcements/post", handleAnnouncements)
@@ -32,8 +53,8 @@ func handleAnnouncements(w http.ResponseWriter, r *http.Request) {
http.Error(w, "You need to provide the authorization token given to you by your system administrator in order to post an announcement.", http.StatusUnauthorized)
return
} else {
if r.FormValue("title") == "" || r.FormValue("link") == "" || r.FormValue("severity") == "" {
http.Error(w, "Your request is not proper. Please add a title, link, and severity.", http.StatusBadRequest)
if r.FormValue("title") == "" || r.FormValue("severity") == "" {
http.Error(w, "Your request is not proper. Please add a title and severity.", http.StatusBadRequest)
return
} else {
w.WriteHeader(http.StatusOK)

90
api/settings.go Normal file
View File

@@ -0,0 +1,90 @@
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)
}
}

18
config/optionannounce.go Normal file
View File

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

18
config/optionform.go Normal file
View File

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

View File

@@ -5,4 +5,8 @@ auth_token = "YOURAUTHTOKEN"
[hcaptcha]
site_key = "YOURSITEKEY"
secret_key = "YOURSECRETKEY"
secret_key = "YOURSECRETKEY"
[options]
announce = true
form = false

1
data/options.json Executable file
View File

@@ -0,0 +1 @@
{"Announcements":"false","Form":"true"}

View File

@@ -46,8 +46,9 @@ func main() {
http.HandleFunc("/announcements", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/announcements.html")
})
api.Settings()
api.Form()
api.Announcements()
api.CheckAnn()
log.Println("[HTTP] HTTP server is now running at " + config.Port() + "!")
log.Println(http.ListenAndServe(":"+config.Port(), nil))
}

View File

@@ -11,6 +11,107 @@
color: #fff;
font-family: 'JetBrains Mono', 'JetBrainsMono Nerd Font', monospace
}
.container {
width: 100%;
max-width: 400px;
margin: 0 auto;
position: relative;
}
#contact input[type="token"],
#contact input[type="link"],
#contact textarea,
#contact select,
#contact button[type="submit"] {
font: 500 15px/16px "Roboto", sans-serif;
}
.container h1 {
text-align: center;
margin: 0;
padding: 0;
font-size: 2rem;
}
#contact textarea {
text-align: center;
border-radius: 5px;
margin-top: 10px;
width: 100%;
border: none;
color: #FFF;
background: #3b3b3b;
margin: 0 0 5px;
padding: 10px;
height: 100px;
max-width: 100%;
resize: none;
}
#contact input, select {
text-align: center;
border-radius: 5px;
color: #FFF;
width: 100%;
border: none;
background: #3b3b3b;
margin: 0 0 5px;
padding: 10px;
max-width: 100%;
}
#contact select {
width: 105%;
max-width: 105%;
}
#contact button[type="submit"] {
cursor: pointer;
width: 105%;
border: none;
background: #00d4aa;
color: #252525;
margin: 0 0 5px;
padding: 10px;
font-size: 15px;
border-radius: 5px;
}
#contact button[type="submit"]:hover {
background: #00a484;
-webkit-transition: background 0.3s ease-in-out;
-moz-transition: background 0.3s ease-in-out;
transition: background-color 0.3s ease-in-out;
}
#contact button[type="submit"]:active {
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
}
#contact button[type="delete"] {
cursor: pointer;
width: 105%;
border: none;
background: #d10006;
color: #FFF;
margin: 0 0 5px;
padding: 10px;
font-size: 15px;
border-radius: 5px;
}
#contact button[type="delete"]:hover {
background: #990005;
-webkit-transition: background 0.3s ease-in-out;
-moz-transition: background 0.3s ease-in-out;
transition: background-color 0.3s ease-in-out;
}
#contact button[type="delete"]:active {
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
}
a {
color: #00d4aa;
@@ -22,8 +123,10 @@
</style>
</head>
<body>
<div class="container">
<h1>Post Announcement</h1>
<form
id="contact"
action="/api/announcements/post"
method="POST"
target="_blank"
@@ -45,7 +148,6 @@
</select>
</div>
<textarea
id="title"
name="title"
rows="4"
cols="25"
@@ -58,10 +160,11 @@
placeholder="Your link for more details"
/>
<br />
<input type="submit" value="Submit" />
<button type="submit" />Submit</button>
</form>
<h1>Delete Announcement</h1>
<h1 style="margin-top: 20px;">Delete Announcement</h1>
<form
id="contact"
action="/api/announcements/delete"
method="POST"
target="_blank"
@@ -75,7 +178,8 @@
/>
<br />
</div>
<input type="submit" value="Submit" />
<button type="delete"/>Delete</button>
</form>
</div>
</body>
</html>