Start fleshing out web server

This commit is contained in:
Justin Hawkins 2017-07-27 12:18:02 +09:30
parent 80a905b7d6
commit f976777f40
2 changed files with 58 additions and 14 deletions

11
dau.go
View File

@ -46,7 +46,8 @@ func main() {
config := parseOptions() config := parseOptions()
checkPath(config.path) checkPath(config.path)
web.Init() wconfig := web.Init()
go processWebChanges(wconfig)
checkUpdates() 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) { func checkPath(path string) {
src, err := os.Stat(path) src, err := os.Stat(path)
if err != nil { if err != nil {

View File

@ -1,32 +1,67 @@
package web package web
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strings"
) )
func sayhelloName(w http.ResponseWriter, r *http.Request) { // DAUWebServer - stuff for the web server
r.ParseForm() // parse arguments, you have to call this by yourself type DAUWebServer struct {
fmt.Println(r.Form) // print form information in server side ConfigChange chan int
fmt.Println("path", r.URL.Path) }
fmt.Println("scheme", r.URL.Scheme)
fmt.Println(r.Form["url_long"]) type response struct {
for k, v := range r.Form { Success int
fmt.Println("key:", k) }
fmt.Println("val:", strings.Join(v, ""))
// 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 // Init is great
func Init() { func Init() DAUWebServer {
wsConfig.ConfigChange = make(chan int)
go startWebServer() go startWebServer()
return wsConfig
} }
func startWebServer() { 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") log.Print("Starting web server on http://localhost:9090")
err := http.ListenAndServe(":9090", nil) // set listen port err := http.ListenAndServe(":9090", nil) // set listen port
if err != nil { if err != nil {