Documentation ¶
Overview ¶
Package server contains all about http server.
Index ¶
- Constants
- func AllowContentCharsetMiddleware(charsets ...string) func(next http.Handler) http.Handler
- func CleanURLPathMiddleware() func(next http.Handler) http.Handler
- func DecompressMiddleware() func(next http.Handler) http.Handler
- func MakeMainHandler(cfg *config.Config) http.Handler
- func Run(cfg *config.Config)
- type CustomResponseWriter
Examples ¶
Constants ¶
const ( UTF8 = "UTF-8" TextHTML = "text/html" TextPlain = "text/plain" ApplicationJSON = "application/json" TextPlainCharsetUTF8 = "text/plain;charset=UTF-8" ApplicationJSONCharsetUTF8 = "application/json;charset=UTF-8" )
Content-Type header values.
Variables ¶
This section is empty.
Functions ¶
func AllowContentCharsetMiddleware ¶
AllowContentCharsetMiddleware generates a handler that writes a 415 Unsupported Media Type response if none of the charsets match. An empty charset will allow requests with no Content-Type header or no specified charset. Skip check for empty content body. Basically this is just a wrapper to chi middleware ContentCharset, with check bout empty content body.
func CleanURLPathMiddleware ¶
CleanURLPathMiddleware clean url path middleware.
func DecompressMiddleware ¶
DecompressMiddleware is make decompress.
func MakeMainHandler ¶
MakeMainHandler make main handlers.
Example (GetURL) ¶
Example for get origina url endpoint.
apiAddress := "http://example.com:8080" endpoint := "/asadg" resp, err := http.Get(apiAddress + endpoint) if err != nil { log.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { log.Fatalf("Unexpected response status code %d", resp.StatusCode) } originalURL := resp.Header.Get("Location") log.Printf("Original URL is %q", originalURL)
Output:
Example (SaveNewURL) ¶
Example for short new url endpoint.
apiAddress := "http://example.com:8080" endpoint := "/" toSaveURL := "http://site.com/my-link" resp, err := http.Post(apiAddress+endpoint, "text/plain", bytes.NewBufferString(toSaveURL)) if err != nil { log.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { log.Fatalf("Unexpected response status code %d", resp.StatusCode) } respBodyBytes, err := io.ReadAll(resp.Body) if err != nil { log.Fatal(err) } respData := struct { Result string `json:"result"` }{} err = json.Unmarshal(respBodyBytes, &respData) if err != nil { log.Fatal(err) } log.Printf("Original URL %q saved as %q", toSaveURL, respData.Result)
Output:
Types ¶
type CustomResponseWriter ¶
type CustomResponseWriter struct { Body []byte StatusCode int // contains filtered or unexported fields }
CustomResponseWriter is custom response writer.
func NewCustomResponseWriter ¶
func NewCustomResponseWriter() *CustomResponseWriter
NewCustomResponseWriter create new custom response writer.
func (*CustomResponseWriter) Header ¶
func (w *CustomResponseWriter) Header() http.Header
Header return header.
func (*CustomResponseWriter) Write ¶
func (w *CustomResponseWriter) Write(b []byte) (int, error)
Write response body.
func (*CustomResponseWriter) WriteHeader ¶
func (w *CustomResponseWriter) WriteHeader(statusCode int)
WriteHeader write response header.
func (*CustomResponseWriter) WriteToNext ¶
func (w *CustomResponseWriter) WriteToNext(nw http.ResponseWriter) (int, error)
WriteToNext write everything to next http.ResponseWriter.