O'Reilly logo

Distributed Computing with Go by V.N. Nikhil Anurag

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

secure/secure.go

secure.go shows the logic for the simple server. It has been divided into four functions:

  • The requestHandler function to respond to incoming HTTP requests.
  • The isAuthorized function to check if the incoming request is authorized.
  • The getAuthorizedUser function to check if the token has an associated user. If the token does not have an associated user, then the token is considered to be invalid.
  • The main function to start the server.

Now let's look at the code:

// secure/secure.go package main import ( "fmt" "log" "net/http" "strings" ) var authTokens = map[string]string{ "AUTH-TOKEN-1": "User 1", "AUTH-TOKEN-2": "User 2", } // getAuthorizedUser tries to retrieve user for the given token. func getAuthorizedUser(token string) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required