server

package
v0.0.0-...-078eab7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package server contains all about http server.

Index

Examples

Constants

View Source
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

func AllowContentCharsetMiddleware(charsets ...string) func(next http.Handler) http.Handler

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

func CleanURLPathMiddleware() func(next http.Handler) http.Handler

CleanURLPathMiddleware clean url path middleware.

func DecompressMiddleware

func DecompressMiddleware() func(next http.Handler) http.Handler

DecompressMiddleware is make decompress.

func MakeMainHandler

func MakeMainHandler(cfg *config.Config) http.Handler

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:

func Run

func Run(cfg *config.Config)

Run http server.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL