gonotes/cmd/server/main.go

49 lines
1,016 B
Go
Raw Normal View History

package main
import (
"flag"
"log"
"net"
"net/http"
2025-06-01 21:27:08 +01:00
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
"forgejo.gwairfelin.com/max/gonotes/internal/notes/views"
)
func main() {
var confFile string
2025-06-24 19:48:55 +01:00
flag.StringVar(&confFile, "c", "/etc/gonotes/conf.toml", "Specify path to config file.")
flag.Parse()
conf.LoadConfig(confFile)
router := http.NewServeMux()
notesRouter := views.GetRoutes("/notes")
2025-06-24 22:13:05 +01:00
router.Handle("/", http.RedirectHandler("/notes/", http.StatusFound))
router.Handle("/notes/", http.StripPrefix("/notes", notesRouter))
2025-01-28 22:08:01 +00:00
router.Handle(
2025-06-25 21:30:57 +01:00
"/static/",
2025-01-28 22:08:01 +00:00
logger(
http.StripPrefix(
"/static",
http.FileServer(http.Dir(conf.Conf.Static.Dir)),
),
),
)
listener, err := net.Listen(conf.Conf.Protocol, conf.Conf.Address)
if err != nil {
log.Fatal(err)
}
log.Fatal(http.Serve(listener, router))
}
2025-01-28 22:08:01 +00:00
func logger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Print(r.URL.Path)
next.ServeHTTP(w, r)
})
}