Started work on Login
This commit is contained in:
		@@ -1,11 +1,14 @@
 | 
				
			|||||||
package util
 | 
					package util
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						//"database/sql"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						//_ "github.com/mattn/go-sqlite3"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var output_channel chan string
 | 
					var output_channel chan string
 | 
				
			||||||
@@ -26,12 +29,49 @@ func StartServer(ch chan string) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func serveMcMan(w http.ResponseWriter, r *http.Request) {
 | 
					func serveMcMan(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	fmt.Fprintf(w, htmlHeader("mc_man - Minecraft Manager"))
 | 
						fmt.Fprintf(w, htmlHeader("mc_man - Minecraft Manager"))
 | 
				
			||||||
	fmt.Fprintf(w, "Hey-o! It's the manager!")
 | 
					
 | 
				
			||||||
 | 
						the_path := r.URL.Path
 | 
				
			||||||
 | 
						output_channel <- the_path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if strings.HasPrefix(the_path, "/login") || the_path == "/" {
 | 
				
			||||||
 | 
							fmt.Fprintf(w, loginScreen())
 | 
				
			||||||
 | 
						} else if strings.HasPrefix(the_path, "/dologin") {
 | 
				
			||||||
 | 
							fmt.Fprintf(w, doLogin(w, r))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fmt.Fprintf(w, htmlFooter())
 | 
						fmt.Fprintf(w, htmlFooter())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func loginScreen() string {
 | 
				
			||||||
 | 
						return `
 | 
				
			||||||
 | 
						<form action="/doLogin" method="POST">
 | 
				
			||||||
 | 
							<input type="text" name="username">
 | 
				
			||||||
 | 
							<input type="password" name="password">
 | 
				
			||||||
 | 
							<input type="submit">
 | 
				
			||||||
 | 
						</form>
 | 
				
			||||||
 | 
					`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func doLogin(w http.ResponseWriter, r *http.Request) string {
 | 
				
			||||||
 | 
						ret := "Do Login<br />"
 | 
				
			||||||
 | 
						ret = ret + r.FormValue("username")
 | 
				
			||||||
 | 
						ret = ret + r.FormValue("password")
 | 
				
			||||||
 | 
						return ret
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
							//r.F
 | 
				
			||||||
 | 
							db, err := sql.Open("sqlite3", "mc_man.db")
 | 
				
			||||||
 | 
							if err == nil {
 | 
				
			||||||
 | 
								// Error opening the DB, can't log in
 | 
				
			||||||
 | 
								return false
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							//	rows, err := db.Query("SELECT * FROM USERS")
 | 
				
			||||||
 | 
						*/
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func serveAPI(w http.ResponseWriter, r *http.Request) {
 | 
					func serveAPI(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
 | 
						_, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
 | 
				
			||||||
 | 
						//body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -41,8 +81,8 @@ func serveAPI(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
 | 
						w.Header().Set("Content-Type", "application/json; charset=UTF-8")
 | 
				
			||||||
	// What are we doing with this request?
 | 
						// What are we doing with this request?
 | 
				
			||||||
	output_channel <- fmt.Sprint("HTTP Request: (", r.Method, ") ", r.URL, "\n")
 | 
						output_channel <- fmt.Sprint("HTTP Request: (", r.Method, ") ", r.URL, "\n")
 | 
				
			||||||
	the_path = r.URL.Path
 | 
						the_path := r.URL.Path
 | 
				
			||||||
	output_string = ""
 | 
						output_string := ""
 | 
				
			||||||
	if strings.HasPrefix(the_path, "/api") {
 | 
						if strings.HasPrefix(the_path, "/api") {
 | 
				
			||||||
		the_path = strings.TrimPrefix(the_path, "/api")
 | 
							the_path = strings.TrimPrefix(the_path, "/api")
 | 
				
			||||||
		if strings.HasPrefix(the_path, "/v1") {
 | 
							if strings.HasPrefix(the_path, "/v1") {
 | 
				
			||||||
@@ -54,6 +94,7 @@ func serveAPI(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						fmt.Fprintf(w, output_string)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* JSON Functions */
 | 
					/* JSON Functions */
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user