91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"log"
|
|
"net/http"
|
|
"html/template"
|
|
"math/rand"
|
|
// "encoding/json"
|
|
// "errors"
|
|
"strings"
|
|
"fmt"
|
|
)
|
|
|
|
const Critique uint64 = 0
|
|
const Upvote uint64 = 1
|
|
const Downvote uint64 = 2
|
|
|
|
type Definition struct {
|
|
Word string
|
|
Content string
|
|
ReviewIDs []uint64
|
|
}
|
|
|
|
type Review struct {
|
|
ParentID uint64
|
|
Category uint64
|
|
Content string
|
|
}
|
|
|
|
type Database struct {
|
|
DefinitionIDs map[string][]uint64
|
|
Definitions map[uint64]Definition
|
|
Reviews map[uint64]Review
|
|
}
|
|
|
|
func ToFilename(s string) string {
|
|
return strings.ReplaceAll(strings.Trim(s, " "), " ", "_")
|
|
}
|
|
|
|
func main() {
|
|
tmpl_funcMap := template.FuncMap{
|
|
"ToFilename": ToFilename,
|
|
}
|
|
|
|
index_tmpl, err := template.New("index.html").Funcs(tmpl_funcMap).ParseFiles("./tmpl/index.html")
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
word_tmpl, err := template.New("word.html").Funcs(tmpl_funcMap).ParseFiles("./tmpl/word.html")
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
transactionFile, err := os.Open("./transactions")
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
database := Database { make(map[string][]uint64), make(map[uint64]Definition), make(map[uint64]Review) }
|
|
|
|
http.HandleFunc("/", func (res http.ResponseWriter, req *http.Request) {
|
|
if req.URL.Path == "/" || req.URL.Path == "/index.html" {
|
|
err = index_tmpl.Execute(res, database)
|
|
if err != nil { log.Fatal(err) }
|
|
} else {
|
|
http.ServeFile(res, req, "static/" + req.URL.Path)
|
|
}
|
|
})
|
|
|
|
http.HandleFunc("/submit/definition", func (res http.ResponseWriter, req *http.Request) {
|
|
err = req.ParseForm()
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
word_name := req.Form["word_name"][0]
|
|
definition := req.Form["definition"][0]
|
|
definitionID := rand.Uint64()
|
|
|
|
database.Definitions[definitionID] = Definition { word_name, definition, make([]uint64, 0) }
|
|
database.DefinitionIDs[word_name] = append(database.DefinitionIDs[word_name], definitionID)
|
|
|
|
http.Redirect(res, req, "/", 303)
|
|
|
|
fmt.Fprintf(transactionFile, "NEW_WORD\n")
|
|
})
|
|
|
|
http.HandleFunc("/words/", func (res http.ResponseWriter, req *http.Request) {
|
|
word_tmpl.Execute(res, struct {
|
|
Word string
|
|
Definitions
|
|
})
|
|
})
|
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
}
|