2020-05-10 01:20:35 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Rodrigue Chakode and contributors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2020-05-23 23:25:12 +02:00
|
|
|
|
2020-05-10 01:20:35 +02:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-05-10 00:02:18 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-05-14 14:58:22 +02:00
|
|
|
"os"
|
2020-05-23 23:25:12 +02:00
|
|
|
"time"
|
2021-09-19 22:49:48 +02:00
|
|
|
"log"
|
2020-05-10 00:02:18 +02:00
|
|
|
|
2020-05-23 23:25:12 +02:00
|
|
|
"github.com/spf13/viper"
|
2020-05-10 00:02:18 +02:00
|
|
|
)
|
|
|
|
|
2020-05-10 01:20:35 +02:00
|
|
|
type Route struct {
|
2020-06-01 19:48:13 +02:00
|
|
|
Name string
|
|
|
|
Method string
|
|
|
|
Pattern string
|
|
|
|
Handler http.Handler
|
2020-05-10 01:20:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Routes []Route
|
|
|
|
|
|
|
|
var routes = Routes{
|
2020-05-23 23:25:12 +02:00
|
|
|
Route{
|
2020-05-10 01:20:35 +02:00
|
|
|
"SendMail",
|
|
|
|
"POST",
|
|
|
|
"/sendmail",
|
2020-06-02 15:27:08 +02:00
|
|
|
MuxSecAllowedDomainsHandler(
|
|
|
|
MuxSecReCaptchaHandler(
|
|
|
|
http.HandlerFunc(SendMail))),
|
2020-05-10 01:20:35 +02:00
|
|
|
},
|
2020-05-23 23:25:12 +02:00
|
|
|
Route{
|
|
|
|
"Healthz",
|
|
|
|
"GET",
|
2021-09-19 22:49:48 +02:00
|
|
|
"/",
|
2020-06-01 19:48:13 +02:00
|
|
|
http.HandlerFunc(Healthz),
|
2020-05-23 23:25:12 +02:00
|
|
|
},
|
2020-05-10 01:20:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func MuxLoggerHandler(inner http.Handler, name string) http.Handler {
|
2020-05-23 23:25:12 +02:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
start := time.Now()
|
|
|
|
inner.ServeHTTP(w, r)
|
|
|
|
log.Printf(
|
|
|
|
"%s %s %s %s",
|
|
|
|
r.Method,
|
|
|
|
r.RequestURI,
|
|
|
|
name,
|
|
|
|
time.Since(start),
|
|
|
|
)
|
|
|
|
})
|
2020-05-10 01:20:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 00:02:18 +02:00
|
|
|
func main() {
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
viper.SetDefault("SERVER_TLS_CERT", "/etc/cert/cert.pem")
|
|
|
|
viper.SetDefault("SERVER_TLS_PRIVATEKEY", "/etc/cert/privkey.pem")
|
|
|
|
viper.SetDefault("SMTP_SERVER_ADDR", "127.0.0.1:465")
|
|
|
|
viper.SetDefault("SMTP_CLIENT_USERNAME", "")
|
|
|
|
viper.SetDefault("SMTP_CLIENT_PASSWORD", "")
|
|
|
|
viper.SetDefault("CONTACT_REPLY_EMAIL", "noreply@company.com")
|
2022-04-28 19:36:46 +02:00
|
|
|
viper.SetDefault("CONTACT_REPLY_BCC_EMAIL", "contact@company.com")
|
2020-05-10 00:02:18 +02:00
|
|
|
viper.SetDefault("EMAIL_SUBJECT", "Thanks to try our product")
|
|
|
|
viper.SetDefault("DEMO_URL", "http://company.com/product-demo")
|
2022-04-30 11:19:46 +02:00
|
|
|
viper.SetDefault("SMTP_AUTHENTICATION_ENABLED", true)
|
2020-05-10 00:02:18 +02:00
|
|
|
|
2022-04-28 19:49:02 +02:00
|
|
|
host := os.Getenv("HOST")
|
2020-05-14 14:58:22 +02:00
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "8080"
|
2021-09-19 22:49:48 +02:00
|
|
|
log.Printf("Defaulting to port %s", port)
|
2020-05-14 14:58:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 22:49:48 +02:00
|
|
|
for _, route := range routes {
|
|
|
|
handler := MuxLoggerHandler(route.Handler, route.Name)
|
|
|
|
http.Handle(route.Pattern, handler)
|
|
|
|
}
|
2020-05-10 00:02:18 +02:00
|
|
|
|
2021-09-19 22:49:48 +02:00
|
|
|
log.Printf("Listening on port %s", port)
|
2020-05-10 01:20:35 +02:00
|
|
|
|
2022-04-28 19:49:02 +02:00
|
|
|
log.Fatal(http.ListenAndServe(host+":"+port, nil))
|
2020-05-10 00:02:18 +02:00
|
|
|
}
|