53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/middleware"
|
|
"forgejo.gwairfelin.com/max/gonotes/internal/notes/views"
|
|
)
|
|
|
|
func main() {
|
|
var confFile string
|
|
|
|
flag.StringVar(&confFile, "c", "/etc/gonotes/conf.toml", "Specify path to config file.")
|
|
flag.Parse()
|
|
|
|
conf.LoadConfig(confFile)
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
router := http.NewServeMux()
|
|
notesRouter := views.GetRoutes("/notes")
|
|
|
|
cacheExpiration, err := time.ParseDuration("24h")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
etag := middleware.NewETag("static", cacheExpiration)
|
|
|
|
router.Handle("/", middleware.LoggingMiddleware(http.RedirectHandler("/notes/", http.StatusFound)))
|
|
router.Handle("/notes/", middleware.LoggingMiddleware(http.StripPrefix("/notes", notesRouter)))
|
|
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))
|
|
}
|