gonotes/cmd/server/main.go

47 lines
990 B
Go

package main
import (
"flag"
"log"
"net"
"net/http"
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
"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. Default is /etc/gonotes/conf.toml")
flag.Parse()
conf.LoadConfig(confFile)
router := http.NewServeMux()
notesRouter := views.GetRoutes("/notes")
router.Handle("/notes/", http.StripPrefix("/notes", notesRouter))
router.Handle(
conf.Conf.Static.Root,
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))
}
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)
})
}