mirror of
https://github.com/tuna/tunasync.git
synced 2025-04-20 20:22:46 +00:00
feature(manager): skeleton of status API
This commit is contained in:
parent
350767e501
commit
ed896b16c1
@ -24,6 +24,8 @@ type ServerConfig struct {
|
||||
type FileConfig struct {
|
||||
StatusFile string `toml:"status_file"`
|
||||
DBFile string `toml:"db_file"`
|
||||
// used to connect to worker
|
||||
CACert string `toml:"ca_cert"`
|
||||
}
|
||||
|
||||
func loadConfig(cfgFile string, c *cli.Context) (*Config, error) {
|
||||
|
36
manager/server.go
Normal file
36
manager/server.go
Normal file
@ -0,0 +1,36 @@
|
||||
package manager
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type worker struct {
|
||||
// worker name
|
||||
name string
|
||||
// url to connect to worker
|
||||
url string
|
||||
// session token
|
||||
token string
|
||||
}
|
||||
|
||||
func makeHTTPServer(debug bool) *gin.Engine {
|
||||
if !debug {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
r := gin.Default()
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"msg": "pong"})
|
||||
})
|
||||
// List jobs, status page
|
||||
r.GET("/jobs", func(c *gin.Context) {})
|
||||
// worker online
|
||||
r.POST("/workers/:name", func(c *gin.Context) {})
|
||||
// post job list
|
||||
r.POST("/workers/:name/jobs", func(c *gin.Context) {})
|
||||
// post job status
|
||||
r.POST("/workers/:name/jobs/:job", func(c *gin.Context) {})
|
||||
|
||||
return r
|
||||
}
|
37
manager/server_test.go
Normal file
37
manager/server_test.go
Normal file
@ -0,0 +1,37 @@
|
||||
package manager
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestHTTPServer(t *testing.T) {
|
||||
Convey("HTTP server should work", t, func() {
|
||||
s := makeHTTPServer(false)
|
||||
So(s, ShouldNotBeNil)
|
||||
port := rand.Intn(10000) + 20000
|
||||
go func() {
|
||||
s.Run(fmt.Sprintf("127.0.0.1:%d", port))
|
||||
}()
|
||||
time.Sleep(50 * time.Microsecond)
|
||||
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/ping", port))
|
||||
So(err, ShouldBeNil)
|
||||
So(resp.StatusCode, ShouldEqual, http.StatusOK)
|
||||
So(resp.Header.Get("Content-Type"), ShouldEqual, "application/json; charset=utf-8")
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
So(err, ShouldBeNil)
|
||||
var p map[string]string
|
||||
err = json.Unmarshal(body, &p)
|
||||
So(err, ShouldBeNil)
|
||||
So(p["msg"], ShouldEqual, "pong")
|
||||
})
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user