Compare commits

...

4 Commits

Author SHA1 Message Date
leah 5525e96125 rename some files 2022-01-29 14:03:02 +00:00
leah aa95d5404f bump go-sqlite3 version 2022-01-29 14:02:56 +00:00
leah d8aef83244 shuffle some bits around 2022-01-29 14:02:37 +00:00
leah 5a7c186694 check for (some) bad data 2022-01-29 14:02:15 +00:00
5 changed files with 91 additions and 47 deletions

2
go.mod
View File

@ -7,7 +7,7 @@ require (
gorm.io/gorm v1.22.5
)
require github.com/mattn/go-sqlite3 v1.14.9 // indirect
require github.com/mattn/go-sqlite3 v1.14.11 // indirect
require (
github.com/jinzhu/inflection v1.0.0 // indirect

5
go.sum
View File

@ -1,4 +1,5 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
@ -7,8 +8,12 @@ github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA=
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.11 h1:gt+cp9c0XGqe9S/wAHTL3n/7MqY+siPWgWJgqdsFrzQ=
github.com/mattn/go-sqlite3 v1.14.11/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

90
main.go
View File

@ -1,11 +1,13 @@
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"text/template"
@ -48,6 +50,21 @@ func loadConfig() (err error) {
err = yaml.Unmarshal(configfile, &config)
// todo: check for bad data !!
var re = regexp.MustCompile(`(((ftp|http|https):\/\/)|(\/)|(..\/))(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?`)
tocheck := [2]string{config.Panel.Host, config.Auth.GiteaURL}
for _, val := range tocheck {
if len(re.FindStringIndex(val)) == 0 {
log.Fatalf(`"%s" is not a valid url.`, val)
}
}
if config.Panel.Port > 65535 {
log.Fatalf("port is too big (max 65535)")
}
// init oauth bits
redirecturi = fmt.Sprintf("%s/login/endpoint", config.Panel.Host)
requesturl = fmt.Sprintf("%s/login/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code&state=STATE",
@ -77,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 {
@ -112,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)
}
}
}
@ -145,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)
}
}

View File

@ -1,23 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>{{.Server.Name}} Admin Panel</title>
<title>Page Title</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='/resources/build/main.css'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
</head>
<body class="bg-slate-200 flex min-h-screen">
<div class="login">
<form>
<h3 class="font-bold mb-6 text-lg text-center">Log in to {{.Server.Name}} Admin</h3>
<a href="/login" class="button my-10 px-16">
Log In With Gitea
</a>
</form>
</div>
<body>
wowzers
</body>
</html>

23
templates/login.html Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>{{.Server.Name}} Admin Panel</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='/resources/build/main.css'>
</head>
<body class="bg-slate-200 flex min-h-screen">
<div class="login">
<form>
<h3 class="font-bold mb-6 text-lg text-center">Log in to {{.Server.Name}} Admin</h3>
<a href="/login" class="button my-10 px-16">
Log In With Gitea
</a>
</form>
</div>
</body>
</html>