{"id":178539,"date":"2013-05-01T11:40:03","date_gmt":"2013-05-01T07:40:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=178539"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=178539","title":{"rendered":"<span class=\"post_title\">\u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u0430\u043a\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0430 Golang<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0412 \u044f\u0437\u044b\u043a\u0435 Go, \u043f\u043e \u0441\u0443\u0442\u0438, \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438: \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0435 \u0444\u0430\u0439\u043b\u044b, \u0438 \u043f\u0430\u043a\u0435\u0442\u044b. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0442\u043e\u0440\u0443\u044e \u043d\u0430 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<p>  \u041f\u0430\u043a\u0435\u0442 \u2014 \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440. \u041f\u043e \u0441\u0432\u043e\u0435\u043c\u0443 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u043e\u043d\u0430 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435, \u0432\u0441\u0435\u043c \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0435, \u043b\u0438\u043d\u043a\u0443\u0435\u043c\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438. \u041f\u0430\u043a\u0435\u0442 \u0432 Go \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u0415\u0441\u043b\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u043e\u0439 \u0431\u0443\u043a\u0432\u044b, \u0442\u043e \u043e\u043d\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 (\u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0430\u043a\u0435\u0442\u0430), \u0435\u0441\u043b\u0438 \u0441 \u0431\u043e\u043b\u044c\u0448\u043e\u0439, \u0442\u043e \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u0435. \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u0430\u043a\u0435\u0442\u0430, \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 \u0438 \u0432\u043d\u0435 \u043f\u0430\u043a\u0435\u0442\u0430. \u0414\u0430\u043d\u043d\u0443\u044e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0430\u043a\u0435\u0442\u043e\u043c json, \u0432\u0445\u043e\u0434\u044f\u0449\u0435\u0439 \u0432 \u0441\u043e\u0441\u0442\u0430\u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u044f\u0437\u044b\u043a\u0430.<\/p>\n<p>  \u041f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<pre><code class=\"go\">type Link struct { \tname  string \turl string \ttitle string \tclass string }  links := make(map[string]Link) if err = json.Unmarshal(response, &links); err != nil {     return err } <\/code><\/pre>\n<p> <a name=\"habracut\"><\/a><br \/>  \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 json, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 c \u043f\u043e\u043b\u044f\u043c\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 (\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>Unmarshal<\/code> \u043a \u043f\u043e\u043b\u044f\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b <code>Link<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0430).<\/p>\n<p>  \u041a\u043e\u0434 \u043f\u0430\u043a\u0435\u0442\u0430 \u0434\u043e\u043b\u0436\u0435\u043d \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0435\u0433\u043e \u0438\u043c\u0435\u043d\u0435\u043c, \u0442\u043e \u0435\u0441\u0442\u044c \u0435\u0441\u043b\u0438 \u043f\u0430\u043a\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>ru\/sezn\/server<\/code>, \u0442\u043e \u0444\u0430\u0439\u043b\u044b <code>*.go<\/code> \u0431\u0443\u0434\u0443\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u043f\u0430\u043f\u043a\u0435 server, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0434\u043f\u0430\u043f\u043a\u043e\u0439 sezn \u0438 ru.<\/p>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0430\u043a\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0448\u0438\u0445 \u0432\u0435\u0431 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445: <a href=\"http:\/\/sezn.ru\/\">http:\/\/sezn.ru\/<\/a> \u0438 <a href=\"http:\/\/hashcode.ru\/\">http:\/\/hashcode.ru\/<\/a><\/p>\n<p>  \u0412 \u044f\u0437\u044b\u043a\u0435 Go \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0432\u043e\u044f \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0430\u044f http-\u0441\u0435\u0440\u0432\u0435\u0440. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438\u043c\u0435\u044e\u0442 \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u0430\u0439\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 http-\u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f (\u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0435\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d 80 \u043f\u043e\u0440\u0442). \u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0443\u043b\u044c <code>fastCGI<\/code> \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 <code>Apache2<\/code>. \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0444\u0430\u0439\u043b \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0445\u043e\u0442\u0441\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"apache\">&lt;VirtualHost *:80&gt;    ServerAdmin webmaster@hashcode.ru     DocumentRoot \/path\/to\/bin\/    ServerName sezn.ru     ErrorLog \/var\/log\/apache2\/sezn_error.log    LogLevel warn    CustomLog \/var\/log\/apache2\/sezn_warning.log combined     AddHandler fastcgi-script our_bin    FastCgiExternalServer \/path\/to\/bin\/our_bin -host 127.0.0.1:3489     RewriteEngine On    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f    RewriteRule ^(.*)$ \/our_bin [QSA,L] &lt;\/VirtualHost&gt; <\/code><\/pre>\n<p>  \u041f\u0430\u043a\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0432 \u0432\u0438\u0434\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0441 \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0444\u0430\u0439\u043b\u043e\u0432, \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0432\u044c\u044e\u0448\u0435\u043a, \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u201c\u043a\u0430\u043f\u0447\u0438\u201d \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435. \u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0440\u043e\u0443\u0442\u0438\u043d\u0433\u0430 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 Gorilla (<a href=\"http:\/\/www.gorillatoolkit.org\/\">http:\/\/www.gorillatoolkit.org\/<\/a>). \u0414\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043a\u0430\u043f\u0447\u0438 \u0431\u044b\u043b\u0430 \u0432\u0437\u044f\u0442\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <a href=\"https:\/\/github.com\/dchest\/captcha\">https:\/\/github.com\/dchest\/captcha<\/a>.<\/p>\n<p>  \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430:<\/p>\n<pre><code class=\"go\">type Server struct { \tRouter    *mux.Router \tTransport string \tAddres    string \tMediaPath string \tCachedFiles map[string]*CachedFile \tfn404     func(w http.ResponseWriter, r *http.Request) } <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043a\u044d\u0448\u0438\u0440\u0432\u043e\u0430\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0444\u0430\u0439\u043b\u043e\u0432 \u0432 \u043f\u0430\u043f\u043a\u0435 \u0441 \u043c\u0435\u0434\u0438\u0435\u0439.<\/p>\n<pre><code class=\"go\">type CachedFile struct { \tFileName  string \tFullPath  string \tFilePath  string \tFileExt   string \tFileData  []byte } <\/code><\/pre>\n<p>  \u0421\u0435\u0440\u0432\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441.<\/p>\n<pre><code class=\"go\">AddNamedHandler(pattern string, handler func(http.ResponseWriter, *http.Request), name string) AddHandler(pattern string, handler func(http.ResponseWriter, *http.Request)) Reverse(name string) *mux.Route Run() error SetRootMediaPath(path string) Set404ErrorHandler(fn func(w http.ResponseWriter, r *http.Request)) ServeHTTP(w http.ResponseWriter, r *http.Request) <\/code><\/pre>\n<p>  \u0410 \u0442\u0430\u0436\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441 \u043e\u0431\u043b\u0430\u0441\u0442\u044c\u044e \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0430\u043a\u0435\u0442\u0430.  <\/p>\n<pre><code class=\"go\">cacheFiles() renderFile(w http.ResponseWriter, filename string) error <\/code><\/pre>\n<p>  \u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043c\u044b \u043b\u0438\u0448\u044c \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b.<\/p>\n<pre><code class=\"go\">func InitServer(transport, addres string) *Server { \tserver := &Server{Router: &mux.Router{}, Transport: transport, Addres: addres}\treturn server } <\/code><\/pre>\n<p>  \u0417\u0430\u0442\u0435\u043c, \u043f\u043e \u0445\u043e\u0434\u0443 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0443\u043b\u0435\u0439, \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0435\/\u043d\u0435 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u043c\u0435\u0442\u043e\u0434 <code>AddNamedHandler<\/code> \u0438\u043b\u0438 <code>AddHandler<\/code>. <\/p>\n<p>  \u041a\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430.<\/p>\n<pre><code class=\"go\">func (server *Server) AddNamedHandler(pattern string, handler func(http.ResponseWriter, *http.Request), name string) { \tserver.Router.HandleFunc(pattern, handler).Name(name) } <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 Run. \u0412 \u043d\u0435\u043c \u043c\u044b \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u043c \u043c\u0435\u0434\u0438\u0430 \u0444\u0430\u0439\u043b\u044b (\u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443 \u043e \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 <a href=\"http:\/\/meta.hashcode.ru\/questions\/1158\/\">http:\/\/meta.hashcode.ru\/questions\/1158\/<\/a>) \u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a fastCGI \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430.<\/p>\n<pre><code class=\"go\">func (server *Server) Run() error { \tserver.cacheFiles() \tl, err := net.Listen(server.Transport, server.Addres) \tif err != nil { \t\treturn err \t}  \tfcgi.Serve(l, server.Router) \treturn nil } <\/code><\/pre>\n<p>  \u0414\u043e \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d \u043c\u0435\u0442\u043e\u0434 Run, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c\u0441\u044f \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u044b\u0439 \u0444\u0430\u0439\u043b \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0439\u0434\u0435\u043d. <\/p>\n<pre><code class=\"go\">func (server *Server) Set404ErrorHandler(fn func(w http.ResponseWriter, r *http.Request)) { \tserver.fn404 = fn \tserver.Router.NotFoundHandler = server } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0435\u0441\u0442\u044c \u0442\u043e\u043d\u043a\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u043c\u044b \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043e\u0448\u0438\u0431\u043a\u0438 404, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 Gorilla \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0435\u0440. \u0421\u0435\u0440\u0432\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 HTTP \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u0417\u0430 \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <code>ServeHTTP<\/code>.<\/p>\n<pre><code class=\"go\">func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { \terr := server.renderFile(w, r.URL.Path) \tif err != nil { \t\tw.Header().Set(&quot;Content-Type&quot;, &quot;text\/html; charset=utf-8&quot;) \t\tw.WriteHeader(http.StatusNotFound) \t\tserver.fn404(w, r) \t} } <\/code><\/pre>\n<p>  \u041c\u044b \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0432\u0441\u0435\u0445 \u0432\u044c\u044e\u0448\u0435\u043a \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0440\u043e\u0443\u0442\u0438\u043d\u0433\u0430 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 Gorilla. \u0415\u0441\u043b\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0442\u043e \u043c\u044b \u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043d\u0430\u0439\u0442\u0438 \u0444\u0430\u0439\u043b, \u0441 \u0442\u0430\u043a\u0438\u043c \u0438\u043c\u0435\u043d\u0435\u043c. \u0415\u0441\u043b\u0438 \u0444\u0430\u0439\u043b\u0430 \u043d\u0435\u0442, \u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043e\u0448\u0438\u0431\u043a\u0438 404.<\/p>\n<p>  \u041c\u0435\u0442\u043e\u0434 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0434\u0438\u0430 \u0444\u0430\u0439\u043b\u043e\u0432 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c:<\/p>\n<pre><code class=\"go\">func (server *Server) cacheFiles() { \tserver.CachedFiles = make(map[string]*CachedFile) \tdir, _ := filepath.Abs(server.MediaPath) \t  \tfilepath.Walk(dir, func(path string, info os.FileInfo, err error) error { \t\tif info.IsDir() { \t\t\treturn nil \t\t \t\t} \t\tfile, err := ioutil.ReadFile(path) \t\tfilePath := strings.Replace(path, dir, &quot;&quot;, -1) \t\tserver.CachedFiles[filePath] = &CachedFile { \t\t\tFileName: info.Name(), \t\t\tFullPath: path, \t\t\tFilePath: filePath, \t\t\tFileExt: filepath.Ext(path), \t\t\tFileData: file, \t\t} \t\treturn nil \t}); } <\/code><\/pre>\n<p>  \u0412 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0444\u0430\u0439\u043b\u0430, \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043b\u0438\u0448\u044c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u043f\u0443\u0442\u0438 \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0441 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u0438 \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442. \u0415\u0441\u043b\u0438 \u0444\u0430\u0439\u043b\u0430 \u0441 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u0430\u0434\u0440\u0435\u0441\u043e\u043c \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043d\u0430\u0439\u0442\u0438 \u0435\u0433\u043e \u043d\u0430 \u0434\u0438\u0441\u043a\u0435.<\/p>\n<pre><code class=\"go\">func (server *Server) renderFile(w http.ResponseWriter, filename string) error { \tvar file []byte \tvar ext string \tvar err error  \t \tif cachedFile, exist := server.CachedFiles[filename]; exist { \t\tfile = cachedFile.FileData \t\text = cachedFile.FileExt \t} else { \t\tfile, err = ioutil.ReadFile(server.MediaPath + filename) \t\tif err != nil { \t\t\treturn err \t\t} \t\text = filepath.Ext(server.MediaPath + filename) \t} \t \tif ext != &quot;&quot; { \t\tw.Header().Set(&quot;Content-Type&quot;, mime.TypeByExtension(ext)) \t} \tif file != nil { \t\tw.Write(file)\t\t \t}   \treturn nil } <\/code><\/pre>\n<p>  \u0418\u043c\u044f \u201cserver\u201d \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0435. \u041c\u044b \u0440\u0430\u0437\u043c\u0435\u0449\u0430\u0435\u043c \u043f\u0430\u043a\u0435\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u043e\u0442\u0430\u0446\u0438\u044e Java. \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043a\u0435\u0442, \u043f\u043e\u043b\u043d\u043e\u0435 \u0438\u043c\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u0430\u043a <code>ru\/sezn\/server<\/code>. \u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0430\u043a\u0435\u0442\u0430 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u0443\u044e \u0441 \u044f\u0437\u044b\u043a\u043e\u043c. <\/p>\n<pre><code class=\"bash\">go get ru\/sezn\/server <\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u0438 \u0432\u0441\u0435. \u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445 \u043a \u043f\u043e\u0441\u0442\u0443 \u0438\u043b\u0438 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0432\u0435\u0442\u043a\u0435 \u043d\u0430 \u0425\u044d\u0448\u041a\u043e\u0434\u0435 (<a href=\"http:\/\/hashcode.ru\/questions\/tagged\/go\/\">http:\/\/hashcode.ru\/questions\/tagged\/go\/<\/a>).<\/p>\n<p>  P. S. \u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u044b\u0432\u0430\u043b\u0441\u044f \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0440\u0430\u0437 \u043f\u043e\u0434 Go RC1 \u0438 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0437\u0434\u043d\u0435\u0439\/\u0440\u0430\u043d\u043d\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0435\u0439 \u044f\u0437\u044b\u043a\u0430.    \t \t\t   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/178539\/\"> http:\/\/habrahabr.ru\/post\/178539\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0412 \u044f\u0437\u044b\u043a\u0435 Go, \u043f\u043e \u0441\u0443\u0442\u0438, \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438: \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0435 \u0444\u0430\u0439\u043b\u044b, \u0438 \u043f\u0430\u043a\u0435\u0442\u044b. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0442\u043e\u0440\u0443\u044e \u043d\u0430 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<p>  \u041f\u0430\u043a\u0435\u0442 \u2014 \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440. \u041f\u043e \u0441\u0432\u043e\u0435\u043c\u0443 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u043e\u043d\u0430 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435, \u0432\u0441\u0435\u043c \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0435, \u043b\u0438\u043d\u043a\u0443\u0435\u043c\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438. \u041f\u0430\u043a\u0435\u0442 \u0432 Go \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438. \u0415\u0441\u043b\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u043e\u0439 \u0431\u0443\u043a\u0432\u044b, \u0442\u043e \u043e\u043d\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 (\u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0430\u043a\u0435\u0442\u0430), \u0435\u0441\u043b\u0438 \u0441 \u0431\u043e\u043b\u044c\u0448\u043e\u0439, \u0442\u043e \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c\u044b\u0435. \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u0430\u043a\u0435\u0442\u0430, \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 \u0438 \u0432\u043d\u0435 \u043f\u0430\u043a\u0435\u0442\u0430. \u0414\u0430\u043d\u043d\u0443\u044e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0430\u043a\u0435\u0442\u043e\u043c json, \u0432\u0445\u043e\u0434\u044f\u0449\u0435\u0439 \u0432 \u0441\u043e\u0441\u0442\u0430\u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u044f\u0437\u044b\u043a\u0430.<\/p>\n<p>  \u041f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<pre><code class=\"go\">type Link struct { \tname  string \turl string \ttitle string \tclass string }  links := make(map[string]Link) if err = json.Unmarshal(response, &#038;links); err != nil {     return err } <\/code><\/pre>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-178539","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/178539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=178539"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/178539\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=178539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=178539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=178539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}