Skip to content

Commit

Permalink
allow to run in subpath
Browse files Browse the repository at this point in the history
  • Loading branch information
msiegenthaler committed Nov 11, 2020
1 parent f77c332 commit 283bd34
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Run by calling the executable with the following parameters (env or command line
- `password`: Password that is used to connect via POP3 (required)
- `token`: Token that is required to submit via webhook (required)")
- `http-interface`: The address to listen on for HTTP requests. Default: `localhost:8080`
- `base-path`: The base HTTP path the webhook should run under. Default: `/`
- `pop-interface`: Interface (host:port) to listen on. Default: `localhost:1100`
- `mail-dir`: Directory to store the mail in. Required unless in-memory-only is set
- `in-memory-only`: Set to `true` to not persist the messages to disk. Default: `false`
Expand Down
8 changes: 6 additions & 2 deletions cmd/pop3-webhook-server/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/gorilla/mux"
"github.com/namsral/flag"
"github.com/DevelHell/popgun"
"github.com/linkyard/pop3-webhook-server/internal/backends"
Expand All @@ -16,6 +17,7 @@ var user = flag.String("user", "mail", "Username that can be used to connect via
var password = flag.String("password", "", "Password that is used to connect via POP3 (required)")
var token = flag.String("token", "", "Token that is required to submit via webhook (required)")
var httpAddr = flag.String("http-interface", "localhost:8080", "The address to listen on for HTTP requests.")
var basePath = flag.String("base-path", "", "Path the http interface is available under. Default: ''")
var popAddr = flag.String("pop-interface", "localhost:1100", "Interface (host:port) to listen on")
var maildir = flag.String("mail-dir", "", "Directory to store the mail in (required unless --in-memory-only)")
var inmem = flag.String("in-memory-only", "false", "Set to true to not persist the messages to disk (default: false)")
Expand Down Expand Up @@ -76,8 +78,10 @@ func main() {
}

httpHandler := webhook.NewReceiveMailHandler(*token, &mailStore)
log.Infof("Started http server on %s (use /mail/v1/store)", *httpAddr)
err = http.ListenAndServe(*httpAddr, httpHandler.Handler())
router := mux.NewRouter().StrictSlash(false)
httpHandler.Register(router.PathPrefix(*basePath).Subrouter())
log.Infof("Started http server on %s (use %s/store)", *httpAddr, *basePath)
err = http.ListenAndServe(*httpAddr, router)
if err != nil {
log.Fatal(err)
os.Exit(1)
Expand Down
2 changes: 2 additions & 0 deletions internal/webhook/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func jsonResponse(f func(request *http.Request) (interface{}, httpError)) func(h
var writeErr error
if err == nil {
log.Debugf("Request to %s was successful", req.RequestURI)
resp.Header().Set("Content-Type", "application/json")
resp.WriteHeader(200)
writeErr = json.NewEncoder(resp).Encode(result)
} else {
Expand All @@ -58,6 +59,7 @@ func jsonResponse(f func(request *http.Request) (interface{}, httpError)) func(h
Code: err.ErrorCode(),
Detail: err.Detail(),
}
resp.Header().Set("Content-Type", "application/json")
resp.WriteHeader(err.ErrorCode())
writeErr = json.NewEncoder(resp).Encode(result)
}
Expand Down
30 changes: 27 additions & 3 deletions internal/webhook/mailhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,34 @@ func NewReceiveMailHandler(token string, mailStore *MailStore) ReceiveMailHandle
}
}

func (r ReceiveMailHandler) Handler() *mux.Router {
router := mux.NewRouter().StrictSlash(false)
func (r ReceiveMailHandler) Register(router *mux.Router) {
router.HandleFunc("/", jsonResponse(index))
router.HandleFunc("/health", jsonResponse(status))
router.HandleFunc("/store", jsonResponse(r.store))
return router
router.PathPrefix("/").HandlerFunc(jsonResponse(notFound))
}

func index(_ *http.Request) (interface{}, httpError) {
return indexResponse{Info: "Hello from pop3-webhook-server. Use POST on store to submit a message"}, nil
}

type indexResponse struct {
Info string `json:"info"`
}

func status(_ *http.Request) (interface{}, httpError) {
return statusResponse{Status: "ok"}, nil
}

type statusResponse struct {
Status string `json:"status"`
}

func notFound(_ *http.Request) (interface{}, httpError) {
return nil, clientError{
detail: "not found",
errorCode: 404,
}
}

func (r ReceiveMailHandler) store(req *http.Request) (interface{}, httpError) {
Expand Down

0 comments on commit 283bd34

Please sign in to comment.