master
bel 2023-06-17 09:28:16 -06:00
parent 7777d2782a
commit fe4e9c4214
3 changed files with 84 additions and 25 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"context"
_ "embed"
"encoding/base64"
"encoding/json"
"errors"
"flag"
@ -11,22 +12,24 @@ import (
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
)
var (
Config struct {
Port int
SessionD string
Debug bool
Semaphore sync.Mutex
Port int
SessionD string
Debug bool
}
//go:embed template.d/login.html
htmlLogin []byte
//go:embed template.d/index.html
htmlIndex []byte
//go:embed template.d/chatbot.html
htmlChatBot []byte
)
func main() {
@ -100,30 +103,46 @@ func listenAndServe(ctx context.Context) {
func handle(w http.ResponseWriter, r *http.Request) {
cookie, _ := ParseCookie(r)
if err := _handle(w, r); err != nil {
log.Printf("%s: %s: %v", cookie.Name, r.URL.Path, err)
log.Printf("%s: %s %s: %v", cookie.Name, r.Method, r.URL.Path, err)
} else {
log.Printf("%s: %s", cookie.Name, r.URL.Path)
log.Printf("%s: %s %s", cookie.Name, r.Method, r.URL.Path)
}
}
func _handle(w http.ResponseWriter, r *http.Request) error {
switch r.URL.Path {
case "/login":
first := strings.Split(strings.TrimLeft(r.URL.Path, "/"), "/")[0]
switch first {
case "login":
return handleLogin(w, r)
case "/":
return handleUI(w, r)
default:
http.NotFound(w, r)
return nil
return handleLoggedIn(w, r)
}
}
func handleLogin(w http.ResponseWriter, r *http.Request) error {
w.Write(htmlLogin)
return nil
switch r.Method {
case http.MethodGet:
w.Write(htmlLogin)
return nil
case http.MethodPost:
err := r.ParseForm()
if err != nil {
return err
}
cookie, err := ParseCookie(r)
if err != nil {
return err
}
cookie.Serialize(w)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return nil
default:
http.NotFound(w, r)
return errors.New("not found")
}
}
func handleUI(w http.ResponseWriter, r *http.Request) error {
func handleLoggedIn(w http.ResponseWriter, r *http.Request) error {
cookie, err := ParseCookie(r)
if err != nil {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
@ -131,10 +150,17 @@ func handleUI(w http.ResponseWriter, r *http.Request) error {
}
_ = cookie
Config.Semaphore.Lock()
defer Config.Semaphore.Unlock()
return errors.New("not impl")
switch r.URL.Path {
case "/":
w.Write(htmlIndex)
return nil
case "/chatbot":
w.Write(htmlChatBot)
return nil
default:
http.NotFound(w, r)
return errors.New("not found: " + r.URL.Path)
}
}
type Cookie struct {
@ -142,28 +168,47 @@ type Cookie struct {
}
func ParseCookie(r *http.Request) (Cookie, error) {
if r.URL.Path != "/login" {
return parseCookieFromCookie(r)
}
cookie := Cookie{
Name: r.PostForm.Get("Name"),
}
return cookie, cookie.Verify()
}
func parseCookieFromCookie(r *http.Request) (Cookie, error) {
cookie, err := r.Cookie("root")
if err != nil {
return Cookie{}, err
}
var result Cookie
if err := json.Unmarshal([]byte(cookie.Value), &result); err != nil {
decoded, err := base64.URLEncoding.DecodeString(cookie.Value)
if err != nil {
return Cookie{}, err
}
if result.Name == "" {
return Cookie{}, errors.New("incomplete cookie")
var result Cookie
if err := json.Unmarshal(decoded, &result); err != nil {
return Cookie{}, err
}
return result, nil
return result, result.Verify()
}
func (cookie Cookie) Verify() error {
if cookie.Name == "" {
return errors.New("incomplete cookie")
}
return nil
}
func (cookie Cookie) Serialize(w http.ResponseWriter) {
b, _ := json.Marshal(cookie)
encoded := base64.URLEncoding.EncodeToString(b)
c := &http.Cookie{
Name: "root",
Value: string(b),
Value: encoded,
}
http.SetCookie(w, c)
}

View File

@ -0,0 +1,9 @@
<html>
<header>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
</header>
<body>
</body>
<footer>
</footer>
</html>

View File

@ -3,6 +3,11 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
</header>
<body>
<form method="POST">
<label for="Name">Your Name</label>
<input name="Name" type="text"/>
<button type="submit">Sign In</button>
</form>
</body>
<footer>
</footer>