guichet/main.go

94 lines
1.7 KiB
Go
Raw Normal View History

2020-02-09 13:46:52 +00:00
package main
import (
"os"
2020-02-09 14:01:20 +00:00
"flag"
"fmt"
2020-02-09 13:46:52 +00:00
"log"
"net/http"
2020-02-09 14:01:20 +00:00
"io/ioutil"
"encoding/json"
"encoding/base64"
"crypto/rand"
2020-02-09 13:46:52 +00:00
"github.com/gorilla/sessions"
)
2020-02-09 14:01:20 +00:00
type ConfigFile struct {
HttpBindAddr string `json:"http_bind_addr"`
SessionKey string `json:"session_key"`
LdapServerAddr string `json:"ldap_server_addr"`
}
2020-02-09 13:46:52 +00:00
2020-02-09 14:01:20 +00:00
var configFlag = flag.String("config", "./config.json", "Configuration file path")
func readConfig() ConfigFile {
key_bytes := make([]byte, 32)
n, err := rand.Read(key_bytes)
if err!= nil || n != 32 {
log.Fatal(err)
}
config_file := ConfigFile{
HttpBindAddr: ":9991",
SessionKey: base64.StdEncoding.EncodeToString(key_bytes),
LdapServerAddr: "127.0.0.1:389",
}
_, err = os.Stat(*configFlag)
if os.IsNotExist(err) {
// Generate default config file
log.Printf("Generating default config file as %s", *configFlag)
bytes, err := json.MarshalIndent(&config_file, "", " ")
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(*configFlag, bytes, 0644)
if err != nil {
log.Fatal(err)
}
return config_file
}
if err != nil {
log.Fatal(err)
}
bytes, err := ioutil.ReadFile(*configFlag)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(bytes, &config_file)
if err != nil {
log.Fatal(err)
}
return config_file
2020-02-09 13:46:52 +00:00
}
2020-02-09 14:01:20 +00:00
var store *sessions.CookieStore = nil
2020-02-09 13:46:52 +00:00
func main() {
2020-02-09 14:01:20 +00:00
flag.Parse()
2020-02-09 13:46:52 +00:00
2020-02-09 14:01:20 +00:00
config := readConfig()
store = sessions.NewCookieStore([]byte(config.SessionKey))
http.HandleFunc("/", handleHome)
2020-02-09 13:46:52 +00:00
2020-02-09 14:01:20 +00:00
err := http.ListenAndServe(config.HttpBindAddr, nil)
2020-02-09 13:46:52 +00:00
if err != nil {
log.Fatal("Cannot start http server: ", err)
}
}
2020-02-09 14:01:20 +00:00
// Page handlers ----
func handleHome(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, world!")
}