shuffle some bits around

This commit is contained in:
leah 2022-01-29 14:02:37 +00:00
parent 5a7c186694
commit d8aef83244
1 changed files with 43 additions and 32 deletions

75
main.go
View File

@ -1,11 +1,13 @@
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"text/template"
@ -92,6 +94,31 @@ func logRequest(handler http.Handler) http.Handler {
})
}
func main() {
err := loadConfig()
if err != nil {
log.Fatalf("couldn't load config: %s", err)
}
openLogFile(config.Panel.LogPath)
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
http.HandleFunc("/login/", loginHandler)
http.Handle("/resources/",
http.StripPrefix("/resources/",
http.FileServer(http.Dir("./resources"))))
http.HandleFunc("/", handler)
fmt.Printf("listening on %v\n", config.Panel.Port)
fmt.Printf("Logging to %v\n", config.Panel.LogPath)
err = http.ListenAndServe(fmt.Sprintf(":%d", config.Panel.Port), logRequest(http.DefaultServeMux))
if err != nil {
log.Fatal(err)
}
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
@ -127,15 +154,24 @@ func handler(w http.ResponseWriter, r *http.Request) {
var tmpl string
switch path {
case "/":
tmpl = "templates/home.html"
t, _ := template.ParseFiles(tmpl)
t.Execute(w, config)
default:
tmpl = fmt.Sprintf("templates%s", path)
userid, err := r.Cookie("user-id")
if errors.As(err, &http.ErrNoCookie) {
tmpl = "templates/login.html"
t, _ := template.ParseFiles(tmpl)
t.Execute(w, config)
} else {
fmt.Println(userid)
switch path {
case "/":
tmpl = "templates/home.html"
t, _ := template.ParseFiles(tmpl)
t.Execute(w, config)
default:
tmpl = fmt.Sprintf("templates%s", path)
t, _ := template.ParseFiles(tmpl)
t.Execute(w, config)
}
}
}
@ -160,28 +196,3 @@ func loginEndpoint(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("code")
fmt.Print(token)
}
func main() {
err := loadConfig()
if err != nil {
log.Fatalf("couldn't load config: %s", err)
}
openLogFile(config.Panel.LogPath)
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
http.HandleFunc("/login/", loginHandler)
http.Handle("/resources/",
http.StripPrefix("/resources/",
http.FileServer(http.Dir("./resources"))))
http.HandleFunc("/", handler)
fmt.Printf("listening on %v\n", config.Panel.Port)
fmt.Printf("Logging to %v\n", config.Panel.LogPath)
err = http.ListenAndServe(fmt.Sprintf(":%d", config.Panel.Port), logRequest(http.DefaultServeMux))
if err != nil {
log.Fatal(err)
}
}