| 1 |
package website.controller; |
| 2 |
|
| 3 |
import ufront.MVC; |
| 4 |
import ufront.ufadmin.controller.*; |
| 5 |
import website.api.*; |
| 6 |
import website.model.SiteDb; |
| 7 |
import haxelib.server.FileStorage; |
| 8 |
import haxe.io.*; |
| 9 |
using StringTools; |
| 10 |
using tink.CoreApi; |
| 11 |
using CleverSort; |
| 12 |
|
| 13 |
class HomeController extends Controller { |
| 14 |
|
| 15 |
@inject public var projectApi:ProjectApi; |
| 16 |
@inject public var projectListApi:ProjectListApi; |
| 17 |
@inject public var userApi:UserApi; |
| 18 |
|
| 19 |
// Perform init() after dependency injection has occured. |
| 20 |
@inject public function init( ctx:HttpContext ) { |
| 21 |
// All MVC actions come through HomeController (our index controller) first, so this is a good place to set global template variables. |
| 22 |
var r = ctx.request; |
| 23 |
var url = 'https://'+r.hostName+r.uri; |
| 24 |
if ( r.queryString!="" ) { |
| 25 |
url += '?'+r.queryString; |
| 26 |
} |
| 27 |
ViewResult.globalValues.set( "useWrapper", true ); |
| 28 |
ViewResult.globalValues.set( "pageUrl", url ); |
| 29 |
ViewResult.globalValues.set( "currentPage", r.uri ); |
| 30 |
ViewResult.globalValues.set( "todaysDate", Date.now() ); |
| 31 |
ViewResult.globalValues.set( "documentationPages", DocumentationController.getDocumentationPages() ); |
| 32 |
ViewResult.globalValues.set( "description", "Haxe is an open source toolkit based on a modern, high level, strictly typed programming language." ); |
| 33 |
ViewResult.globalValues.set( "searchTerm", ctx.session.get('searchTerm') ); |
| 34 |
ViewResult.globalValues.set( "escape", Util.escape); |
| 35 |
ViewResult.globalValues.set( "formatDate", Util.formatDate); |
| 36 |
ViewResult.globalValues.set( "extension", Path.extension); |
| 37 |
ViewResult.globalValues.set( "min", Math.min); |
| 38 |
ViewResult.globalValues.set( "max", Math.max); |
| 39 |
} |
| 40 |
|
| 41 |
@:route("/") |
| 42 |
public function homepage() { |
| 43 |
var allProjects = projectListApi.all().sure(); |
| 44 |
|
| 45 |
var latestProjects = projectListApi.latest( 12 * 3 ).sure() ; |
| 46 |
var popularProjects = prepareProjectList( projectListApi.all().sure() ); |
| 47 |
var users = userApi.getUserList().sure(); |
| 48 |
var tags = projectListApi.getTagList( 25 ).sure(); |
| 49 |
|
| 50 |
var hasRecentProject = new Map<String, Bool>(); |
| 51 |
latestProjects = [for (p in latestProjects) { |
| 52 |
if (p.p !=null && !hasRecentProject.exists(p.p.name)) { |
| 53 |
hasRecentProject.set(p.p.name, true); |
| 54 |
p; |
| 55 |
} |
| 56 |
}]; |
| 57 |
|
| 58 |
return new ViewResult({ |
| 59 |
title: "Haxelib - the Haxe package manager", |
| 60 |
description: "Haxelib is a tool that enables sharing libraries and code in the Haxe ecosystem.", |
| 61 |
pageUrl: context.request.uri, |
| 62 |
latestProjects: function(offset:Int, total:Int) return [for (i in offset...offset+total) latestProjects[i]], |
| 63 |
popularProjects: function(offset:Int, total:Int) return [for (i in offset...offset+total) popularProjects[i]], |
| 64 |
users: function(offset:Int, total:Int) return [for (i in offset...offset+total) users[i]], |
| 65 |
|
| 66 |
tags: tags, |
| 67 |
exampleCode: CompileTime.readFile( "website/homepage-example.txt" ), |
| 68 |
useWrapper: false, |
| 69 |
}); |
| 70 |
} |
| 71 |
|
| 72 |
@:route("/p/*") |
| 73 |
public var projectController:ProjectController; |
| 74 |
|
| 75 |
@:route("/recent/") |
| 76 |
public function recent() { |
| 77 |
var latestProjects = projectListApi.latest( 100 ).sure(); |
| 78 |
|
| 79 |
latestProjects = [for (p in latestProjects) if (p.p != null ) p]; |
| 80 |
|
| 81 |
return new ViewResult({ |
| 82 |
title: "Recent updates - the Haxe package manager", |
| 83 |
description: "List of the most recent changes of Haxe libraries.", |
| 84 |
projects: latestProjects, |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
@:route("/u/*") |
| 89 |
public var userController:UserController; |
| 90 |
|
| 91 |
@:route("/rss/") |
| 92 |
public var rssController:RSSController; |
| 93 |
|
| 94 |
@:route("/documentation/*") |
| 95 |
public var documentationController:DocumentationController; |
| 96 |
|
| 97 |
/** |
| 98 |
`/files` is backed by a `FileStorage`. |
| 99 |
In production, it should be routed by httpd to S3 using mod_proxy, thus |
| 100 |
this function should never be called. |
| 101 |
*/ |
| 102 |
@:route("/files/3.0/$fileName") |
| 103 |
public function downloadFile( fileName:String ) { |
| 104 |
return FileStorage.instance.readFile( |
| 105 |
'files/3.0/$fileName', |
| 106 |
function(path) { |
| 107 |
var r = new FilePathResult(path); |
| 108 |
r.setContentTypeByFilename(Path.withoutDirectory(path)); |
| 109 |
return r; |
| 110 |
} |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
@cacheRequest |
| 115 |
@:route("/t/") |
| 116 |
public function tagList():ViewResult { |
| 117 |
var tagList = projectListApi.getTagList( 50 ).sure(); |
| 118 |
|
| 119 |
// Build a tag cloud. |
| 120 |
var least = null, |
| 121 |
most = null, |
| 122 |
minSize = 10, |
| 123 |
maxSize = 140; |
| 124 |
for (t in tagList) { |
| 125 |
if ( least==null || t.count<least ) |
| 126 |
least = t.count; |
| 127 |
if ( most==null || t.count>most ) |
| 128 |
most = t.count; |
| 129 |
} |
| 130 |
function fontSizeForCount( count:Int ) { |
| 131 |
var countRange = most - least; |
| 132 |
var sizeRange = maxSize-minSize; |
| 133 |
var pos = (count - least) / countRange; |
| 134 |
return minSize + pos*sizeRange; |
| 135 |
} |
| 136 |
var tagCloud = [for (t in tagList) { tag:t.tag, size:fontSizeForCount(t.count) }]; |
| 137 |
tagCloud.cleverSort( _.tag ); |
| 138 |
|
| 139 |
return new ViewResult({ |
| 140 |
title: 'Haxelib Tags', |
| 141 |
description: 'Projects of popular tags on Haxelib', |
| 142 |
tags: tagList, |
| 143 |
tagCloud: tagCloud, |
| 144 |
taggedProjects: function(tagName:String, offset:Int, total:Int) { |
| 145 |
var projects = prepareProjectList( projectListApi.byTag( tagName ).sure() ); |
| 146 |
return [for (i in offset...offset + total) projects[i]]; |
| 147 |
} |
| 148 |
}); |
| 149 |
} |
| 150 |
|
| 151 |
// TODO: get ufront-mvc to support `/t/$tagName` and `/t/$tagName.json` as different routes. |
| 152 |
@cacheRequest |
| 153 |
@:route("/t/$tagName") |
| 154 |
public function tag( tagName:String ):ActionResult { |
| 155 |
if ( tagName.endsWith(".json") ) { |
| 156 |
return tagJson( tagName.substr(0,tagName.length-5) ); |
| 157 |
} |
| 158 |
else { |
| 159 |
|
| 160 |
var list = prepareProjectList( projectListApi.byTag( tagName ).sure() ); |
| 161 |
return new ViewResult({ |
| 162 |
title: 'Tag: $tagName', |
| 163 |
icon: 'fa-tag', |
| 164 |
currentTag: tagName, |
| 165 |
tags: projectListApi.getTagList( 50 ).sure(), |
| 166 |
description: 'A list of all projects on Haxelib with the tag "$tagName"', |
| 167 |
projects: list, |
| 168 |
}, "tagProjectList.html"); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
@cacheRequest |
| 173 |
@:route("/all") |
| 174 |
public function all() { |
| 175 |
var list = prepareProjectList( projectListApi.all().sure() ); |
| 176 |
return new ViewResult({ |
| 177 |
title: 'All Haxelibs', |
| 178 |
icon: 'fa-star', |
| 179 |
description: 'A list of every project uploaded on haxelib, sorted by popularity', |
| 180 |
projects: list, |
| 181 |
}, "projectList.html"); |
| 182 |
} |
| 183 |
|
| 184 |
@:route("/search") |
| 185 |
public function search( ?args:{ v:String } ) { |
| 186 |
var result = new ViewResult(); |
| 187 |
if ( args.v==null || args.v.length==0 ) { |
| 188 |
result.setVars({ |
| 189 |
title: 'Search Haxelib', |
| 190 |
description: 'Search Haxelib project names and descriptions', |
| 191 |
searchTerm: "", |
| 192 |
projects: null |
| 193 |
}); |
| 194 |
} |
| 195 |
else { |
| 196 |
context.session.set( 'searchTerm', args.v ); |
| 197 |
var list = prepareProjectList( projectListApi.search( args.v ).sure() ); |
| 198 |
result.setVars({ |
| 199 |
title: 'Search for "${args.v}"', |
| 200 |
description: 'Haxelib projects that match the search word "${args.v}"', |
| 201 |
projects: list, |
| 202 |
searchTerm: args.v, |
| 203 |
}); |
| 204 |
} |
| 205 |
return result; |
| 206 |
} |
| 207 |
|
| 208 |
static function prepareProjectList( list:Array<Project> ):Array<{ name:String, user:User, author:String, description:String, version:Version, downloads:Int }> { |
| 209 |
return [for (p in list) if (p != null && p.ownerObj != null && p.versionObj != null) { |
| 210 |
name: p.name, |
| 211 |
user: p.ownerObj, |
| 212 |
author: p.ownerObj.name, |
| 213 |
description: p.description, |
| 214 |
version: p.versionObj, |
| 215 |
downloads: p.downloads, |
| 216 |
}]; |
| 217 |
} |
| 218 |
|
| 219 |
@:route("/search.json") |
| 220 |
public function searchJson( args:{ v:String } ) |
| 221 |
return new JsonResult( projectListApi.search(args.v).sure() ); |
| 222 |
|
| 223 |
@cacheRequest |
| 224 |
@:route("/all.json") |
| 225 |
public function allJson() |
| 226 |
return new JsonResult( projectListApi.all().sure() ); |
| 227 |
|
| 228 |
@:route("/ufadmin/*") |
| 229 |
public function ufadmin() { |
| 230 |
return executeSubController( UFAdminHomeController ); |
| 231 |
} |
| 232 |
|
| 233 |
public function tagJson( tagName:String ) |
| 234 |
return new JsonResult( projectListApi.byTag(tagName).sure() ); |
| 235 |
} |