First HTTP API commit
This commit is contained in:
parent
d4996aaea3
commit
44a8e325c6
|
|
@ -0,0 +1,63 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PARAM_JID = "jid"
|
||||||
|
METHOD_ACCESS = "method"
|
||||||
|
DOMAIN_ACCESS = "domain"
|
||||||
|
TRANSACTION_ID = "transaction_id"
|
||||||
|
|
||||||
|
RETURN_VALUE_OK = "OK"
|
||||||
|
RETURN_VALUE_NOK = "NOK"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
HttpPortBind = 9090
|
||||||
|
|
||||||
|
ChanRequest = make(chan interface{}, 5)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// TODO
|
||||||
|
fmt.Fprintf(w, "Welcome to HTTP authentification over XMPP")
|
||||||
|
}
|
||||||
|
|
||||||
|
func authHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r.ParseForm()
|
||||||
|
chanAnswer := make(chan bool)
|
||||||
|
|
||||||
|
ChanRequest <- strings.Join(r.Form[PARAM_JID], "")
|
||||||
|
ChanRequest <- strings.Join(r.Form[METHOD_ACCESS], "")
|
||||||
|
ChanRequest <- strings.Join(r.Form[DOMAIN_ACCESS], "")
|
||||||
|
ChanRequest <- strings.Join(r.Form[TRANSACTION_ID], "")
|
||||||
|
ChanRequest <- chanAnswer
|
||||||
|
|
||||||
|
answer := <- chanAnswer
|
||||||
|
ret := RETURN_VALUE_NOK
|
||||||
|
if answer {
|
||||||
|
ret = RETURN_VALUE_OK
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func Run() {
|
||||||
|
http.HandleFunc("/", indexHandler) // set router
|
||||||
|
http.HandleFunc("/toto", authHandler)
|
||||||
|
|
||||||
|
port := strconv.Itoa(HttpPortBind)
|
||||||
|
log.Println("Listenning on port "+port)
|
||||||
|
err := http.ListenAndServe(":"+port, nil) // set listen port
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("ListenAndServe: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,67 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/http"
|
||||||
|
|
||||||
|
"github.com/jimlawless/cfg"
|
||||||
|
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Version = "v0.1.0"
|
||||||
|
configurationFilePath = "httpAuth.cfg"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
mapConfig = make(map[string]string)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
err := cfg.Load(configurationFilePath, mapConfig)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Failed to load configuration file.", err)
|
||||||
|
}
|
||||||
|
// TODO make config
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func request() {
|
||||||
|
for {
|
||||||
|
jid := <- http.ChanRequest
|
||||||
|
log.Println(jid)
|
||||||
|
method := <- http.ChanRequest
|
||||||
|
log.Println(method)
|
||||||
|
domain := <- http.ChanRequest
|
||||||
|
log.Println(domain)
|
||||||
|
transaction := <- http.ChanRequest
|
||||||
|
log.Println(transaction)
|
||||||
|
|
||||||
|
chanResult := <- http.ChanRequest
|
||||||
|
// TODO make the XMPP request
|
||||||
|
if v, ok := chanResult.(chan bool); ok {
|
||||||
|
v <- false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// TODO start ressources
|
||||||
|
go http.Run()
|
||||||
|
go request()
|
||||||
|
|
||||||
|
sigchan := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigchan, os.Interrupt)
|
||||||
|
signal.Notify(sigchan, syscall.SIGTERM)
|
||||||
|
signal.Notify(sigchan, os.Kill)
|
||||||
|
<-sigchan
|
||||||
|
|
||||||
|
// TODO close all ressources
|
||||||
|
|
||||||
|
log.Println("Exit main()")
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue