From 795397aa82f44755f2d01ece18ec057e2836b7fd Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Wed, 7 Apr 2021 11:29:21 +0200 Subject: [PATCH] trying to come up with a good go pkg structure --- app/index/handler.go | 13 +++++++------ app/routes.go | 6 +++++- app/server.go | 24 +++++++++++++++++++++--- app/webmention/handler.go | 14 ++++++++++++++ common/config.go | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 app/webmention/handler.go create mode 100644 common/config.go diff --git a/app/index/handler.go b/app/index/handler.go index 9c55d55..83e13bd 100644 --- a/app/index/handler.go +++ b/app/index/handler.go @@ -4,13 +4,14 @@ package index import ( "net/http" "fmt" + + "github.com/wgroeneveld/go-jamming/common" ) -func HandleIndex(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - fmt.Printf("testje") +func Handle(conf *common.Config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + fmt.Printf("testje") + } } -//func (s *server) handleIndex() http.HandlerFunc { - -//} diff --git a/app/routes.go b/app/routes.go index 014b6b6..f743122 100644 --- a/app/routes.go +++ b/app/routes.go @@ -3,8 +3,12 @@ package app import ( "github.com/wgroeneveld/go-jamming/app/index" + "github.com/wgroeneveld/go-jamming/app/webmention" ) +// stole bits from https://pace.dev/blog/2018/05/09/how-I-write-http-services-after-eight-years.html +// not that contempt with passing conf, but can't create receivers on non-local types, and won't move specifics into package app func (s *server) routes() { - s.router.HandleFunc("/", index.HandleIndex) + s.router.HandleFunc("/", index.Handle(s.conf)) + s.router.HandleFunc("/webmention/{domain}/{token}", s.authorizedOnly(webmention.Handle(s.conf))) } diff --git a/app/server.go b/app/server.go index c899c43..68e3124 100644 --- a/app/server.go +++ b/app/server.go @@ -3,22 +3,40 @@ package app import ( "fmt" + "strconv" "net/http" "github.com/gorilla/mux" + + "github.com/wgroeneveld/go-jamming/common" ) type server struct { router *mux.Router + conf *common.Config +} + +// mimicing NotFound: https://golang.org/src/net/http/server.go?s=64787:64830#L2076 +func unauthorized(w http.ResponseWriter, r *http.Request) { http.Error(w, "401 unauthorized", http.StatusUnauthorized) } + +func (s *server) authorizedOnly(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + if vars["token"] != s.conf.Token { + unauthorized(w, r) + return + } + h(w, r) + } } func Start() { r := mux.NewRouter() - server := &server{router: r} + server := &server{router: r, conf: common.Configure()} server.routes() http.Handle("/", r) - fmt.Printf("Serving at port 1337...\n") - http.ListenAndServe(":1337", nil) + fmt.Printf("Serving at port %d...\n", server.conf.Port) + http.ListenAndServe(":" + strconv.Itoa(server.conf.Port), nil) } diff --git a/app/webmention/handler.go b/app/webmention/handler.go new file mode 100644 index 0000000..7afb38b --- /dev/null +++ b/app/webmention/handler.go @@ -0,0 +1,14 @@ + +package webmention + +import ( + "net/http" + + "github.com/wgroeneveld/go-jamming/common" +) + +func Handle(conf *common.Config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + } +} + diff --git a/common/config.go b/common/config.go new file mode 100644 index 0000000..d4e446e --- /dev/null +++ b/common/config.go @@ -0,0 +1,36 @@ + +package common + +import ( + "os" + "strconv" +) + +type Config struct { + Port int + Token string + UtcOffset int + AllowedWebmentionSources []string + DisallowedWebmentionDomains []string +} + +func Configure() (c *Config) { + portstr := os.Getenv("PORT") + port, err := strconv.Atoi(portstr) + if err != nil { + port = 1337 + } + token := os.Getenv("TOKEN") + if token == "" { + token = "miauwkes" + } + + c = &Config{ + Port: port, + Token: token, + UtcOffset: 60, + AllowedWebmentionSources: []string{ "brainbaking.com", "jefklakscodex.com" }, + DisallowedWebmentionDomains: []string{ "youtube.com" }, + } + return +}