Start building the Model
This commit is contained in:
203
util/webserver.go.disabled
Normal file
203
util/webserver.go.disabled
Normal file
@@ -0,0 +1,203 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
//"database/sql"
|
||||
"fmt"
|
||||
//_ "github.com/mattn/go-sqlite3"
|
||||
//"encoding/json"
|
||||
"github.com/boltdb/bolt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
//"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var output_channel chan string
|
||||
|
||||
type WebUser struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func StartServer(ch chan string) {
|
||||
output_channel = ch
|
||||
_, err := os.Stat("mapcrafter/index.html")
|
||||
if err == nil {
|
||||
// Looks like mapcrafter is present
|
||||
output_channel <- "* Mapcrafter Directory is Present, routing to /map\n"
|
||||
fs := http.FileServer(http.Dir("mapcrafter"))
|
||||
http.Handle("/", fs)
|
||||
}
|
||||
http.HandleFunc("/admin/", serveMcMan)
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
||||
func serveMcMan(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, htmlHeader("mc_man - Minecraft Manager"))
|
||||
|
||||
the_path := r.URL.Path
|
||||
output_channel <- the_path + "\n"
|
||||
|
||||
the_path = strings.TrimPrefix(strings.ToLower(the_path), "/admin")
|
||||
output_channel <- the_path + "\n"
|
||||
|
||||
fmt.Fprintf(w, the_path)
|
||||
if strings.HasPrefix(the_path, "/login") {
|
||||
fmt.Fprintf(w, loginScreen())
|
||||
} else if strings.HasPrefix(the_path, "/dologin") {
|
||||
fmt.Fprintf(w, doLogin(w, r))
|
||||
} else if strings.HasPrefix(the_path, "/api/") {
|
||||
fmt.Fprintf(w, serveAPI(w, r))
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func serveAPI(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
|
||||
//body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := r.Body.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
// What are we doing with this request?
|
||||
output_channel <- fmt.Sprint("HTTP Request: (", r.Method, ") ", r.URL, "\n")
|
||||
the_path := r.URL.Path
|
||||
output_string := ""
|
||||
if strings.HasPrefix(the_path, "/api") {
|
||||
the_path = strings.TrimPrefix(the_path, "/api")
|
||||
if strings.HasPrefix(the_path, "/v1") {
|
||||
the_path = strings.TrimPrefix(the_path, "/v1")
|
||||
if strings.HasPrefix(the_path, "/whitelist") {
|
||||
output_string = handleWhitelist(r)
|
||||
} else if strings.HasPrefix(the_path, "/ops") {
|
||||
output_string = handleOps(r)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(w, output_string)
|
||||
}
|
||||
|
||||
/* JSON Functions */
|
||||
func handleOps(r *http.Request) string {
|
||||
if r.Method == "GET" {
|
||||
return getOps()
|
||||
} else if r.Method == "POST" {
|
||||
// Add posted user to Ops
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func handleWhitelist(r *http.Request) string {
|
||||
if r.Method == "GET" {
|
||||
return getWhitelist()
|
||||
} else if r.Method == "POST" {
|
||||
// Add posted user to whitelist
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func getOps() string {
|
||||
ret := "["
|
||||
num_users := 0
|
||||
for _, op_user := range GetConfig().Ops {
|
||||
if num_users > 0 {
|
||||
ret += ","
|
||||
}
|
||||
ret += fmt.Sprint("\"", op_user, "\"")
|
||||
}
|
||||
ret += "]"
|
||||
return ret
|
||||
|
||||
}
|
||||
|
||||
func getWhitelist() string {
|
||||
ret := "["
|
||||
num_users := 0
|
||||
for _, wl_user := range GetConfig().Whitelist {
|
||||
if num_users > 0 {
|
||||
ret += ","
|
||||
}
|
||||
ret += fmt.Sprint("\"", wl_user, "\"")
|
||||
}
|
||||
ret += "]"
|
||||
return ret
|
||||
}
|
||||
|
||||
func setupDatabase() bool {
|
||||
/*
|
||||
db, err := bolt.Open("mc_man.db", 0600, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
err = db.Update(func(tx *bolt.Tx) error {
|
||||
return nil
|
||||
})
|
||||
*/
|
||||
/*
|
||||
var err error
|
||||
web_database, err = sql.Open("sqlite3", "mc_man.db")
|
||||
defer web_database.Close()
|
||||
|
||||
if err == nil {
|
||||
// Error opening the DB, can't log in
|
||||
return false
|
||||
}
|
||||
rows, err := db.Query("SELECT name FROM sqlite_master WHERE type='table' AND name='users';")
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
err := rows.Scan
|
||||
}
|
||||
// rows, err := db.Query("SELECT * FROM USERS")
|
||||
return true
|
||||
*/
|
||||
return true
|
||||
}
|
||||
|
||||
/* HTML Functions */
|
||||
func htmlHeader(title string) string {
|
||||
head := `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>`
|
||||
head += title
|
||||
head += `
|
||||
</title>
|
||||
</head>
|
||||
<body>
|
||||
`
|
||||
return head
|
||||
}
|
||||
|
||||
func htmlFooter() string {
|
||||
return `
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
}
|
Reference in New Issue
Block a user