10 examples of 'golang http server' in Go

Every line of 'golang http server' 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

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}
43func server() {
44 // Wrap and register in one shot
45 http.HandleFunc(
46 sensor.TraceHandler("entry-handler", "/golang/entry",
47 func(writer http.ResponseWriter, req *http.Request) {
48 requestExit1(req)
49 time.Sleep(time.Second)
50 requestExit2(req)
51 },
52 ),
53 )
54
55 // Wrap and register in two separate steps, depending on your preference
56 http.HandleFunc("/golang/exit",
57 sensor.TracingHandler("exit-handler", func(w http.ResponseWriter, req *http.Request) {
58 time.Sleep(450 * time.Millisecond)
59 }),
60 )
61
62 // Wrap and register in two separate steps, depending on your preference
63 http.HandleFunc("/instana/exit",
64 sensor.TracingHandler("exit-handler", func(w http.ResponseWriter, req *http.Request) {
65 time.Sleep(450 * time.Millisecond)
66 }),
67 )
68
69 if err := http.ListenAndServe(":9060", nil); err != nil {
70 panic(err)
71 }
72}
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}
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}
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}
680func Server(url string, dsl ...func()) {
681 goadsl.Server(url, dsl...)
682}
90func server() {
91 router := gin.New()
92 router.GET("/test.header", func(c *gin.Context) {
93 h2 := testHeader{}
94 err := c.BindHeader(&h2)
95 if err != nil {
96 c.String(500, "fail")
97 return
98 }
99 })
100
101 router.Run()
102}
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}

Related snippets