Basic web server startup

This commit is contained in:
Justin Hawkins 2017-07-26 22:40:21 +09:30
parent ab54ace0d2
commit 497d2e3e27
2 changed files with 33 additions and 7 deletions

5
dau.go
View File

@ -45,12 +45,11 @@ type Config struct {
func main() {
config := parseOptions()
checkPath(config.path)
checkUpdates()
web.Init()
checkUpdates()
log.Print("Waiting for images to appear in ", config.path)
// wander the path, forever
for {

View File

@ -1,8 +1,35 @@
package web
// web server for the discord-auto-upload package
import (
"fmt"
"log"
"net/http"
"strings"
)
// Init - start the web server
func Init() {
return
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, ""))
}
fmt.Fprintf(w, "Response!") // send data to client side
}
// Init is great
func Init() {
go startWebServer()
}
func startWebServer() {
http.HandleFunc("/", sayhelloName) // set router
log.Print("Starting web server on http://localhost:9090")
err := http.ListenAndServe(":9090", nil) // set listen port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}