2025-01-26 22:23:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2025-07-30 09:35:01 +01:00
|
|
|
"crypto/rand"
|
2025-06-18 22:18:12 +01:00
|
|
|
"flag"
|
2025-01-26 22:23:42 +00:00
|
|
|
"log"
|
2025-06-17 22:06:56 +01:00
|
|
|
"net"
|
2025-01-26 22:23:42 +00:00
|
|
|
"net/http"
|
2025-06-26 22:42:03 +01:00
|
|
|
"os"
|
2025-06-27 20:39:17 +01:00
|
|
|
"time"
|
2025-01-26 22:23:42 +00:00
|
|
|
|
2025-06-01 21:27:08 +01:00
|
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
2025-06-27 20:39:17 +01:00
|
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/middleware"
|
2025-06-01 21:27:08 +01:00
|
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/notes/views"
|
2025-01-26 22:23:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2025-06-18 22:18:12 +01:00
|
|
|
var confFile string
|
|
|
|
|
|
2025-07-30 09:35:01 +01:00
|
|
|
cache := make(map[string]string, 20)
|
|
|
|
|
|
2025-06-24 19:48:55 +01:00
|
|
|
flag.StringVar(&confFile, "c", "/etc/gonotes/conf.toml", "Specify path to config file.")
|
2025-06-18 22:18:12 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
conf.LoadConfig(confFile)
|
2025-01-26 22:23:42 +00:00
|
|
|
|
2025-06-26 22:42:03 +01:00
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
|
|
2025-01-26 22:23:42 +00:00
|
|
|
router := http.NewServeMux()
|
|
|
|
|
notesRouter := views.GetRoutes("/notes")
|
|
|
|
|
|
2025-06-27 20:39:17 +01:00
|
|
|
cacheExpiration, err := time.ParseDuration("24h")
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
etag := middleware.NewETag("static", cacheExpiration)
|
|
|
|
|
|
2025-07-30 09:35:01 +01:00
|
|
|
if !conf.Conf.Production {
|
|
|
|
|
router.HandleFunc("/login/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
user := r.FormValue("user")
|
|
|
|
|
log.Printf("Trying to log in %s", user)
|
|
|
|
|
|
|
|
|
|
sessionID := rand.Text()
|
|
|
|
|
cache[sessionID] = user
|
|
|
|
|
|
|
|
|
|
cookie := http.Cookie{
|
|
|
|
|
Name: "id", Value: sessionID, MaxAge: 3600,
|
|
|
|
|
Secure: true, HttpOnly: true, Path: "/",
|
|
|
|
|
}
|
|
|
|
|
http.SetCookie(w, &cookie)
|
|
|
|
|
http.Redirect(w, r, "/notes/", http.StatusFound)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 20:42:07 +01:00
|
|
|
router.Handle("/", middleware.LoggingMiddleware(http.RedirectHandler("/notes/", http.StatusFound)))
|
2025-07-30 09:35:01 +01:00
|
|
|
router.Handle("/notes/", middleware.SessionMiddleware(cache, middleware.LoggingMiddleware(http.StripPrefix("/notes", notesRouter))))
|
2025-01-28 22:08:01 +00:00
|
|
|
router.Handle(
|
2025-06-25 21:30:57 +01:00
|
|
|
"/static/",
|
2025-06-27 20:42:07 +01:00
|
|
|
middleware.LoggingMiddleware(
|
2025-06-27 20:39:17 +01:00
|
|
|
middleware.StaticEtagMiddleware(
|
|
|
|
|
*etag,
|
|
|
|
|
http.FileServer(http.FS(conf.Static)),
|
|
|
|
|
),
|
2025-01-28 22:08:01 +00:00
|
|
|
),
|
|
|
|
|
)
|
2025-06-17 22:06:56 +01:00
|
|
|
|
|
|
|
|
listener, err := net.Listen(conf.Conf.Protocol, conf.Conf.Address)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
log.Fatal(http.Serve(listener, router))
|
2025-01-26 22:23:42 +00:00
|
|
|
}
|