forked from wgroeneveld/go-jamming
trying to come up with a good go pkg structure
This commit is contained in:
parent
42dd3fbe83
commit
795397aa82
|
@ -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 {
|
||||
|
||||
//}
|
||||
|
|
|
@ -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)))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue