Initial commit
This commit is contained in:
126
main.go
Normal file
126
main.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
var StaticDir string = "./static/"
|
||||
|
||||
var myLDAP = LDAP{
|
||||
Host: "localhost",
|
||||
Port: 389,
|
||||
BaseDN: "dc=example,dc=com",
|
||||
}
|
||||
|
||||
|
||||
type ResponseWriterPrefix struct {
|
||||
real http.ResponseWriter
|
||||
prefix string
|
||||
}
|
||||
|
||||
func (r ResponseWriterPrefix) Header() http.Header {
|
||||
return r.real.Header()
|
||||
}
|
||||
|
||||
func (r ResponseWriterPrefix) WriteHeader(s int) {
|
||||
if v, exists := r.real.Header()["Location"]; exists {
|
||||
r.real.Header().Set("Location", r.prefix + v[0])
|
||||
}
|
||||
r.real.WriteHeader(s)
|
||||
}
|
||||
|
||||
func (r ResponseWriterPrefix) Write(z []byte) (int, error) {
|
||||
return r.real.Write(z)
|
||||
}
|
||||
|
||||
func StripPrefix(prefix string, h http.Handler) http.Handler {
|
||||
if prefix == "" {
|
||||
return h
|
||||
}
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if prefix != "/" && r.URL.Path == "/" {
|
||||
http.Redirect(w, r, prefix + "/", http.StatusFound)
|
||||
} else if p := strings.TrimPrefix(r.URL.Path, prefix); len(p) < len(r.URL.Path) {
|
||||
r2 := new(http.Request)
|
||||
*r2 = *r
|
||||
r2.URL = new(url.URL)
|
||||
*r2.URL = *r.URL
|
||||
r2.URL.Path = p
|
||||
h.ServeHTTP(ResponseWriterPrefix{w, prefix}, r2)
|
||||
} else {
|
||||
h.ServeHTTP(w, r)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
var bind = flag.String("bind", "127.0.0.1:8080", "Bind port/socket")
|
||||
var baseURL = flag.String("baseurl", "/", "URL prepended to each URL")
|
||||
flag.StringVar(&StaticDir, "static", StaticDir, "Directory containing static files")
|
||||
flag.Parse()
|
||||
|
||||
// Sanitize options
|
||||
var err error
|
||||
log.Println("Checking paths...")
|
||||
if StaticDir, err = filepath.Abs(StaticDir); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if *baseURL != "/" {
|
||||
tmp := path.Clean(*baseURL)
|
||||
baseURL = &tmp
|
||||
} else {
|
||||
tmp := ""
|
||||
baseURL = &tmp
|
||||
}
|
||||
|
||||
// Load config file
|
||||
if fd, err := os.Open("config.json"); err != nil {
|
||||
log.Fatal(err)
|
||||
} else if cnt, err := ioutil.ReadAll(fd); err != nil {
|
||||
log.Fatal(err)
|
||||
} else if err := json.Unmarshal(cnt, &myLDAP); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Prepare graceful shutdown
|
||||
interrupt := make(chan os.Signal, 1)
|
||||
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
// Register handlers
|
||||
http.HandleFunc(fmt.Sprintf("%s/", *baseURL), changePassword)
|
||||
http.Handle(fmt.Sprintf("%s/css/", *baseURL), http.StripPrefix(*baseURL, http.FileServer(http.Dir(StaticDir))))
|
||||
http.HandleFunc(fmt.Sprintf("%s/login", *baseURL), tryLogin)
|
||||
http.HandleFunc(fmt.Sprintf("%s/change", *baseURL), changePassword)
|
||||
http.HandleFunc(fmt.Sprintf("%s/reset", *baseURL), resetPassword)
|
||||
http.HandleFunc(fmt.Sprintf("%s/lost", *baseURL), lostPassword)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: *bind,
|
||||
}
|
||||
|
||||
// Serve content
|
||||
go func() {
|
||||
log.Fatal(srv.ListenAndServe())
|
||||
}()
|
||||
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
|
||||
|
||||
// Wait shutdown signal
|
||||
<-interrupt
|
||||
|
||||
log.Print("The service is shutting down...")
|
||||
srv.Shutdown(context.Background())
|
||||
log.Println("done")
|
||||
}
|
||||
Reference in New Issue
Block a user