repl.it
@vityavv/

BisqueStickyActivemovie

Go

No description

fork
loading
Files
  • main.go
  • articles
  • templates
  • articles.go
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main

import (
	"html/template"
	"net/http"
	"log"
	"strconv"
	"os"
	"golang.org/x/crypto/bcrypt"
	"fmt"
)

var templates = template.Must(template.ParseGlob("./templates/*.html"))

var pwHash = os.Getenv("PASSWORD")

func executeTemplate(w http.ResponseWriter, templ string, content interface{}) {
	err := templates.ExecuteTemplate(w, templ, content)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func httpFunc(w http.ResponseWriter, r *http.Request) {
	switch r.URL.Path {
		case "/", "/index.html":
			executeTemplate(w, "frontPage.html", GetFrontPage())
			return
		case "/archive", "/archive.html":
			executeTemplate(w, "archive.html", GetAllArticles())
			return
		case "/dashboard", "/dashboard.html":
			executeTemplate(w, "dashboard.html", GetAllArticles())
			return
		case "/new", "/new.html":
			if r.Method == "POST" {
				if r.FormValue("content") == "" || r.FormValue("title") == "" || r.FormValue("password") == "" {
					http.Error(w, "Either the content, title, or password are missing", http.StatusBadRequest)
				} else {
					err := bcrypt.CompareHashAndPassword([]byte(pwHash), []byte(r.FormValue("password")))
					if err != nil {
						http.Error(w, "Password does not match", http.StatusUnauthorized)
					} else {
						CreateArticle(r.FormValue("title"), r.FormValue("content"))
						http.Redirect(w, r, "/", http.StatusFound)
					}
				}
			} else {
				executeTemplate(w, "new.html", []string{})//this last one doesn't matter, we aren't using anything in the template
			}
			return
	}
	http.NotFound(w, r)
}

func articleFunc(w http.ResponseWriter, r *http.Request) {
	num := r.URL.Path[len("/articles/"):]
	if num[len(num) - 1] == '/' {
		num = num[:len(num) - 1]
	}
	pageNum, err := strconv.Atoi(num)
	if err != nil {
		http.NotFound(w, r)
	}
	page, err := GetArticle(pageNum)
	if err != nil {
		http.NotFound(w, r)
	} else {
		executeTemplate(w, "article.html", page)
	}
}

func deleteFunc(w http.ResponseWriter, r *http.Request) {
	strKey := r.URL.Path[len("/delete/"):]
	key, err := strconv.Atoi(strKey)
	if err != nil {
		http.Error(w, "That's not a valid key", http.StatusBadRequest)
		return
	}
	if r.Method == "DELETE" {
		if r.FormValue("password") == "" {
			http.Error(w, "Password is missing", http.StatusBadRequest)
		} else {
			err := bcrypt.CompareHashAndPassword([]byte(pwHash), []byte(r.FormValue("password")))
			if err != nil {
				http.Error(w, "Password does not match", http.StatusUnauthorized)
			} else {
				err = DeleteArticle(key)
				if err != nil {
					if err.Error() == "Article not found" {
						http.Error(w, "Article not found", http.StatusNotFound)
					} else {
						http.Error(w, "Internal Server Error", http.StatusInternalServerError)
					}
				} else {
					fmt.Fprint(w, "Article successfully deleted")
				}
			}
		}
	} else {
		http.Redirect(w, r, "/", http.StatusFound)
	}
}

func main() {
	if pwHash == "" {
		log.Fatal("There is no password set! Please create a file called .env and make the contents \"PASSWORD=asdf\", with your password bcrypt hashed instead of asdf ")
	}
	FileInit()
	http.HandleFunc("/articles/", articleFunc)
	http.HandleFunc("/delete/", deleteFunc)
	http.HandleFunc("/", httpFunc)
	log.Fatal(http.ListenAndServe(":8080", nil))
}