Simple Web API Server in Golang (1)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

To be an better Gopher, get your hands dirty. Topcoder offered a serials of challenges for learning Golang.

In this blog, I tried to implement "Go Learning Challenge - Simple Web-API Server"[1].

What's used in this challenge ? Following aspects and packages will be covered in this challenge

  • How to create a HTTP server: net/http
  • How to create routers (handlers for different URLs): regex + url
  • How to write HTTP responses: net/http
  • How to load JSON config: json + io/ioutil
  • How to parse URL query string: url
  • How to encode data in sha256/base64: sha256/base64  + strings
  • How to write tests in Golang: testing
  • How to parse command line arguments: flag
  • How to log: log

Configuration File example:

[    {        "domain": "topcoder.com",        "users": [            {                "username": "takumi",                "password": "ilovego"            },            {                "username": "teru",                "password": "ilovejava"            },            {                "username": "toshi",                "password": "iloveapex"            }        ]    },    {        "domain": "appirio.com",        "users": [            {                "username": "jun",                "password": "ilovetopcoder"            },            {                "username": "narinder",                "password": "ilovesamurai"            },            {                "username": "chris",                "password": "ilovesushi"            }        ]    }]

config.go

package SimpleWebAPIServerimport (    "io/ioutil"    "encoding/json"    "errors")type User struct {    UserName string `json:"username"`    Password string `json:"password"`}type Domain struct {    Name string `json:"domain"`    Users UserList `json:"users"`}type DomainWarehouse []Domaintype UserList []Userfunc ReadConfig(fn string) (DomainWarehouse, error) {    data, err := ioutil.ReadFile(fn)    if err != nil {        return nil, err    }    //fmt.Println(string(data))    var m DomainWarehouse    json.Unmarshal(data, &m)    return m, nil}func (dw DomainWarehouse) GetDomain(name string) (*Domain, error) {    for _, domain := range dw {        if domain.Name == name {            return &domain, nil        }    }    return nil, errors.New("Failed to find domain")}func (ul UserList) GetUser(username string) (*User, error) {    for _, user := range ul {        if user.UserName == username {            return &user, nil        }    }    return nil, errors.New("Failed to find user")}

 

tests for config.go

package SimpleWebAPIServerimport (    "testing"    "fmt")func TestReadConfig(t *testing.T) {    dw, err := ReadConfig("./users.json")    if err != nil {        fmt.Println(err)        t.Error("Failed to read config")    }    if dw == nil || len(dw) != 2 {        t.Error("Failed to unmarshal json objects")    }    if dw[0].Name != "topcoder.com" || len(dw[0].Users) != 3 {        t.Error("Incorrect value")    }}func TestGetDomain(t *testing.T) {    dw, err := ReadConfig("./users.json")    if err != nil {        fmt.Println(err)        t.Error("Failed to read config")    }    domain, err := dw.GetDomain("topcoder.com")    if err != nil {        fmt.Println(err)        t.Error("Failed to get domain")    }    if domain.Name != "topcoder.com" || len(domain.Users) != 3 {        t.Error("Incorrect value")    }}func TestGetUser(t *testing.T) {    dw, err := ReadConfig("./users.json")    if err != nil {        fmt.Println(err)        t.Error("Failed to read config")    }    domain, err := dw.GetDomain("topcoder.com")    if err != nil {        fmt.Println(err)        t.Error("Failed to get domain")    }    if domain.Name != "topcoder.com" || len(domain.Users) != 3 {        t.Error("Incorrect value")    }    ul := domain.Users    u, err := ul.GetUser("takumi")    if err != nil {        t.Error("Failed to get user")    }    if u.UserName != "takumi" || u.Password != "ilovego" {        t.Error("Invalid user values")    }}

 

 

After implementing this simple web api server, I got better understanding of Golang syntax and Web API stuffs. For more Web API server challenges, go to [3] about OAuth2.

 

[1] topcoder : http://www.topcoder.com/challenge-details/30046011/?type=develop&noncache=true

[2] git : https://coding.net/u/huys03/p/SimpleWebAPIServer/git

[3] next challenge: http://www.topcoder.com/challenge-details/30046224/?type=develop

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.