Start fleshing out web server
This commit is contained in:
parent
80a905b7d6
commit
f976777f40
11
dau.go
11
dau.go
@ -46,7 +46,8 @@ func main() {
|
||||
|
||||
config := parseOptions()
|
||||
checkPath(config.path)
|
||||
web.Init()
|
||||
wconfig := web.Init()
|
||||
go processWebChanges(wconfig)
|
||||
|
||||
checkUpdates()
|
||||
|
||||
@ -63,6 +64,14 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func processWebChanges(wc web.DAUWebServer) {
|
||||
for {
|
||||
change := <-wc.ConfigChange
|
||||
log.Print(change)
|
||||
log.Print("Got a change!")
|
||||
}
|
||||
}
|
||||
|
||||
func checkPath(path string) {
|
||||
src, err := os.Stat(path)
|
||||
if err != nil {
|
||||
|
61
web/web.go
61
web/web.go
@ -1,32 +1,67 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func sayhelloName(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm() // parse arguments, you have to call this by yourself
|
||||
fmt.Println(r.Form) // print form information in server side
|
||||
fmt.Println("path", r.URL.Path)
|
||||
fmt.Println("scheme", r.URL.Scheme)
|
||||
fmt.Println(r.Form["url_long"])
|
||||
for k, v := range r.Form {
|
||||
fmt.Println("key:", k)
|
||||
fmt.Println("val:", strings.Join(v, ""))
|
||||
// DAUWebServer - stuff for the web server
|
||||
type DAUWebServer struct {
|
||||
ConfigChange chan int
|
||||
}
|
||||
|
||||
type response struct {
|
||||
Success int
|
||||
}
|
||||
|
||||
// I am too noob to work out how to pass context around
|
||||
var wsConfig DAUWebServer
|
||||
|
||||
// r.ParseForm() // parse arguments, you have to call this by yourself
|
||||
// fmt.Println(r.Form) // print form information in server side
|
||||
// fmt.Println("path", r.URL.Path)
|
||||
// fmt.Println("scheme", r.URL.Scheme)
|
||||
// fmt.Println(r.Form["url_long"])
|
||||
// for k, v := range r.Form {
|
||||
// fmt.Println("key:", k)
|
||||
// fmt.Println("val:", strings.Join(v, ""))
|
||||
// }
|
||||
|
||||
func getIndex(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "index here")
|
||||
}
|
||||
|
||||
func getSetWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
wsConfig.ConfigChange <- 1
|
||||
goodResponse := response{1}
|
||||
js, err := json.Marshal(goodResponse)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "Response!") // send data to client side
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(js)
|
||||
}
|
||||
|
||||
func getSetDirectory(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Init is great
|
||||
func Init() {
|
||||
func Init() DAUWebServer {
|
||||
wsConfig.ConfigChange = make(chan int)
|
||||
go startWebServer()
|
||||
return wsConfig
|
||||
}
|
||||
|
||||
func startWebServer() {
|
||||
http.HandleFunc("/", sayhelloName) // set router
|
||||
|
||||
http.HandleFunc("/", getIndex)
|
||||
http.HandleFunc("/rest/config/webhook", getSetWebhook)
|
||||
http.HandleFunc("/rest/config/directory", getSetDirectory)
|
||||
|
||||
log.Print("Starting web server on http://localhost:9090")
|
||||
err := http.ListenAndServe(":9090", nil) // set listen port
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user