saveName(event) {
event.preventDefault();
axios(`http://localhost:8000/post`, {
method: "post", data: {
first_name: this.state.first_name ,
last_name: this.state.last_name
}
})
axios.get('http://localhost:8000/get')
.then(rest => {
console.log(res.data) //вот про этот console.log() я говорю
this.setState({ names : res.data});
})
this.setState({first_name : ""})
this.setState({last_name : ""})
}
type MyFileServer struct {
server http.Handler
}
func (h MyFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//Свою логику проверки паролей/логинов
if r.Header.Get("login") == "admin" && r.Header.Get("password") == "password" {
h.server.ServeHTTP(w, r)
} else {
http.NotFound(w, r)
}
}
func main() {
fs := MyFileServer{server: http.FileServer(http.Dir("static"))}
http.Handle("/", fs)
http.ListenAndServe(":8000", nil)
}
curl -H "login:admin" -H "password:password" 127.0.0.1:8000/test.txt
t.ExecuteTemplate(w, "shedule", struct{Test string}{Test: test})
type Department struct {
Id int `json:"id"`
Name string `json:"name"`
Phone string `json:"phone"`
}
type Department struct {
Id int `json:"id"`
Name string `json:"name"`
Phone string
AnyField string `json:"part"`
}
{id: 123, name: "Имя", Phone: "123", part: "AnyField"}
package client_vk
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
//Данные приложения для windows клиента
//https://new.vk.com/windows_app
//Это приложение имеет права на прямую авторизацию с логином и паролем
const (
client_id = "3697615"
client_secret = "AlVXZFMUqyrnABp8ncuU"
authHost = "oauth.vk.com"
authPath = "/token"
)
func GetClientWithLoginPassword(username, password string) (*Client, bool) {
token := GetTokenWithLoginPassword(username, password)
if token == "" {
return &Client{}, false
}
return DefaultClient(token), true
}
func GetTokenWithLoginPassword(username, password string) string {
params := paramsMake(username, password)
req, err := requestmake(params)
if err != nil {
return ""
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return ""
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return ""
}
var response ResponseAuthLoginPass
json.Unmarshal(b, &response)
return response.AccessToken
}
func paramsMake(username, password string) url.Values {
params := url.Values{}
params.Add("client_id", client_id)
params.Add("client_secret", client_secret)
params.Add("grant_type", "password")
params.Add("v", defailtVersion)
params.Add("username", username)
params.Add("password", password)
return params
}
func requestmake(params url.Values) (*http.Request, error) {
u := url.URL{}
u.Host = authHost
u.Scheme = defaultScheme
u.Path = authPath
u.RawQuery = params.Encode()
req, err := http.NewRequest(defaultMethod, u.String(), nil)
return req, err
}
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
urls := []string{"http://google.ru", "http://vk.com", "http://ya.ru"}
chanUrls := make(chan string, 100)
chanRes := make(chan http.Response, 100)
chanPrint := make(chan string, 100)
for _, url := range urls {
chanUrls <- url
}
close(chanUrls)
go httpGet(chanUrls, chanRes)
go analys(chanRes, chanPrint)
printRes(chanPrint)
}
func httpGet(chanUrls <-chan string, chanRes chan<- http.Response) {
defer close(chanRes)
for url := range chanUrls {
res, err := http.DefaultClient.Get(url)
if err == nil {
chanRes <- *res
}
}
}
func analys(chanRes <-chan http.Response, chanPrint chan<- string) {
defer close(chanPrint)
for res := range chanRes {
body, err := ioutil.ReadAll(res.Body)
if err == nil {
chanPrint <- string(body)
}
}
}
func printRes(chanPrint <-chan string) {
for pr := range chanPrint {
fmt.Println(pr)
}
}