42 lines
1.0 KiB
Go
Executable File
42 lines
1.0 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"local/system/sysconf"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
var serviceName = sysconf.Get("watchmans").Name
|
|
|
|
const secretError = http.StatusOK
|
|
|
|
func redirHandle(w http.ResponseWriter, r *http.Request) {
|
|
host := strings.Split(r.Host, ":")[0] + ":" + strings.Split(sysconf.Get(serviceName).Port, ",")[0]
|
|
target := "https://" + host + r.URL.Path
|
|
if len(r.URL.RawQuery) > 0 {
|
|
target += "?" + r.URL.RawQuery
|
|
}
|
|
log.Print(target)
|
|
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
|
|
}
|
|
|
|
func main() {
|
|
conf := sysconf.Get(serviceName)
|
|
ports := strings.Split(conf.Port, ",")
|
|
if len(ports) < 2 {
|
|
panic("not enough ports in sysconf")
|
|
}
|
|
log.Print("making new API")
|
|
api, err := newAPI(conf)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Printf("Listening for http on %v", ports[0])
|
|
go func() {
|
|
log.Fatal(http.ListenAndServe(":"+ports[0], http.HandlerFunc(redirHandle)))
|
|
}()
|
|
log.Printf("Listening for https on %v", ports[1])
|
|
log.Fatal(http.ListenAndServeTLS(":"+ports[1], "./cert.pem", "./key.pem", api))
|
|
}
|