Skip to content

Commit

Permalink
refactor modules, config, webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
PaluMacil committed Aug 17, 2018
1 parent eba3b55 commit 7894ad9
Show file tree
Hide file tree
Showing 21 changed files with 272 additions and 191 deletions.
8 changes: 4 additions & 4 deletions api/groupapi/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"net/http"

"github.com/PaluMacil/dwn/api"
"github.com/PaluMacil/dwn/app"
"github.com/PaluMacil/dwn/database"
)

type Module struct {
*app.App
Db *database.Database
}

func New(app *app.App) *Module {
func New(db *database.Database) *Module {
return &Module{
App: app,
Db: db,
}
}

Expand Down
14 changes: 9 additions & 5 deletions api/infoapi/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ package infoapi
import (
"net/http"

"github.com/PaluMacil/dwn/configuration"

"github.com/PaluMacil/dwn/api"
"github.com/PaluMacil/dwn/app"
"github.com/PaluMacil/dwn/database"
)

type Module struct {
*app.App
Config configuration.Configuration
Db *database.Database
}

func New(app *app.App) *Module {
func New(db *database.Database, config configuration.Configuration) *Module {
return &Module{
App: app,
Db: db,
Config: config,
}
}

Expand All @@ -28,7 +32,7 @@ func (mod Module) ServeHTTP(w http.ResponseWriter, r *http.Request) {
route := InfoRoute(api.GetRoute(w, r, mod.Db))
switch route.Endpoint {
case "server":
route.handleServerInfo(mod.App)
route.handleServerInfo(mod.Config)
case "permissions":
route.handlePermissions()
}
Expand Down
11 changes: 6 additions & 5 deletions api/infoapi/serverinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"encoding/json"
"net/http"

"github.com/PaluMacil/dwn/app"
"github.com/PaluMacil/dwn/dwn"
"runtime"

"github.com/PaluMacil/dwn/configuration"
"github.com/PaluMacil/dwn/dwn"
)

// api/info/server
func (rt *InfoRoute) handleServerInfo(app *app.App) {
func (rt *InfoRoute) handleServerInfo(config configuration.Configuration) {
if rt.Current == nil {
http.Error(rt.W, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
Expand All @@ -26,7 +27,7 @@ func (rt *InfoRoute) handleServerInfo(app *app.App) {
return
}

resp := InfoResponse{app, info, runtime.Version()}
resp := InfoResponse{config, info, runtime.Version()}

if err := json.NewEncoder(rt.W).Encode(resp); err != nil {
rt.API().ServeInternalServerError(err)
Expand All @@ -38,7 +39,7 @@ func (rt *InfoRoute) handleServerInfo(app *app.App) {
}

type InfoResponse struct {
*app.App
Config configuration.Configuration `json:"config"`
dwn.SetupInfo
GoVersion string `json:"goVersion"`
}
12 changes: 6 additions & 6 deletions api/searchapi/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import (
"net/http"

"github.com/PaluMacil/dwn/api"
"github.com/PaluMacil/dwn/app"
"github.com/PaluMacil/dwn/database"
)

type Module struct {
*app.App
Db *database.Database
}

func New(app *app.App) *Module {
func New(db *database.Database) *Module {
return &Module{
App: app,
Db: db,
}
}

type SearchRoute api.Route

func (ur SearchRoute) API() api.Route {
return api.Route(ur)
func (rt SearchRoute) API() api.Route {
return api.Route(rt)
}

// api/search/...
Expand Down
33 changes: 33 additions & 0 deletions api/typeaheadapi/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package typeaheadapi

import (
"net/http"

"github.com/PaluMacil/dwn/api"
"github.com/PaluMacil/dwn/database"
)

type Module struct {
Db *database.Database
}

func New(db *database.Database) *Module {
return &Module{
Db: db,
}
}

type TypeaheadRoute api.Route

func (rt TypeaheadRoute) API() api.Route {
return api.Route(rt)
}

// api/typeahead/...
func (mod Module) ServeHTTP(w http.ResponseWriter, r *http.Request) {
route := TypeaheadRoute(api.GetRoute(w, r, mod.Db))
switch route.Endpoint {
case "user":
route.handleUser()
}
}
31 changes: 31 additions & 0 deletions api/typeaheadapi/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package typeaheadapi

import (
"encoding/json"
"net/url"
)

// api/typeahead/user?query=searchstring
func (rt *TypeaheadRoute) handleUser() {
switch rt.R.Method {
case "GET":
qry, err := url.QueryUnescape(rt.R.URL.Query().Get("query"))
if len(qry) < 2 || err != nil {
rt.API().ServeBadRequest()
return
}
//TODO: only allow logged in users to search
user, err := rt.Db.Users.CompletionSuggestions(qry) //TODO: check for absence of @ and search by username
if err != nil {
rt.API().ServeInternalServerError(err)
return
}
if err := json.NewEncoder(rt.W).Encode(user); err != nil {
rt.API().ServeInternalServerError(err)
return
}
default:
rt.API().ServeMethodNotAllowed()
return
}
}
12 changes: 6 additions & 6 deletions api/userapi/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import (
"net/http"

"github.com/PaluMacil/dwn/api"
"github.com/PaluMacil/dwn/app"
"github.com/PaluMacil/dwn/database"
)

type Module struct {
*app.App
Db *database.Database
}

func New(app *app.App) *Module {
func New(db *database.Database) *Module {
return &Module{
App: app,
Db: db,
}
}

type UserRoute api.Route

func (ur UserRoute) API() api.Route {
return api.Route(ur)
func (rt UserRoute) API() api.Route {
return api.Route(rt)
}

// api/user/...
Expand Down
12 changes: 6 additions & 6 deletions api/usergroupapi/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import (
"net/http"

"github.com/PaluMacil/dwn/api"
"github.com/PaluMacil/dwn/app"
"github.com/PaluMacil/dwn/database"
)

type Module struct {
*app.App
Db *database.Database
}

func New(app *app.App) *Module {
func New(db *database.Database) *Module {
return &Module{
App: app,
Db: db,
}
}

type UserGroupRoute api.Route

func (ur UserGroupRoute) API() api.Route {
return api.Route(ur)
func (rt UserGroupRoute) API() api.Route {
return api.Route(rt)
}

// api/usergroup/...
Expand Down
6 changes: 5 additions & 1 deletion app/app.go → application/app.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package application

import (
"fmt"
Expand All @@ -9,6 +9,7 @@ import (
"github.com/PaluMacil/dwn/database/repo"
"github.com/PaluMacil/dwn/database/search"
"github.com/PaluMacil/dwn/database/stores/badgerstore"
"github.com/PaluMacil/dwn/webserver"
)

func New() (*App, error) {
Expand All @@ -17,9 +18,11 @@ func New() (*App, error) {
if err != nil {
return nil, fmt.Errorf(`initializing default database: %s`, err)
}
web := webserver.New(db, config)
return &App{
Config: config,
Db: db,
Web: web,
}, nil
}

Expand Down Expand Up @@ -51,4 +54,5 @@ func defaultDatabase(config configuration.DatabaseConfiguration) (*database.Data
type App struct {
Config configuration.Configuration `json:"config"`
Db *database.Database `json:"-"`
Web *webserver.WebServer `json:"-"`
}
Loading

0 comments on commit 7894ad9

Please sign in to comment.