65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/auth"
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/middleware"
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/notes"
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/notes/views"
|
|
)
|
|
|
|
func main() {
|
|
var confFile string
|
|
|
|
sessions := middleware.NewSessionStore()
|
|
|
|
flag.StringVar(&confFile, "c", "/etc/gonotes/conf.toml", "Specify path to config file.")
|
|
flag.Parse()
|
|
|
|
conf.LoadConfig(confFile)
|
|
|
|
err := notes.Init()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
router := http.NewServeMux()
|
|
notesRouter := views.GetRoutes("/notes")
|
|
authRouter := auth.GetRoutes("/auth", sessions.Login)
|
|
|
|
cacheExpiration, err := time.ParseDuration("24h")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
etag := middleware.NewETag("static", cacheExpiration)
|
|
|
|
router.Handle("/logout/", sessions.AsMiddleware(middleware.LoggingMiddleware(http.HandlerFunc(sessions.Logout))))
|
|
router.Handle("/", middleware.LoggingMiddleware(http.RedirectHandler("/notes/", http.StatusFound)))
|
|
router.Handle("/notes/", sessions.AsMiddleware(middleware.LoggingMiddleware(http.StripPrefix("/notes", notesRouter))))
|
|
router.Handle("/auth/", sessions.AsMiddleware(middleware.LoggingMiddleware(http.StripPrefix("/auth", authRouter))))
|
|
router.Handle(
|
|
"/static/",
|
|
middleware.LoggingMiddleware(
|
|
middleware.StaticEtagMiddleware(
|
|
*etag,
|
|
http.FileServer(http.FS(conf.Static)),
|
|
),
|
|
),
|
|
)
|
|
|
|
listener, err := net.Listen(conf.Conf.Protocol, conf.Conf.Address)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Fatal(http.Serve(listener, router))
|
|
}
|