10 examples of 'http server golang' in Go

Every line of 'http server golang' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
21func serverHTTP() *egin.Component {
22 server := egin.Load("server.http").Build()
23 server.GET("/hello", func(ctx *gin.Context) {
24 ctx.JSON(200, "Hello")
25 return
26 })
27 return server
28}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

83func (s *Server) ServerHttp(w http.ResponseWriter, r *http.Request) {
84 switch s.config.Environment {
85 case "staging", "production":
86 s.staticAssetsHandlerForProd(w, r)
87 default:
88 s.staticAssetsHandler(w, r)
89 }
90
91}
46func server() {
47 router := gin.New()
48 router.POST("/upload/file", func(c *gin.Context) {
49 var file bytes.Buffer
50 io.Copy(&file, c.Request.Body)
51 fmt.Printf("server:file size = %d\n", file.Len())
52 })
53
54 router.Run()
55}
22func redirectHttps(w http.ResponseWriter, r *http.Request){
23 host := strings.Split(r.Host, ":")[0]
24 u := r.URL
25 u.Host = host
26 u.Scheme="https"
27 log.Println(u.String())
28 http.Redirect(w,r,u.String(), http.StatusMovedPermanently)
29}
411func runServerHTTP(host string) {
412 addr := fmt.Sprintf("%s:%d", host, *flagHTTPPort)
413 http.HandleFunc("/", handleHTTP)
414 http.HandleFunc("/debug/reload", handleReload)
415 http.HandleFunc("/debug/toggle", handleToggle)
416 log.Println("HTTP: Started at", addr)
417 log.Fatalln(http.ListenAndServe(addr, nil))
418 panic("not reachable")
419}
30func main() {
31 l, err := net.Listen("tcp", ":5000")
32 if err != nil {
33 fmt.Printf("cannot create listener: %v", err)
34 os.Exit(1)
35 }
36 s := &http.Server{
37 Handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
38 fmt.Fprint(rw, "I am running under k8s!\n")
39 }),
40 }
41
42 fmt.Println("Listening on http://localhost:5000 hit Ctrl+C to stop me...")
43
44 if err = s.Serve(l); err != nil {
45 fmt.Printf("cannot serve: %v", err)
46 os.Exit(1)
47 }
48
49}
85func (s *HTTP) servHTTPS(r *request, c net.Conn) {
86 rc, dialer, err := s.proxy.Dial("tcp", r.uri)
87 if err != nil {
88 io.WriteString(c, r.proto+" 502 ERROR\r\n\r\n")
89 log.F("[http] %s <-> %s [c] via %s, error in dial: %v", c.RemoteAddr(), r.uri, dialer.Addr(), err)
90 return
91 }
92 defer rc.Close()
93
94 io.WriteString(c, "HTTP/1.1 200 Connection established\r\n\r\n")
95
96 log.F("[http] %s <-> %s [c] via %s", c.RemoteAddr(), r.uri, dialer.Addr())
97
98 if err = proxy.Relay(c, rc); err != nil {
99 log.F("[http] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), r.uri, dialer.Addr(), err)
100 // record remote conn failure only
101 if !strings.Contains(err.Error(), s.addr) {
102 s.proxy.Record(dialer, false)
103 }
104 }
105}
47func main() {
48 color := getColor()
49
50 fmt.Printf("Booted with color: #%s", color)
51 fmt.Println()
52
53 go serveTCP()
54
55 http.HandleFunc("/", httpHandler)
56 fmt.Println("listening with http on :8080 and tcp on :8081")
57 http.ListenAndServe(":8080", nil)
58}
58func createGoServer(options Options, handler http.Handler) *http.Server {
59 if options.UseHTTP {
60 return goHTTPServerWithPort(options.HTTPPort, handler)
61 }
62
63 return goTLSServerWithDomain(options.Domain, handler)
64}
44func (h *XHTTPHandler) ServeXHTTP(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
45 stop := false
46 for _, handler := range h.reqHandlers {
47 stop = handler.Handle(w, r, params)
48 if stop {
49 return
50 }
51 }
52
53 h.targetHandler.Handle(w, r, params)
54
55 for _, handler := range h.respHandlers {
56 handler.Handle(w, r, params)
57 }
58}

Related snippets