github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/v2/library/go-web.md (about)

     1  ---
     2  title: Go Web
     3  keywords: go-web
     4  tags: [go-web]
     5  sidebar: home_sidebar
     6  permalink: /go-web
     7  summary: Go Web provides an interface for micro web apps
     8  ---
     9  
    10  # Overview
    11  
    12  Go Web provides a tiny HTTP web server library which leverages [go-micro](https://github.com/micro/go-micro) to create 
    13  micro web services as first class citizens in a microservice world. It wraps go-micro to give you service discovery, 
    14  heartbeating and the ability to create web apps as microservices.
    15  
    16  ## Features
    17  
    18  - **Service Discovery** - Services are automatically registered in service discovery on startup. Go Web includes 
    19  a http.Client with pre-initialised roundtripper which makes use of service discovery so you can use service names.
    20  
    21  - **Heartbeating** - Go Web apps will periodically heartbeat with service discovery to provide liveness updates. 
    22  In the event a service fails it will be removed from the registry after a pre-defined expiry time.
    23  
    24  - **Custom Handlers** - Specify your own http router for handling requests. This allows you to maintain full 
    25  control over how you want to route to internal handlers.
    26  
    27  - **Static Serving** - Go Web automatically detects a local static `html` dir and serves files if no route handler 
    28  is specified. A quick solution for those who want to write JS web apps as microservices.
    29  
    30  ## Getting Started
    31  
    32  - [Dependencies](#dependencies)
    33  - [Usage](#usage)
    34  - [Set Handler](#set-handler)
    35  - [Call Service](#call-service)
    36  - [Static Files](#static-files)
    37  
    38  ## Dependencies
    39  
    40  Go Web makes use of Go Micro which means it needs service discovery
    41  
    42  See the [go-micro](https://github.com/micro/go-micro#service-discovery) for install instructions
    43  
    44  ## Usage
    45  
    46  ```go
    47  service := web.NewService(
    48  	web.Name("example.com"),
    49  )
    50  
    51  service.HandleFunc("/foo", fooHandler)
    52  
    53  if err := service.Init(); err != nil {
    54  	log.Fatal(err)
    55  }
    56  
    57  if err := service.Run(); err != nil {
    58  	log.Fatal(err)
    59  }
    60  ```
    61  
    62  ## Set Handler
    63  
    64  You might have a preference for a HTTP handler, so use something else. This loses the ability to register endpoints in discovery 
    65  but we'll fix that soon.
    66  
    67  ```go
    68  import "github.com/gorilla/mux"
    69  
    70  r := mux.NewRouter()
    71  r.HandleFunc("/", indexHandler)
    72  r.HandleFunc("/objects/{object}", objectHandler)
    73  
    74  service := web.NewService(
    75  	web.Handler(r)
    76  )
    77  ```
    78  
    79  ## Call Service
    80  
    81  Go-web includes a http.Client with a custom http.RoundTripper that uses service discovery
    82  
    83  ```go
    84  c := service.Client()
    85  
    86  rsp, err := c.Get("http://example.com/foo")
    87  ```
    88  
    89  This will lookup service discovery for the service `example.com` and route to one of the available nodes.
    90  
    91  ## Static Files
    92  
    93  Go web was always meant as a way to register web apps where the majority of the code would be written in JS. To enable that by default, if no handler is registered on "/" and we find a local "html" directory then static files will be served.
    94  
    95  You will see a log output like so.
    96  
    97  ```
    98  2019/05/12 14:55:47 Enabling static file serving from /tmp/foo/html
    99  ```
   100  
   101  If you want to set this path manually use the StaticDir option. If a relative path is specified we will use os.Getwd() and prefix this.
   102  
   103  ```
   104  service := web.NewService(
   105  	web.Name("example.com"),
   106  	web.StaticDir("/tmp/example.com/html"),
   107  )
   108  
   109  ```
   110  
   111