func f(v interface{}) {
switch v.(type) {
case int:
fmt.Println("int", v)
case string:
fmt.Println("string", v)
default:
panic(fmt.Sprintf("f: unsupported type %T", v))
}
}
func new_query(Query string, args ...interface{}) {
db := mysql.New("tcp", "", "127.0.0.1:3306", "root", "12345", "test")
err := db.Connect()
if err != nil {
log.Println("Не удалось подключиться к базе данных")
return
}
defer db.Close()
stmt, err := db.Prepare(Query)
_, err = stmt.Run(args...)
if err != nil {
log.Println("Не удалось выполнить запрос")
return
}
}
Name := "Название"
CreatedBy := 777
query := "insert into table(Name,CreatedBy) values (?,?)"
new_query(query, Name, CreatedBy)
type struct_json struct {
Function string
Data json.RawMessage
}
type get_Dir_Db_struct_json struct {
Param1 string
Param2 string
}
var var_struct_json = &struct_json{}
var b = []byte(`{"function": "get_Dir_Db", "data": {"param1":"111","param2":"222"} }`)
err := json.Unmarshal(b, var_struct_json)
if err != nil {
log.Println("error:", err)
}
switch var_struct_json.Function {
case "get_Dir_Db":
var var_get_Dir_Db_struct_json = &get_Dir_Db_struct_json{}
err := json.Unmarshal(var_struct_json.Data, var_get_Dir_Db_struct_json)
if err != nil {
log.Println("error:", err)
}
log.Println("-",var_get_Dir_Db_struct_json.Param1)
case "other":
// ...
default:
log.Println("Функция,которую отослал клиент, не распознана- ", var_struct_json.Function)
}
type logWrapper struct {
http.Handler
}
func (wr logWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.Method, r.URL, time.Now())
wr.Handler.ServeHTTP(w, r)
}
func main() {
router := mux.NewRouter()
s := http.StripPrefix("/static/", http.FileServer(http.Dir("./files/")))
router.HandleFunc("/", Index)
router.HandleFunc("/login", LogIn)
router.HandleFunc("/logout", LogOut)
FileHandler := http.HandlerFunc(File)
router.Handle("/file.html", logWrapper{FileHandler})
router.PathPrefix("/static/").Handler(s)
http.Handle("/", router)
http.ListenAndServe(":5000", nil)
}
func Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello")
}
func File(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "это обрабатывается в обёртке)) ")
}